using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class NamePlateController : MonoBehaviour
{
    [SerializeField] private TMP_Text health_TMP;
    [SerializeField] private TMP_Text mana_TMP;
    [SerializeField] private Image healthFill;
    [SerializeField] private Image manaFill;

    Health health;
    Mana mana;

    private void Awake()
    {
        health = GetComponentInParent<Health>();
        mana = GetComponentInParent<Mana>();

        health.onResourceChanged.AddListener(UpdateCurrentHealth);
        health.onMaxHealthChanged.AddListener(UpdateMaxHealth);

        mana.onResourceChanged.AddListener(UpdateCurrentMana);
        mana.onMaxManaChanged.AddListener(UpdateMaxMana);
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    public void UpdateCurrentHealth(float value)
    {
        healthFill.fillAmount = value / health.GetMaxValue();
        UpdateHealthTMP();
    }
    public void UpdateMaxHealth(float value)
    {
        healthFill.fillAmount = health.GetCurrentValue() / value;
        UpdateHealthTMP();
    }

    public void UpdateHealthTMP()
    {
        health_TMP.text = $"{health.GetCurrentValue().ToString("F1")}/{health.GetMaxValue().ToString("F1")}";
    }

    public void UpdateCurrentMana(float value)
    {
        manaFill.fillAmount = value / mana.GetMaxValue();
        UpdateManaTMP();
    }
    public void UpdateMaxMana(float value)
    {
        manaFill.fillAmount = mana.GetCurrentValue() / value;
        UpdateManaTMP();
    }

    public void UpdateManaTMP()
    {
        mana_TMP.text = $"{mana.GetCurrentValue().ToString("F1")}/{mana.GetMaxValue().ToString("F1")}";
    }
}
