using UnityEngine;

public class GameManager : MonoBehaviour
{
    public static GameManager instance;
    private Player _player;
    
    private int comboCount = 0;
    
    [SerializeField] private float maxTimeScale = 1.3f;
    [SerializeField] private int comboForMaxSpeed = 30;

    private void Awake()
    {
        if(instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(gameObject);
            return;
        }
        
        GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
        if (playerObj != null)
        {
            _player = playerObj.GetComponent<Player>();
        }
    }

    private void OnEnable()
    {
        Entity.OnEnemyDeathEvent += OnEnemyKilled;
    }

    private void OnDisable()
    {
        Entity.OnEnemyDeathEvent -= OnEnemyKilled;
    }

    private void OnEnemyKilled(int reward)
    {
        AddCombo();
        _player.Heal(reward);
    }

    private void AddCombo()
    {
        comboCount++;
        
        float speedRatio = (float)comboCount / comboForMaxSpeed;
        Time.timeScale = 1 + ((maxTimeScale - 1) * speedRatio);
        
        if (Time.timeScale > maxTimeScale) Time.timeScale = maxTimeScale;
    }

    public void ResetCombo()
    {
        comboCount = 0;
        Time.timeScale = 1f;
    }
}