using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour
{
    public enum State
    {
        TitleScreen,
        Credits,
        GamePlay,
    }

    public static GameManager instance = null;
    public bool StartGame = false;

    public Camera mainCamera;
    public GameObject TitleScreen;
    public GameObject CreditScreen;

    [HideInInspector]
    public GamePlayManager GamePlayManager;

    private State _state;

    // Awake is called before any Start functions.
    void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else if (instance != this)
        {
            Destroy(gameObject);
        }

        //Sets this to not be destroyed when reloading scene
        DontDestroyOnLoad(gameObject);
    }

    // Use this for initialization
    void Start()
    {
        GamePlayManager = GetComponent<GamePlayManager>();

        mainCamera.transparencySortMode = TransparencySortMode.Orthographic;

        _state = State.TitleScreen;
    }

    // Update is called once per frame
    void Update()
    {
        if (StartGame)
        {
            StartGame = false;

            SetState(State.GamePlay);
        }

        if (_state == State.TitleScreen)
        {
            if (Input.GetKeyDown(KeyCode.C) || Input.GetKeyDown(KeyCode.JoystickButton6))
            {
                SetState(State.Credits);
            }
            else if (Input.anyKeyDown)
            {
                SetState(State.GamePlay);
            }
        }
        else if (_state == State.Credits)
        {
            if (Input.GetKeyDown(KeyCode.C) || Input.GetKeyDown(KeyCode.JoystickButton6))
            {
                SetState(State.TitleScreen);
            }
            else if (Input.anyKeyDown)
            {
                SetState(State.GamePlay);
            }
        }
    }

    public void SetState(State state)
    {
        _state = state;

        switch (state)
        {
            case State.TitleScreen:
                {
                    CreditScreen.SetActive(false);
                    TitleScreen.SetActive(true);
                }
                break;

            case State.Credits:
                {
                    CreditScreen.SetActive(true);
                    TitleScreen.SetActive(false);
                }
                break;


            case State.GamePlay:
                {
                    CreditScreen.SetActive(false);
                    TitleScreen.SetActive(false);
                    GamePlayManager.InitGame();
                }
                break;
        }
    }
}
