using UnityEngine;
using System.Collections;

public class game : MonoBehaviour {
	
	public float timeLimit;
	public float timer;
	public string etat = "menu";
	
	public Texture2D menu;
	public Texture2D menuL;
	public Texture2D menuW;
	public Texture2D boutonS;
	public Texture2D boutonC;
	public Texture2D boutonQ;
	public Texture2D boutonR;
	public Texture2D boutonN;
	public Texture2D boutonP;
	
	public int score;
	public int oldSocre; 
	//store the score of 1est level if the 2end level start
	GameObject fire;
	int requiredMark;
	float badPercent;
	
	public GameObject boulet;//model of candy
	public GameObject boulet1;//model of candy
	
	
	public GameObject badBoulet;//model of bomb
	public monster monster;//character
	int level;
	float xx,yy,zz;
	float vitesse;
	string move;//direction of moving
	
 	public GUISkin gameSkin;
	// Use this for initialization
	void Start () {
	 
		badPercent = 0.0f;
	  //  monster.renderer.material.color = new Color(0.5f,0.2f,0.5f,1.0f);
	
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	
	void OnGUI()
	{
		//if(gameSkin!=null)
			GUI.skin = gameSkin;
	
			switch(etat)
		{	
			case "menu": onMenu();break;
			case "init": onInit();break;
		    case "game": onGame();break;
		    case "pause": onPause();break;
		    case "win": onWin();break;
		    case "loose": onLoose();break;
		    case "quit": onQuit(); break;
		    case "next": onNext();break; //next level
	     }		
	}
	
	//initial the game
	void onInit()
	{
		 level = 1;
		 boulet.rigidbody.drag = 2;
		 badBoulet.rigidbody.drag = 2;
		 monster.mark = 0;
		 score = 0;
		 oldSocre = 0;
		 move = "stop"; 
		 xx=yy=zz=0.0f;
		 vitesse = 6.0f;
		 timer = Time.time;
		 timeLimit = 30.0f;
		 requiredMark = 5;
		 etat = "game";
		
	}
	
	void onNext()
	{
		level = 2;
		requiredMark = 5;
		timer = Time.time;
		timeLimit = 30.0f;
		monster.mark = 0;
		oldSocre = score;
		score = 0;
		
		badPercent = 0.2f;
		boulet.rigidbody.drag = 1;
		boulet1.rigidbody.drag = 1;
		
		badBoulet.rigidbody.drag = 1;
		//change the speed of falling candy
		etat = "game";
		monster.loose = false; 
	}
	
	void onQuit()
	{
		GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),menu);
		
		
		GUI.TextArea(new Rect(Screen.width/5,Screen.height/7*4,Screen.width/5*3,Screen.height/10),"YOUR TOTAL SCORE IS: " + (score+oldSocre));
		
	}
	
	//main function of the game
	void onGame()
	{
		onKey();
	    if(Time.time%1.0f>0.99f)
		{
		 onBonus();
		}
		//GUI.skin = null;
		if(GUI.Button(new Rect(50,100,200,100),boutonP))
		{
			//GUI.skin = gameSkin;
			timeLimit -= Time.time - timer; 
			//stop counting time since click pause
			etat = "pause";
			
		}
		 score = monster.mark;
		
		GUI.TextArea(new Rect(50,300,100,25)," SCORE:"+score);
		float timeR = timeLimit+timer-Time.time;
		GUI.TextArea(new Rect(50,400,200,25)," Time remaining: "+(int)timeR+" s");
		int numRest = requiredMark -score;
		if(numRest < 0)
		GUI.TextArea(new Rect(50,500,200,25)," You need to eat: 0 candy");
		else
		GUI.TextArea(new Rect(50,500,200,25)," You need to eat: "+ numRest+ " candy");	
		
		if(timer+timeLimit<Time.time)
		{
			//GUI.skin = gameSkin;
			if(score<requiredMark)
				etat = "loose"; 
			//over time && not get enough candies
			else
				etat = "win";
			
		}
		if(monster.loose) //have touched the bomb
		{
			//GUI.skin = gameSkin;
			etat = "loose";
		}
	}
	
	//stop for a while
	void onPause()
	{
		
		GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),menu);
		if(GUI.Button(new Rect(Screen.width/3,Screen.height/7*4,Screen.width/3,Screen.height/10),boutonC))
		{
			timer = Time.time;
			etat = "game";
			
		}
		if(GUI.Button(new Rect(Screen.width/3,Screen.height/7*5,Screen.width/3,Screen.height/10),boutonQ))
		{
			etat = "quit";
		}
		
	}

	
	void onWin()
	{
		GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),menuW);
		//GUI.TextArea(new Rect(100,100,300,50),"CONGRATULATIONS! YOU WIN THE FIRST LEVEL");
		if(GUI.Button(new Rect(Screen.width/3,Screen.height/9*5,Screen.width/3,Screen.height/10),boutonR))
		{
			etat = "init";	
		}
		if(level == 1)
		{
			if(GUI.Button(new Rect(Screen.width/3  ,Screen.height/9*6,Screen.width/3,Screen.height/10),boutonN))
			{
				etat = "next";
			}
			
		}
		/*if(level == 2)
		{
			if(GUI.Button(new Rect(Screen.width/3,Screen.height/9*6,Screen.width/3,Screen.height/10),boutonR))
			{
				etat = "next";
			}
			
			
		}*/
		if(GUI.Button(new Rect(Screen.width/3 ,Screen.height/9*7,Screen.width/3,Screen.height/10),boutonQ))
		{
			etat = "quit";
		}	
	}
	
	void onLoose()
	{
		GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),menuL);
		//GUI.TextArea(new Rect(100,100,300,50),"WHAT A PITY! YOU LOOSE THE LEVEL");
		if(level == 1)
		{
		if(GUI.Button(new Rect(Screen.width/3,Screen.height/7*4,Screen.width/3,Screen.height/10),boutonR))
		{
			etat = "init";	
		}
		}
		if(level == 2)
		{
			if(GUI.Button(new Rect(Screen.width/3,Screen.height/7*4,Screen.width/3,Screen.height/10),boutonR))
			{
				etat = "next";
			}
			
		}
		if(GUI.Button(new Rect(Screen.width/3 ,Screen.height/7*5,Screen.width/3,Screen.height/10),boutonQ))
		{
			etat = "quit";
		}	
	}
	
	
	//first menu of start
	void onMenu()
	{
		GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),menu);
		if(GUI.Button(new Rect(Screen.width/3,Screen.height/7*4,Screen.width/3,Screen.height/10),boutonS))
		{
			etat = "init";
			
		}
		if(GUI.Button(new Rect(Screen.width/3 ,Screen.height/7*5,Screen.width/3,Screen.height/10),boutonQ))
		{
			etat = "quit";
		}
	}
	
	//record the state of keyboard
	//decide the direction of moving
	void onKey()
	{
		if(Input.GetKeyDown(KeyCode.UpArrow))
	       move = "up";
		if(Input.GetKeyDown(KeyCode.DownArrow))
		   move = "down";
		if(Input.GetKeyDown(KeyCode.RightArrow))
		   move = "right";
		if(Input.GetKeyDown(KeyCode.LeftArrow))
		   move = "left";
		
		if(((Input.GetKeyUp(KeyCode.UpArrow))&&move == "up")
			||((Input.GetKeyUp(KeyCode.DownArrow))&&move == "down")
			||((Input.GetKeyUp(KeyCode.RightArrow))&&move == "right")
			||((Input.GetKeyUp(KeyCode.LeftArrow))&&move == "left"))
			move = "stop";
	
		if((monster.transform.position.x>4&&move == "right")
			||(monster.transform.position.x<-4&&move == "left")
			||(monster.transform.position.z>4&&move == "up")
			||(monster.transform.position.z<-4&&move == "down"))
			move = "stop"; // limit the character in the room
		onMove();
	}
	
	//move the character
	void onMove()
	{
		switch(move){
		case "up":
			   monster.transform.Translate
				(Vector3.down*vitesse*Time.deltaTime);break;
		
		case "down":
	
			   monster.transform.Translate
				(Vector3.up*vitesse*Time.deltaTime);break;
		
		
	    case "right":
			   monster.transform.Translate
				(Vector3.right*vitesse*Time.deltaTime);break;
		
	
	    case "left":
			   monster.transform.Translate
				(Vector3.left*vitesse*Time.deltaTime);break;
		
		case "stop":
			break;
		}
	}
	
	//creat the candies and also the bombs
	void onBonus()
	{
		float randomBonbon  = Random.Range(0,2.0f);
		xx = Random.Range(-4.5f,4.5f);
		zz = Random.Range(-4.5f,4.5f);
		yy = 8.0f;
		if(Random.value<1.0f - badPercent)
		{
			//colorful candy
			if(randomBonbon<1.0f)
				fire = (GameObject)Instantiate
			(boulet,new Vector3(xx,yy,zz),transform.rotation);
			else 
				fire = (GameObject)Instantiate
			(boulet1,new Vector3(xx,yy,zz),transform.rotation);
			
	
		}
		else
		{
		fire = (GameObject)Instantiate
			(badBoulet,new Vector3(xx,yy,zz),transform.rotation);
		}
	}
	
	
}
