using UnityEngine;
using UnityEngine.SceneManagement;

public class MenuManager : MonoBehaviour
{
    private IMenuState currentMenu;

    public UIStartMenu startMenu;
    
    public UIInGameMenu inGameMenu;
    public UITutorial tutorialMenu;
    public UIDayEndMenu dayEndMenu;
    public UIGameEndMenu gameEndMenu;

    public static MenuManager Instance;
    private void Awake()
    {
        if (Instance == null) Instance = this;
        else Destroy(this);

        Events.OnGameEnd += OnGameEnd;
        Events.OnDayEnd += OnDayEnd;
    }

    private void OnDestroy()
    {
        Events.OnGameEnd -= OnGameEnd;
        Events.OnDayEnd -= OnDayEnd;
    }

    private void Start()
    {
        currentMenu = startMenu;
        currentMenu.Enter();
    }

    private void Update() => currentMenu.UpdateMenu();
    
    private void OnGameEnd() => MenuTransition(gameEndMenu);

    private void OnDayEnd() => MenuTransition(dayEndMenu);


    public void MenuTransition(IMenuState nextMenu)
    {
        currentMenu.Exit();
        currentMenu = nextMenu;
        nextMenu.Enter();
    }

    public void RestartScene()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentSceneIndex);
    }

    public void ExitGame()
    {
        Application.Quit();
    }
}
