using System;
using UnityEngine;

public class GameManager : Singleton<GameManager>
{
    #region Fields
    [SerializeField] private GameState _currentGameState = GameState.StartMenu;

    #endregion Fields

    #region Properties
    public GameState CurrentState => _currentGameState;
    public bool IsGamePlaying { get { return _currentGameState != GameState.StartMenu && _currentGameState != GameState.EndMenu; } }

    public bool UseTuto = true;
    #endregion Properties

    #region Events
    private event Action<GameState> _gameStateChanged = null;
    public event Action<GameState> GameStateChanged
    {
        add
        {
            _gameStateChanged -= value;
            _gameStateChanged += value;
        }
        remove
        {
            _gameStateChanged -= value;
        }
    }
    #endregion Events

    void Start()
    {
        _currentGameState = GameState.StartMenu;
    }

    void Update()
    {
#if UNITY_EDITOR
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            _currentGameState = (GameState)(((int)_currentGameState + 1) % Enum.GetValues(typeof(GameState)).Length);
            if (_gameStateChanged != null)
            {
                _gameStateChanged(_currentGameState);
            }
        }
#endif // UNITY_EDITOR
    }

    public void LaunchGame()
    {
        if (UseTuto == false)
        {
            GameSceneManager.Instance.WaveManager.LaunchWaves();
        }
        _currentGameState = GameState.TrajectoryMix;
        _gameStateChanged(_currentGameState);
    }

    public void GameOver()
    {
        _currentGameState = GameState.EndMenu;
        _gameStateChanged(_currentGameState);
    }
}
