using UnityEngine;
using UnityEngine.UI;

public class PlayerEvents : MonoBehaviour
{
    [Header("Vida del jugador")]
    [SerializeField] private int maxLife = 100;
    [SerializeField] private int currentLife;

    [Header("UI")]
    [SerializeField] private Slider lifeSlider; // referencia al slider en la UI
    public Animator anim;

    private void Start()
    {
        currentLife = maxLife;

        // Configurar el slider
        if (lifeSlider != null)
        {
            lifeSlider.maxValue = maxLife;
            lifeSlider.value = currentLife;
        }
    }

    private void Update()
    {
        if (lifeSlider != null)
        {
            lifeSlider.value = currentLife;
        }
    }

    public void MakeDamage(GameObject otherCollider)
    {
        Enemy e = otherCollider.GetComponent<Enemy>();
        if (e != null)
        {
            e.ReceiveDamage(5);
        }
    }

    public void ReceiveDamage(int received)
    {
        currentLife -= received;
        if (currentLife < 0) currentLife = 0;
        Death();
        anim.Play("Rig_Dano");
        GetComponent<PlayerMovement>().enabled = false;
    }

    private void Death()
    {
        if (currentLife <= 0)
        {
            anim.Play("Rig_Muerte");
        }
    }

    public void StopMove(){
        GetComponent<PlayerMovement>().enabled = true;
    }
}
