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

public class ShopBuildingButton : MonoBehaviour
{
    public BuildingData buildingData;
    public Button btn;
    public TMP_Text txt;
    void Start()
    {
        DBmanager.OnStateChanged.AddListener(refresh);
        refresh();
        btn.onClick.AddListener(OnClick);
    }

    void OnClick(){
        btn.interactable=false;
    }

    void OnValidate(){
        if(btn==null){btn = GetComponent<Button>();}
        if(txt==null){txt=GetComponentInChildren<TMP_Text>();}
    }

    public void refresh(){
        bool exists = false;
        foreach (BuildingState buildingState in DBmanager.buildingStates){
            if(buildingState.id == buildingData.buildingName){
                exists=true;
                break;
            }
        }
        gameObject.SetActive(!exists);
        btn.interactable = (DBmanager.Coins > buildingData.levels[0].price);
        txt.text = buildingData.levels[0].price.ToString();
    }
}
