using System;
using EasyButtons;
using UnityEngine;

public class BuildingManager : MonoBehaviour
{
    public Building[] buildings;
    [Button]
    void GetAllBuildings(){
        buildings = FindObjectsOfType<Building>();
    }


    void Start()
    {
        DBmanager.OnStateChanged.AddListener(UpdateBuildings);
        UpdateBuildings();
    }


    public void UpdateBuildings(){
        Debug.Log("Im getting called");
        foreach (Building building in buildings){
            bool buildingExists = false;
            foreach (BuildingState buildingState in DBmanager.buildingStates){
                if(buildingState.id == building.buildingData.buildingName){
                    buildingExists  =true;
                    building.curLevel = buildingState.level;
                    building.transform.position = (buildingState.position != Vector3.zero) ? buildingState.position : building.transform.position;
                    building.lastCollected = buildingState.lastCollectedTimestamp;
                    break;
                }
            }
            building.gameObject.SetActive(buildingExists);
        }
    }

    public void OnClickBuyBuilding(BuildingData buildingData){
        DBmanager.AddBuilding(buildingData);
    }
}
