using UnityEngine;
using UnityEngine.UI;
using TMPro;
using IStalk.Core;
using IStalk.Data;
using IStalk.Eye;

namespace IStalk.UI
{
    public class EyeHUD : MonoBehaviour
    {
        [Header("Dread")]
        [SerializeField] private Slider _dreadBar;
        [SerializeField] private TextMeshProUGUI _dreadText;
        [SerializeField] private Image _dreadBarFill;

        [Header("Laser")]
        [SerializeField] private Image _laserCooldownFill;
        [SerializeField] private TextMeshProUGUI _laserStateText;

        [Header("Match")]
        [SerializeField] private TextMeshProUGUI _matchTimerText;
        [SerializeField] private TextMeshProUGUI _stateText;
        [SerializeField] private TextMeshProUGUI _runnersVisibleText;

        [Header("Traps")]
        [SerializeField] private Transform _trapSlotContainer;

        private DreadEconomy _dread;
        private LoSManager _los;

        public void Initialize(DreadEconomy dread, LoSManager los)
        {
            _dread = dread;
            _los = los;

            _dread.OnDreadChanged += RefreshDread;
            _los.OnVisibilityChanged += RefreshVisibility;

            if (GameManager.Instance != null)
                GameManager.Instance.OnStateChanged += RefreshState;
        }

        private void Update()
        {
            if (GameManager.Instance == null) return;

            float timer = GameManager.Instance.MatchTimer;
            int minutes = Mathf.FloorToInt(timer / 60f);
            int seconds = Mathf.FloorToInt(timer % 60f);
            if (_matchTimerText != null)
                _matchTimerText.text = $"{minutes:00}:{seconds:00}";
        }

        private void RefreshDread(float amount)
        {
            float max = _dread != null ? _dread.MaxDread : 200f;
            if (_dreadBar != null)
                _dreadBar.value = Mathf.Clamp01(amount / max);
            if (_dreadText != null)
                _dreadText.text = $"Dread: {Mathf.FloorToInt(amount)}/{Mathf.FloorToInt(max)}";
        }

        private void RefreshVisibility(System.Collections.Generic.List<int> visibleIds)
        {
            if (_runnersVisibleText != null)
                _runnersVisibleText.text = $"Runners Visible: {visibleIds.Count}";
        }

        private void RefreshState(MatchState state)
        {
            if (_stateText != null)
            {
                _stateText.text = state switch
                {
                    MatchState.GracePeriod => "RUNNERS EXPLORING...",
                    MatchState.Hunt => "HUNT",
                    MatchState.Overtime => "OVERTIME - SEAL ROOMS",
                    MatchState.RunnersWin => "THEY ESCAPED...",
                    MatchState.EyeWins => "VICTORY",
                    _ => ""
                };
            }
        }

        private void OnDestroy()
        {
            if (_dread != null) _dread.OnDreadChanged -= RefreshDread;
            if (_los != null) _los.OnVisibilityChanged -= RefreshVisibility;
            if (GameManager.Instance != null) GameManager.Instance.OnStateChanged -= RefreshState;
        }
    }
}
