using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;

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()}";
        }

    }
}
