using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Serialization;

namespace AsteroidGame.Handlers
{
    public class ScoreHandler : HandlerBase
    {
        [Header("UI")]
        [SerializeField]
        private TextMeshProUGUI displayBalance;

        [Header("Parameters")]
        [SerializeField]
        private int startHealth = 5;
        [SerializeField] 
        private int startBalance = 100;
        [Header("Stats")]
        [SerializeField]
        private int currentHealth;
        [SerializeField] 
        private int currentBalance;
        public int CurrentBalance => currentBalance;

        private void Start()
        {
            currentHealth = startHealth;
            currentBalance = startBalance;
            UpdateGUI();
        }

        public void ModifyHealth(GameObject enemy)
        {
            currentHealth -= 1;
            CheckIfYouLost();
        }

        public void ModifyHealth(int amount)
        {
            currentHealth += amount;
            CheckIfYouLost();
        }

        private void CheckIfYouLost(){
            if(currentHealth <= 0)
            {
                Debug.Log("You lost");
                Reload();
            }
        }

        public void ModifyWealth(int amount){
            currentBalance += amount;
            UpdateGUI();
            // Debug.Log($"Wealth modification. Change:{_amount}. Current: {wealthAmount}");
        }

        private void Reload()
        {
            int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
            SceneManager.LoadScene(currentSceneIndex);
        }

        private void UpdateGUI(){
            displayBalance.text = $"Gold: {currentBalance.ToString()}";
        }

    }
}
