using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class CollectBtn : MonoBehaviour
{
    public DateTime lastCollected;
    public string buildingId;
    public float productionRate;
    public double collectableAmount;
    public Button btn;
    public TMP_Text txt;

    public CollectablesData.ResourceType resourceType;


    void Start(){
        btn.onClick.AddListener(OnClick);
    }

    void Update()
    {
        collectableAmount=((DateTime.UtcNow - lastCollected).TotalSeconds * productionRate);
        txt.text = ((int)collectableAmount).ToString();

        btn.interactable = collectableAmount > 1;
    }

    public void Set(string id,DateTime _lastCollected, float _productionRate, CollectablesData.ResourceType _resourceType){
        buildingId = id;
        lastCollected = _lastCollected;
        productionRate=_productionRate;
        resourceType = _resourceType;
    }

    async void OnClick(){
        collectableAmount=((DateTime.Now - lastCollected).TotalSeconds * productionRate);
        lastCollected = await DBmanager.GetNetworkTime();
        switch (resourceType){
            case CollectablesData.ResourceType.Metal:
                DBmanager.SetMetal(DBmanager.Metal + (int)collectableAmount);
                break;

            case CollectablesData.ResourceType.Oxygen:
                DBmanager.SetOxygen(DBmanager.Oxygen + (int)collectableAmount);
                break;
        }
        DBmanager.CollectBuilding(buildingId);
    }
}
