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

namespace IStalk.UI
{
    public class ShrineUI : MonoBehaviour
    {
        [SerializeField] private GameObject _panel;
        [SerializeField] private Transform _itemContainer;
        [SerializeField] private GameObject _itemSlotPrefab;
        [SerializeField] private TextMeshProUGUI _playerLootText;
        [SerializeField] private Button _closeBtn;

        private Shrine _currentShrine;
        private PlayerInventory _inventory;

        private void Start()
        {
            _panel.SetActive(false);
            if (_closeBtn != null)
                _closeBtn.onClick.AddListener(Hide);
        }

        public void Show(Shrine shrine, PlayerInventory inventory)
        {
            _currentShrine = shrine;
            _inventory = inventory;
            _panel.SetActive(true);
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
            RefreshItems();
        }

        public void Hide()
        {
            _panel.SetActive(false);
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
            _currentShrine = null;
        }

        public bool IsOpen => _panel.activeSelf;

        private void RefreshItems()
        {
            foreach (Transform child in _itemContainer)
                Destroy(child.gameObject);

            if (_currentShrine == null) return;

            var items = _currentShrine.GetAvailableItems();
            foreach (var item in items)
            {
                if (item == null) continue;
                var slot = Instantiate(_itemSlotPrefab, _itemContainer);
                SetupSlot(slot, item);
            }

            if (_playerLootText != null && _inventory != null)
                _playerLootText.text = $"Your Loot Value: {_inventory.TotalLootValue}";
        }

        private void SetupSlot(GameObject slot, ShrineItemData_SO item)
        {
            var nameText = slot.transform.Find("Name")?.GetComponent<TextMeshProUGUI>();
            var costText = slot.transform.Find("Cost")?.GetComponent<TextMeshProUGUI>();
            var descText = slot.transform.Find("Description")?.GetComponent<TextMeshProUGUI>();
            var icon = slot.transform.Find("Icon")?.GetComponent<Image>();
            var btn = slot.GetComponent<Button>();

            if (nameText != null) nameText.text = item.displayName;
            if (costText != null) costText.text = $"Cost: {item.lootCost}";
            if (descText != null) descText.text = item.description;
            if (icon != null && item.icon != null) icon.sprite = item.icon;

            bool canAfford = _inventory != null && _inventory.TotalLootValue >= item.lootCost;
            if (btn != null)
            {
                btn.interactable = canAfford;
                string itemId = item.itemId;
                btn.onClick.AddListener(() => OnPurchase(itemId));
            }
        }

        private void OnPurchase(string itemId)
        {
            if (_currentShrine == null || _inventory == null) return;
            _currentShrine.TryPurchase(_inventory, itemId);
            RefreshItems();
        }
    }
}
