using UnityEngine;
using System.Collections;

public class GameStateController : SingletonBehaviour<GameStateController>
{
    private bool _tutorialActive = true;

    // Use this for initialization
    void Start()
    {
        foreach (var name in Input.GetJoystickNames())
        {
            Debug.Log(name);
        }

    }

    public event GameStateHandler GameStateChanged;

    public delegate void GameStateHandler(EGameState oldState, EGameState newState);

    // Update is called once per frame
    void Update()
    {

        switch (GameState)
        {
            case EGameState.Playing:
            case EGameState.Tutoring_Summoning:
                if (Input.GetButtonDown("OpenSpawner") || Input.GetButtonDown("SpawnerToggle"))
                {
                    GameState = EGameState.Summoming;
                }
                break;
            case EGameState.Summoming:
                if (Input.GetButtonUp("OpenSpawner") || Input.GetButtonDown("SpawnerToggle"))
                {
                    if (!_tutorialActive)
                    {
                        GameState = EGameState.Playing;
                    }
                    else
                    {
                        GameState = EGameState.Tutoring_Summoning;
                    }
                }
                break;
        }
    }

    private EGameState _gameState;

    public EGameState GameState
    {
        get
        {
            return _gameState;
        }

        set
        {
            if (value == _gameState)
                return;

            var oldState = _gameState;

            _gameState = value;

            switch (_gameState)
            {
                case EGameState.Summoming:
                    Time.timeScale = 0.05f;
                    break;
                default:
                    Time.timeScale = 1.0f;
                    break;
            }

            Debug.Log(string.Format("GameStateChanged: {0} -> {1} ", oldState, _gameState));

            if (GameStateChanged != null)
            {
                GameStateChanged(oldState, _gameState);
            }

        }
    }

    public bool AcceptPlayerInput
    {
        get
        {
            return GameState == EGameState.Playing || _gameState == EGameState.TutoringGlyphs || _gameState == EGameState.Tutoring_Summoning || _gameState == EGameState.Tutoring_SummoningFinished || _gameState == EGameState.Tutoring_Byebye;
        }
    }

    public void SetTutoringGlyphs()
    {
        GameState = EGameState.TutoringGlyphs;
    }

    public void SetTutoringSummon()
    {
        GameState = EGameState.Tutoring_Summoning;
    }

    public void SetTutoringFinished()
    {
        _tutorialActive = false;
        GameState = EGameState.Tutoring_Byebye;
    }

    public void TutorialDemonSummoned()
    {
        if (GameState == EGameState.Tutoring_Summoning)
        {
            GameState = EGameState.Tutoring_SummoningFinished;
        }
    }

}

public enum EGameState
{
    Init,
    Tutoring_Welcome,
    TutoringGlyphs,
    Tutoring_Summoning,
    Tutoring_SummoningFinished,
    Tutoring_Byebye,
    Playing,
    Summoming,
    GameOver,
}
