using UnityEngine;
using System.Collections;

public enum GameStates {FPS, Overview, Paused };

public class GameManager : MonoBehaviour {

	CameraManager CameraMan;

	public GameStates _CurGameState;
	public GameStates CurGameState{
		get{
			return _CurGameState;
		}
		set{
			_CurGameState = value;
		}
	}

	GameStates _PrevGameState;
	public GameStates PrevGameState{
		get{
			return _PrevGameState;
		}
		protected set{
			_PrevGameState = value;
		}
	}

	float ftLastTimePushed = 0;
	public float ftTimeBetweenPush = 0.5f;

	float ftLastTimePausePushed = 0;

	bool _GameStateJustChanged = false;

	public bool GameStateJustChanged{
		get{
			return _GameStateJustChanged;
		}
		protected set{
			_GameStateJustChanged = value;
		}
	}

	bool FirstFPSSwitch = true;
	bool Finished = false;
	float StartingTime = 0;
	float PauseStartTime = 0;
	float FinalTime;
	// Use this for initialization
	void Start () {
		CameraMan = gameObject.GetComponent<CameraManager> ();
		//Switch to Over view
		CameraMan.SwitchToOverview ();
		ChangeGameState (GameStates.Overview);

	}
	
	// Update is called once per frame
	void Update () {
		if (InputWrapper.GetKey (MovementKeys.CameraSwitch) && (Time.time - ftLastTimePushed) > ftTimeBetweenPush &&
		    CurGameState != GameStates.Paused) {
			if(CameraMan.GetCameraState() == CameraState.FPS){
				CameraMan.SwitchToOverview();
				ChangeGameState(GameStates.Overview);
			}
			else{
				CameraMan.SwitchToFPS();
				ChangeGameState(GameStates.FPS);
				/*
				if(FirstFPSSwitch){
					FirstFPSSwitch = false;
					StartingTime = Time.time;
				}*/
			}
			ftLastTimePushed = Time.time;
		}

		if (InputWrapper.GetKey (MovementKeys.Pause) && (Time.time - ftLastTimePausePushed ) > ftTimeBetweenPush
		    && CurGameState != GameStates.Overview) {
			if(CurGameState == GameStates.Paused){
				if(StartingTime != 0)
					StartingTime += (Time.time - PauseStartTime);
				ChangeGameState(PrevGameState);
			}else{

				PauseStartTime = Time.time;
				ChangeGameState(GameStates.Paused);
			}
			ftLastTimePausePushed = Time.time;
		}
	}

	void OnGUI(){
		if (CurGameState == GameStates.Overview) {
			GUI.Label (new Rect (10, Screen.height - 25, 130, 20), "Press Enter to Maze");
		} else if (CurGameState == GameStates.FPS) {
			GUI.Label (new Rect (10, Screen.height - 25, 130, 20), "Press Enter for Map");
		} else if (CurGameState == GameStates.Paused) {
			GUI.Label (new Rect (Screen.width / 2, 10, 100, 20), "Paused");
		}
		if (StartingTime != 0 && CurGameState != GameStates.Paused) {
						if (!Finished)
								GUI.Label (new Rect (10, 10, 100, 20), (Time.time - StartingTime).ToString ("0.00"));
						else

								GUI.Label (new Rect (10, 10, 100, 20), "Final Time " + FinalTime.ToString ("0.00"));
				}

	}

	protected void ChangeGameState(GameStates newState){
		PrevGameState = CurGameState;
		CurGameState = newState;
		GameStateJustChanged = true;
	}

	private void ResetFlags(){
		GameStateJustChanged = false;
	}

	public void StartTimer(){
		if(StartingTime == 0)
			StartingTime = Time.time;
	}

	public void StopTimer(){
		if (!Finished) {
			Finished = true;
			FinalTime = Time.time - StartingTime;
		}
	}
}
