using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace UI {
    public class UnitWorldUI : MonoBehaviour {
        [SerializeField] private TextMeshProUGUI actionPointsText;
        [SerializeField] private Unit unit;
        [SerializeField] private Image healthBarImage;
        [SerializeField] private HealthSystem healthSystem;

        private void Start() {
            Unit.OnAnyActionPointsChanged += Unit_OnAnyActionPointsChanged;
            healthSystem.OnDamage += HealthSystem_OnDamage;
            UpdateActionPointsText();
            UpdateHealthBar();
        }

        private void HealthSystem_OnDamage(object sender, EventArgs e) => UpdateHealthBar();

        private void Unit_OnAnyActionPointsChanged(object sender, EventArgs e) => UpdateActionPointsText();

        private void UpdateActionPointsText() => actionPointsText.text = unit.ActionPoints.ToString();

        private void UpdateHealthBar() => healthBarImage.fillAmount = healthSystem.HealthNormalized;
    }
}
