using UnityEngine;
using System.Collections;

public class Menu : MonoBehaviour {

	public static string state ="mentry";		/*first state of the game (i have put 3 states - mentry /game /mfinish)
								  - "mentry" has: Start, Rules, Exit
								  - "mfinish" has: currentScore, Again, Menu, Quit
								  - "again" goes to the "game" / menu goes to the "mentry" / and quit */
	
	
	static int nbCalculDone=6;					//nombre de calculs deja fait
	public static int nbColect=0;					//nombre de calculs gagnes
	
	float timeDelay=1060.0f;				//combien de temps on attends
	float timer=1060.0f;		
	
	public Camera cam1;			//entry Camera
	public Camera cam2;							//player Camera
	
	public Texture logo;
	
	public GUISkin font;
	// Use this for initialization
	void Start () {
		cam1.enabled = true;
		cam2.enabled = false;
	}
	
	// Update is called once per frame
	void Update () {
		switch(state)
		{
			case "calculate"	:	UpdateCalculate();	break;		//to update numbers of fish/candy / currentScore
			case "game"			:   UpdateGame();		break;
		}
		
	}
	void OnGUI() //OnGUI is called for rendering and handling GUI events
	{
		switch(state)
		{
			case "game"			:   UpdateGame();		break;
			case "calculate"	:	OnGUICalculate();	break;
			case "mentry"		:	OnGUIMenu();		break;
			case "mfinish"		:	OnGUIScore();		break; //--------------------SCORE smisli-------------
			case "rules"		:	OnGUIRules();		break;
		}
	}
	
	void OnGUIMenu()	//when state is "menu" - user enters -------------------------------------
	{
		GUI.skin = font;
		if(state=="mentry")
		{
			GUI.DrawTexture(new Rect(0,-20.0f,500,480),logo);
		}
		//if user clicks on start, state goes to "game"
		if (GUI.Button(new Rect(620,100,150,80), "Start"))
		{
			state="game";
			cam2.enabled=!cam2.enabled;			//change cameras
			cam1.enabled=!cam1.enabled;
		}
		//if user clicks on rules, state goes into "rules"
		if (GUI.Button(new Rect(620,180,150,80), "Rules"))
		{
			state="rules";
		}
		//if user clicks on the quit, state goes into "quit"
		if (GUI.Button(new Rect(620,260,150,80), "Quit"))
		{
			state="quit";
		}
	
	}
	void UpdateQuit()	//calls this when clicked on button "Quit" ---------------------------------
	{	
		Application.Quit();
	}
	
	void UpdateGame()	//calls this when clicked on button "Start" --------------------------------
	{
		
		timer-=Time.time;
		Debug.Log ("u update game-u si???");	//RADI
		state="calculate";
	}
	
	void OnGUIRules()	//calls this when clicked on the button "Rules" ----------------------------
	{
		GUI.skin = font;
		GUI.Label(new Rect(100,150,300,200),"Collect all the carrots as fast as possible. Get as many candies as you can for the score. Good luck!");
		if (GUI.Button(new Rect(620,130,150,80), "Return"))
		{
			state="mentry";
		}
	}
	
	void OnGUIScore()	//calls this when game is finished -----------------------------------------
	{
		//GUI.Label(new Rect(100,100,100,100),""+"fishes eated, candy collected");
		
		//if user clicks on Again, state goes to "game"
		GUI.skin = font;
		if (GUI.Button(new Rect(100,100,100,100), "Again"))
		{
			nbCalculDone = 0;
			//nbCalculWin = 0;
			state="game";
		}
		//if user clicks on Menu, state goes into "mentry"
		if (GUI.Button(new Rect(200,100,100,100), "Menu"))	//RADI
		{
			state="mentry";
		}
		//if user clicks on the quit, state goes into "quit"
		if (GUI.Button(new Rect(300,100,100,100), "Quit"))
		{
			state="quit";
		}
		
	}
	
	void OnGUICalculate()	//call this to calculate during the game highscore? ----------------------- 
	{
		GUI.skin = font;
		Debug.Log ("ongui calculate");	//RADI
		GUI.Label(new Rect(50,100,300,50),"" + (int)(timer-Time.time+1)+"  s");
		GUI.Label(new Rect(350,100,300,50),"" + "Carrots "+nbColect+" / "+nbCalculDone);
		if(nbColect == nbCalculDone)
			state="mfinish";
		
		
		state ="game";
	}
	
	void UpdateCalculate()	// checks if there is still remaining time --------------------------------
	{
		Debug.Log ("u update calculate-u si???");
		state="game";
		//if(Time.time > timer)
		//	state="mfinish";	//goes to the menu highscore when there is no time left
				
	}
	
	public static string getState()
	{
		return state;
		
	}
	/*public static int getCarrot()
	{
		return nbColect;
	}*/
}
