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

namespace YachtDice.UI
{
    public class GameInfoView : MonoBehaviour
    {
        [Header("Turn Info")]
        [SerializeField] private TMP_Text turnText;

        [Header("Currency")]
        [SerializeField] private TMP_Text currencyText;

        [Header("Navigation")]
        [SerializeField] private Button shopButton;
        [SerializeField] private Button inventoryButton;

        [Header("Game Over Overlay")]
        [SerializeField] private GameObject gameOverPanel;
        [SerializeField] private TMP_Text finalScoreText;
        [SerializeField] private Button newGameButton;

        public event Action OnNewGameClicked;
        public event Action OnShopClicked;
        public event Action OnInventoryClicked;

        private void Awake()
        {
            newGameButton.onClick.AddListener(() => OnNewGameClicked?.Invoke());
            gameOverPanel.SetActive(false);

            if (shopButton != null)
                shopButton.onClick.AddListener(() => OnShopClicked?.Invoke());
            if (inventoryButton != null)
                inventoryButton.onClick.AddListener(() => OnInventoryClicked?.Invoke());
        }

        public void SetTurnText(int turn, int maxTurns)
        {
            turnText.text = $"Ход {turn} / {maxTurns}";
        }

        public void SetCurrencyText(int amount)
        {
            if (currencyText != null)
                currencyText.text = amount.ToString();
        }

        public void ShowGameOver(int finalScore)
        {
            gameOverPanel.SetActive(true);
            finalScoreText.text = $"Итого: {finalScore}";
        }

        public void HideGameOver()
        {
            gameOverPanel.SetActive(false);
        }

        private void OnDestroy()
        {
            newGameButton.onClick.RemoveAllListeners();

            if (shopButton != null)
                shopButton.onClick.RemoveAllListeners();
            if (inventoryButton != null)
                inventoryButton.onClick.RemoveAllListeners();
        }
    }
}
