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

namespace UI {
    public class TurnSystemUI : MonoBehaviour {
        [FormerlySerializedAs("NextTurnButton")] [SerializeField] private Button nextTurnButton;
        [FormerlySerializedAs("TurnText")] [SerializeField] private TextMeshProUGUI turnText;
        [SerializeField] private GameObject enemyTurnVisualGameObject;

        private void Start() {
            nextTurnButton.onClick.AddListener(() => TurnSystem.Instance.NextTurn());
            TurnSystem.Instance.OnTurnChanged += TurnSystem_OnTurnChanged;
            UpdateTurnText();
            UpdateEnemyTurnVisual();
            UpdateEndTurnButtonVisibility();
        }

        private void TurnSystem_OnTurnChanged(object sender, EventArgs e) {
            UpdateTurnText();
            UpdateEnemyTurnVisual();
            UpdateEndTurnButtonVisibility();
        }

        private void UpdateTurnText() => turnText.text = $"Turn {TurnSystem.Instance.TurnNumber}";

        private void UpdateEnemyTurnVisual() => enemyTurnVisualGameObject.SetActive(!TurnSystem.Instance.IsPlayerTurn);

        private void UpdateEndTurnButtonVisibility() => nextTurnButton.gameObject.SetActive(TurnSystem.Instance.IsPlayerTurn);
    }
}