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

public class UIManagerGame : Manager<UIManagerGame>
{
    public Button FuelIndicator;

    public float Fuel = 1.0f;
    public int CountdownSeconds = 0;
    public float Score = 0.0f;

    private RectTransform fuelIndicatorRect;
    private Vector2 initialFuelIndicatorSize;

    [SerializeField]
    private TextMeshProUGUI countdownText;

    [SerializeField]
    private TextMeshProUGUI scoreText;

    private CanvasRenderer canvasRenderer;

    public void OnGameStateChanged(GameState newState, GameState oldState)
    {
        if(newState == GameState.Play)
        {
            this.gameObject.SetActive(true);
        }
        else
        {
            this.gameObject.SetActive(false);
        }
    }

    protected void Start()
    {
        GameStateManager.Instance.OnGameStateChangedEvent.AddListener(OnGameStateChanged);

        canvasRenderer = GetComponent<CanvasRenderer>();
        canvasRenderer.SetAlpha(1.0f);
        fuelIndicatorRect = (RectTransform)FuelIndicator.transform;
        initialFuelIndicatorSize = fuelIndicatorRect.sizeDelta;
    }

    protected void Update()
    {
        fuelIndicatorRect.sizeDelta = new Vector2(Fuel * initialFuelIndicatorSize.x, initialFuelIndicatorSize.y);
        int countdownSeconds = CountdownSeconds%60;
        int countdownMinutes = CountdownSeconds/60;
        countdownText.text = (countdownMinutes < 10 ? "0" : "") + countdownMinutes + ":" + (countdownSeconds < 10 ? "0" : "") + countdownSeconds;
        scoreText.text = Score + "m";
    }

    protected void OnDestroy()
    {
        GameStateManager.Instance.OnGameStateChangedEvent.RemoveListener(OnGameStateChanged);
    }
}
