using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using CorgiLabs.Utility;
using UnityEngine.SceneManagement;

public class UIManager : MonoBehaviour
{
    [SerializeField] private TextMeshProUGUI m_LevelText;
    [SerializeField] private TextMeshProUGUI m_FailsText;
    [SerializeField] private Animator m_LevelUpAnimator;

    [SerializeField] private GameObject m_GameOverPanel;

    [SerializeField] private UnityEngine.UI.Button m_TryAgainButton;

    private void OnEnable()
    {
        EventManager.StartListening<int>(EventID.LevelChanged, SetLevelText);
        EventManager.StartListening<int, int>(EventID.FailsChanged, SetFailsText);
        EventManager.StartListening(EventID.LevelUp, LevelUpPopup);
        EventManager.StartListening(EventID.GameOver, ShowGameOverPanel);
    }

    private void SetLevelText(int lvl)
    {
        m_LevelText.text = lvl.ToString();
    }

    private void SetFailsText(int fails, int maxFails)
    {
        m_FailsText.text = fails + " / " + maxFails;
    }

    private void LevelUpPopup()
    {
        m_LevelUpAnimator.SetTrigger("Popup");
    }

    private void ShowGameOverPanel()
    {
        m_GameOverPanel.SetActive(true);
        m_TryAgainButton.Select();
    }

    public void ReloadScene()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    public void LoadMainMenu()
    {
        SceneManager.LoadScene("Main Menu Temporary");
    }
}
