using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class ShopManagerScript : MonoBehaviour
{
    public int Cheese;
    public TMP_Text CheeseUI;
    public ShopItemOP[] shopItemsOP;
    public GameObject[] shopPanels;
    public Button[] MyPurchaseBTNS;

    void Start()
    {
        for (int i = 0; i < shopItemsOP.Length; i++)
        {
            shopPanels[i].SetActive(true);
        }
        CheeseUI.text = "Cheese: " + Cheese.ToString();
        LoadPanels();
        CheckPurchaseable();
    }

   
    void Update()
    {
        for (int i = 0; i < shopItemsOP.Length; i++)
        {
            if(shopPanels[i].GetComponent<ShopTemplate>().down)
            {
                shopPanels[i].GetComponent<ShopTemplate>().down = false;
                PurchaseItem(i);
            }   
        }
    }

    public void AddCheese()
    {
        Cheese++;
        CheeseUI.text = "Cheese: " + Cheese.ToString();
        CheckPurchaseable();
    }

    public void CheckPurchaseable()
    {
        for (int i = 0; i < shopItemsOP.Length; i++)
        {
            if (Cheese >= shopItemsOP[i].BaseCost)
            {
                MyPurchaseBTNS[i].interactable = true;
            }
            else
            {
                MyPurchaseBTNS[i].interactable = false;
            }
        }
    }

    public void PurchaseItem(int BtnNO)
    {
        if (Cheese >= shopItemsOP[BtnNO].BaseCost)
        {
            Cheese = Cheese - shopItemsOP[BtnNO].BaseCost;
            CheeseUI.text = "Cheese : " + Cheese.ToString();
            CheckPurchaseable();
        }
    }

    public void LoadPanels()
    {
        for (int i = 0; i < shopItemsOP.Length; i++)
        {
            shopPanels[i].GetComponent<ShopTemplate>().titleTxt.text = shopItemsOP[i].Title;
            shopPanels[i].GetComponent<ShopTemplate>().explanationTxt.text = shopItemsOP[i].Explanation;
            shopPanels[i].GetComponent<ShopTemplate>().prizeTxt.text = "Cheese: " + shopItemsOP[i].BaseCost.ToString();
        }
    }
}
