using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ItemSlotUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    // Kind of confusing here... ItemSlot refers to the class that is the DATA for a
    // "slot". It is used for serialization and keeping track of the inventory in data.
    // ItemSlot simply has an item ID and a quantity. ItemSlotUI is the visual represenation of this.
    private ItemSlot slotData; 
    private Image image;
    private TextMeshProUGUI quantityText;

    private Item itemObj;

    // (I hate this approach) passed in from InventorySlotUI
    private ItemNameUI itemNameUI;
    private ItemDescriptionUI itemDescriptionUI;

    private void Start()
    {
        image = GetComponentsInChildren<Image>()[1]; //ignore parent
        quantityText = GetComponentInChildren<TextMeshProUGUI>();
    }

    public void LoadImage(ItemNameUI itemNameUI, ItemDescriptionUI itemDescriptionUI)
    {
        if (this.itemNameUI == null) this.itemNameUI = itemNameUI;
        if (this.itemDescriptionUI == null) this.itemDescriptionUI = itemDescriptionUI;

        itemObj = new Item(slotData.item);
        image.sprite = Resources.Load<Sprite>(itemObj.itemImagePath);
    }

    public void SetItem(ItemSlot itemSlot)
    {
        this.slotData = itemSlot;
        this.quantityText.text = slotData.quantity.ToString();
    }

    public void OnPointerEnter(PointerEventData pointerEventData)
    {
        if (itemObj != null && itemObj.id != ItemIdEnum.NONE)
        {
            itemNameUI.nameText = itemObj.itemName;
            itemDescriptionUI.descriptionText = itemObj.description;
        }
    }

    public void OnPointerExit(PointerEventData pointerEventData)
    {
        itemNameUI.nameText = "";
        itemDescriptionUI.descriptionText = "";
    }
}
