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

namespace YachtDice.UI
{
    public class DicePanelView : MonoBehaviour
    {
        [Header("Dice Display")]
        [SerializeField] private Button[] diceButtons = new Button[5];
        [SerializeField] private TMP_Text[] diceValueTexts = new TMP_Text[5];
        [SerializeField] private Image[] diceBackgrounds = new Image[5];

        [Header("Roll")]
        [SerializeField] private Button rollButton;
        [SerializeField] private TMP_Text rollButtonText;

        [Header("Colors")]
        [SerializeField] private Color unlockedColor = Color.white;
        [SerializeField] private Color lockedColor = new Color(1f, 0.85f, 0.4f, 1f);

        public event Action<int> OnDiceToggled;
        public event Action OnRollClicked;

        private void Awake()
        {
            for (int i = 0; i < diceButtons.Length; i++)
            {
                int capturedIndex = i;
                diceButtons[i].onClick.AddListener(() => OnDiceToggled?.Invoke(capturedIndex));
            }

            rollButton.onClick.AddListener(() => OnRollClicked?.Invoke());

            for (int i = 0; i < diceValueTexts.Length; i++)
            {
                diceValueTexts[i].text = "?";
                diceBackgrounds[i].color = unlockedColor;
                diceButtons[i].interactable = false;
            }

            SetRollButtonState(true, 0, 3);
        }

        public void SetDieValue(int index, int value)
        {
            if (index >= 0 && index < diceValueTexts.Length)
                diceValueTexts[index].text = value.ToString();
        }

        public void SetAllDiceValues(int[] values)
        {
            for (int i = 0; i < values.Length && i < diceValueTexts.Length; i++)
                diceValueTexts[i].text = values[i].ToString();
        }

        public void SetDieLocked(int index, bool isLocked)
        {
            if (index >= 0 && index < diceBackgrounds.Length)
                diceBackgrounds[index].color = isLocked ? lockedColor : unlockedColor;
        }

        public void SetDiceInteractable(bool interactable)
        {
            for (int i = 0; i < diceButtons.Length; i++)
                diceButtons[i].interactable = interactable;
        }

        public void SetRollButtonState(bool interactable, int currentRoll, int maxRolls)
        {
            rollButton.interactable = interactable;

            if (currentRoll >= maxRolls)
                rollButtonText.text = "Выберите категорию";
            else
                rollButtonText.text = $"Бросок {currentRoll + 1}/{maxRolls}";
        }

        public void ResetForNewTurn()
        {
            for (int i = 0; i < diceValueTexts.Length; i++)
            {
                diceValueTexts[i].text = "?";
                diceBackgrounds[i].color = unlockedColor;
                diceButtons[i].interactable = false;
            }
        }

        public void ResetForNewGame()
        {
            ResetForNewTurn();
            SetRollButtonState(true, 0, 3);
        }

        private void OnDestroy()
        {
            for (int i = 0; i < diceButtons.Length; i++)
                diceButtons[i].onClick.RemoveAllListeners();

            rollButton.onClick.RemoveAllListeners();
        }
    }
}
