using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class ItemShop : MonoBehaviour
{
    public InventoryItem selectedItem;

    public Image itemImage;
    public TMP_Text itemTxt;
    public TMP_Text levelTxt;

    public TMP_Text statTxt;
    public TMP_Text priceTxt;
    public TMP_Text gemTxt;

    public Button buyBtn;

    void Start(){
        SelectShopItem(selectedItem);
    }

    public void SelectShopItem(InventoryItem selected){
        selectedItem = selected;

        if(selectedItem==null){Debug.LogError("Null shop item selected, Please check this"); return;}

        itemImage.sprite = selectedItem.image;
        itemTxt.text = selectedItem.itemName;
        statTxt.text = selectedItem.stat.ToString();
        priceTxt.text = selectedItem.price.ToString();
        gemTxt.text = selectedItem.gems.ToString();

        buyBtn.interactable = selectedItem.price < DBmanager.Coins && selectedItem.gems < DBmanager.Gems;
    }

    public async void BuySelected(){
        if(selectedItem == null){
            Debug.LogError("Cant buy, No item is selected");
            return;
        }

        buyBtn.interactable=false;
        await DBmanager.SetCoins(DBmanager.Coins - selectedItem.price);   
        DBmanager.SetGems(DBmanager.Gems- selectedItem.gems);
        DBmanager.AddInventoryItem(selectedItem);
        SelectShopItem(selectedItem);
    }
}
