using System;
using Core;
using Script.Gameplay.Player;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace UI
{
    public class PlayerHpViewer : UIBase
    {
        private PlayerController _playerController;
        public TMP_Text hpText;

        private void Awake()
        {
            ControllerLocator.Instance.TryGetWait<PlayerController>(OnGet);
        }

        private void OnGet(PlayerController playerController)
        {
            _playerController = playerController;
            _playerController.OnHealthChanged += UpdateHpText;
            UpdateHpText(_playerController.CurrentHealth);
        }

        private void UpdateHpText(int currentHealth)
        {
            hpText.text = "HP: " +  currentHealth.ToString();
        }

        private void OnDestroy()
        {
            _playerController.OnHealthChanged -= UpdateHpText;
        }
    }
}