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

public class MoneyUpdater : MonoBehaviour
{
    public TextMeshProUGUI AddMoneyText = null;

    // Start is called before the first frame update
    void Start()
    {
        nextUpdate = 0.0f;
    }

    // Update is called once per frame
    void Update()
    {
        if (DisplayedAmount != GameState.Money)
        {
            if (Time.time > nextUpdate)
            {
                nextUpdate = Time.time + 0.2f;

                int delta = GameState.Money - DisplayedAmount;
                float change = Mathf.Abs(delta) / 4.0f;
                change = Mathf.CeilToInt(change);

                change *= Mathf.Sign(delta);

                int actualChange = Mathf.RoundToInt(change);

                DisplayedAmount += actualChange;

                GetComponent<TextMeshProUGUI>().text = "$ " + DisplayedAmount.ToString();
            }
        }
    }

    int DisplayedAmount = 0;
    float nextUpdate = 0.0f;

    public void AddMoney(int Amount)
    {
        // StartCoroutine(ToastMoney(Amount));
        GameState.Money += Amount;
    }

    private IEnumerator ToastMoney(int Amount)
    {
        AddMoneyText.text = "+ " + Amount.ToString();

        yield return new WaitForSeconds(2.0f);

        AddMoneyText.text = "";
    }

}
