using System;
using Abilities;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public enum DiceColor
{
    Black,
    White,
}

public class Die : MonoBehaviour
{
    public DiceColor color;
    private int _result = 0;
    [SerializeField] private Button dieButton; // assign in the editor
    private Color originalColor;
    private bool _isClickable = false;
    private bool _selected = false;
    private bool _blackDieAbilityUsed = false;
    public event Action<Die> DieSelected;
    public event Action<Die> DieUnselected;

    private void OnEnable() {
        GameManager.StateChanged += HandleStateChange;
        StartingBlackDieAbility.AbilitySelected += HandleOnBlackDieAbilitySelected;
    }

    private void OnDisable() {
        GameManager.StateChanged -= HandleStateChange;
        StartingBlackDieAbility.AbilitySelected -= HandleOnBlackDieAbilitySelected;
    }

    private void Start() {
        dieButton.onClick.AddListener(DiePressed);
        originalColor = gameObject.GetComponent<Image>().color;
    }

    public void SetResult(int result) {
        _result = result;
        gameObject.GetComponentInChildren<TextMeshProUGUI>().text = _result.ToString();
    }

    public int GetResult()
    {
        return _result;
    }

    public void ResetDie() {
        _selected = false;
        _blackDieAbilityUsed = false;
        gameObject.GetComponent<Image>().color = originalColor;
        gameObject.GetComponent<Outline>().enabled = false;
    }

    public void DieBeingUsed(bool isPairComplete)
    {
        if (isPairComplete)
        {
            // Signify that both dice have been selected
            DieSelectedAndPairComplete();
        }
        else
        {
            // Signify that the die is selected but the pair is not complete
            DieSelectedButPairNotComplete();
        }
    }

    private void DiePressed()
    {
        if (!_isClickable || (color == DiceColor.Black && !_blackDieAbilityUsed)) return;

        if (_selected)
        {
            DieUnselected?.Invoke(this);
        }
        else
        {
            DieSelected?.Invoke(this);
        }
    }

    private void DieSelectedButPairNotComplete()
    {
        _selected = true;
        gameObject.GetComponent<Outline>().enabled = true;
    }

    private void DieSelectedAndPairComplete() {
        _selected = true;
        gameObject.GetComponent<Outline>().enabled = false;
        gameObject.GetComponent<Image>().color =
            ColorHelper.AddColorTint(gameObject.GetComponent<Image>().color, ColorHelper.OkayGreen, 0.5f);
    }

    private void HandleStateChange(GameState state) {
        switch (state)
        {
            case GameState.PickDiceOne:
            case GameState.PickDiceTwo:
            case GameState.PickRoomOne:
            case GameState.PickRoomTwo:
                _isClickable = true;
                break;
            case GameState.RollDice:
                _isClickable = false;
                break;
            default:
                _isClickable = false;
                break;
        }
    }

    private void HandleOnBlackDieAbilitySelected() {
        _blackDieAbilityUsed = true;
    }
}
