using UnityEngine;
using UnityEngine.UI;

public class HealthManager : MonoBehaviour
{
    public bool isEnabled = true;
    public bool isGameOverEnabled = false;
    public float maxHealth = 30f;
    public float minHealth = 0f;
    public float health = 30f;
    public float healthPerSecond = 1f;
    public Text uiText;
    public Slider slider;

    private void Start()
    {
        if (isEnabled)
        {
            updateUI();
        }
    }

    private void Update()
    {
        if (isEnabled)
        {
            health -= healthPerSecond * Time.deltaTime;
            if (health < minHealth) health = minHealth;
            if (isGameOverEnabled && health <= 0)
            {
                GameObject.FindGameObjectsWithTag("GameManager")[0].GetComponent<Game_Manager>().triggerGameOver();
                //GameObject.FindGameObjectsWithTag("MasterManager")[0].GetComponent<GameInput_Manager>().actionTriggerGameOver();
                //GameObject.FindGameObjectsWithTag("MasterManager")[0].GetComponent<GameInput_Manager>().enable(false);
            }
            updateUI();
        }
    }

    public void addHealth(float add)
    {
        if (isEnabled)
        {
            this.health += add;
            if (health > maxHealth) health = maxHealth;
            if (health < minHealth) health = minHealth;
            updateUI();
        }
    }

    private void updateUI()
    {
        if (isEnabled)
        {
            if (uiText != null) uiText.text = "" + (int)health;
            if (slider != null) slider.value = (int)health;
        }
    }
}
