using UnityEngine;
using System.Collections;

public enum GameState
{
    title_screen,
    how_to_play,
    playing,
    waiting_for_game_over,
    between_floors,
    frame_after_between_floors,
    game_over,
}

public class GameManager : MonoBehaviour 
{
    public GameState currentGameState;

    GUIManager myGUI;
    InputManager theInput;
    LevelMaker theLevelMaker;

    GameObject playerObject;
    PlayerMovement playerScript;

    private bool bIAmTheOGManager;
    private bool bInit = false;

    public bool bGameOverFromPlayerDeath;
    public bool bGameOverFromPlayerEthics;

    public float fGameOverDelayTimer = 0.0f;

    public int FinalScore;

    public static int iFloorNumber;

    public void Awake()
    {
        DontDestroyOnLoad(this);

        if (FindObjectsOfType(GetType()).Length > 1 && !bIAmTheOGManager)
        {
            Debug.Log("I must die");
            Destroy(gameObject);
        }

        bIAmTheOGManager = true;
    }

	// Use this for initialization
	void Start ()
	{
	    Init();
	}

    void Init()
    {
        myGUI = GameObject.Find("GUIManager").GetComponent<GUIManager>();
        theInput = GameObject.Find("InputManager").GetComponent<InputManager>();
        theLevelMaker = GameObject.Find("LevelMaker").GetComponent<LevelMaker>();
        DontDestroyOnLoad(gameObject);
        bInit = true;

        if( theInput == null )
            Debug.Log("Init called with no theInput!");
    }

	void OnGUI()
    {
        switch(currentGameState)
        {
            case GameState.title_screen:
                myGUI.UpdateTitleScreenGUI();
                break;
            case GameState.game_over:
                myGUI.UpdateGameOverGUI();
                break;
            case GameState.how_to_play:
                myGUI.UpdateHowToPlayGUI();
                break;


        }
    }

    public void AdvanceToNextFloor()
    {
        currentGameState = GameState.between_floors;
        iFloorNumber++;
        fGameOverDelayTimer = 2.0f;
    }

    void RemakeMapForNextFloor()
    {
        Application.LoadLevel(Application.loadedLevelName);
        currentGameState = GameState.frame_after_between_floors;
        fGameOverDelayTimer = 0.5f;
    }

    protected virtual bool GetPlayerScript()
    {
        GameObject go = GameObject.Find("PlayerMans");
        if (go != null)
        {
            playerScript = go.GetComponent<PlayerMovement>();
        }

        return playerScript != null;
    }

    // Update is called once per frame
	void Update ()
	{
	    if (!bInit)
	        Init();

	    if (fGameOverDelayTimer > 0.0f)
	        fGameOverDelayTimer -= Time.smoothDeltaTime;

        GetPlayerScript();

        switch (currentGameState)
        {
            case GameState.title_screen:
                UpdateTitleScreen();
                break;
            case GameState.how_to_play:
                UpdateHowToPlay();
                break;
            case GameState.game_over:
                UpdateGameOver();
                break;
            case GameState.waiting_for_game_over:
                if (fGameOverDelayTimer <= 0.0f)
                    GameOver();
                break;
            case GameState.between_floors:
                if (fGameOverDelayTimer <= 0.0f)
                    RemakeMapForNextFloor();
                break;
            case GameState.frame_after_between_floors:
                if (fGameOverDelayTimer <= 0.0f)
                {
                    bInit = false;
                    Init();
                    currentGameState = GameState.playing;
                    playerObject = theLevelMaker.MakeLevel(iFloorNumber, playerObject);
                    GetPlayerScript();
                    playerScript.bCalledInit = false;
                }
                break;
        }

        //if (theInput.InputJustPressedThisFrame(InputTypes.y) && playerScript != null)
        //{
        //    GameOver();
        //    bGameOverFromPlayerDeath = true;
        //}
	}

    void UpdateTitleScreen()
    {
        if( theInput.InputJustPressedThisFrame(InputTypes.a) || theInput.InputJustPressedThisFrame(InputTypes.x))
        {
            currentGameState = GameState.how_to_play;
            fGameOverDelayTimer = 1.0f;
        }
    }

    void UpdateHowToPlay()
    {
        if ((theInput.InputJustPressedThisFrame(InputTypes.a) || theInput.InputJustPressedThisFrame(InputTypes.x)) && fGameOverDelayTimer <= 0.0f)
        {
            currentGameState = GameState.playing;
            playerObject = theLevelMaker.MakeLevel(iFloorNumber, playerObject);
        }
    }

    void UpdateGameOver()
    {
        if ((theInput.InputJustPressedThisFrame(InputTypes.a) || theInput.InputJustPressedThisFrame(InputTypes.x)) && fGameOverDelayTimer <= 0.0f)
        {
            currentGameState = GameState.title_screen;
            bGameOverFromPlayerDeath = false;
            bGameOverFromPlayerEthics = false;



            //playerScript.bCalledInit = false;
            //playerScript.gameObject.SetActive(false);
            Application.LoadLevel(Application.loadedLevelName);
            bInit = false;
        }
    }


    public void GameOver()
    {
        FinalScore = playerScript.iScore;
        iFloorNumber = 0;
        Destroy( playerScript.gameObject);
        currentGameState = GameState.game_over;
        fGameOverDelayTimer = 3.0f;
    }
}
