using Kryz.CharacterStats.Examples;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class VendorSlotUI : ItemSlot
{
    public TMP_Text priceText;
    public Image currencyIcon;
    public bool soldMultipleTimes = false;

    CoinBag coinBag;
    Inventory inventory;
    private VendorData currentVendorData;
    VendorItem vendorItem;

    private void Awake()
    {
        coinBag = FindObjectOfType<CoinBag>();
        inventory = FindObjectOfType<Inventory>();
    }

    public void AddItem(VendorData vendor, VendorItem newItem)
    {
        vendorItem = newItem;
        Item = vendorItem.item;
        currentVendorData = vendor;
        priceText.text = Item.sellPriceVendor.ToString();
        image.sprite = Item.Icon;
        image.enabled = true;
    }

    public void ClearSlot()
    {
        Item = null;
        currentVendorData = null;
        priceText.text = "";
        image.sprite = null;
        image.enabled = false;
    }

    public void Purchase()
    {
        if (Item == null) return;

        if(coinBag.HasEnoughCoin(Item.sellPriceVendor))
        {
            if(inventory.AddItem(Item))
            {
                coinBag.SpendCoin(Item.sellPriceVendor);

                if (vendorItem.soldMultipleTimes) return;

                currentVendorData.items.Remove(vendorItem);
                ClearSlot();
            }
            else
            {
                Debug.Log("Failed to purchase");
            }
        }
    }

    public override void OnPointerEnter(PointerEventData eventData)
    {
        ItemTooltip.Instance.ShowTooltip(Item);
    }
}
