using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    [SerializeField]
    private GameState _state = GameState.INTRO;

    void OnEnable()
    {
        EventDispatcher.Instance.AddListener<GameOverEvent>(OnGameOver);
    }

    void OnDisable()
    {
        EventDispatcher.Instance.RemoveListener<GameOverEvent>(OnGameOver);
    }
    
    void Start()
    {
        ChangeState(_state);
    }

    private void OnGameOver(GameOverEvent e)
    {
        StartCoroutine(RestartGame());
    }

    private IEnumerator RestartGame()
    {
        yield return new WaitForSeconds(3);
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    private void ChangeState(GameState newState)
    {
        switch (newState)
        {
            case GameState.INTRO:
                break;
            case GameState.GAMEPLAY:
                break;
            case GameState.GAME_OVER:
                break;
        }

        _state = newState;

        EventDispatcher.Instance.Dispatch(new GameStateChangeEvent(gameObject, newState));
    }
}

public enum GameState
{
    INTRO,
    GAMEPLAY,
    GAME_OVER
}