using UnityEngine;
using System.Collections;

// a faire : faire clignoter le message recharger 

public class Game : MonoBehaviour {
	
	string state = "menu"; 					//Etat en cours
	
	int lvl;								//niveau sélectionné
	
	public static int hitDetected;			//nombre de cibles atteintes
	int hitToReach ;						//nombre de cibles à atteindre
	
	float timerDelay;						//Temps de jeux
	float timerDelayLvl;					//Temps de dans le niveau
	float timer;							//Temps limite de jeux à fixer
	
	float startTime;						//Début du jeu
	float playTime;							//Temps utilisé pour finir
	float[] record = new float[2] {31.0f,31.0f}; 	//liste des records
	
	public static int snowBallLvl;			//Nombre de boules de neiges par chargeur dans le niveau
	public static int snowBallMagazine;		//Chargeur de boules de neige
	
	public static Texture2D ammoZone;		//affichage de la zone de rechargement*
	public Texture2D reloadImg;				//image rechargement demandé
	
	public Texture2D btn;					//Fond des boutons
	public Texture2D titre;					//image titre
		
	ArrayList listOfInstanciedObjects = new ArrayList(); //liste des cibles
	
	public GameObject[] posBdN;				//position des BdN
	public GameObject[] posObstLvl2;		//Position des Obstacles du niveau2
	public GameObject[] obstLvl2;			//Obstacles du niveau 2
	float clignStart = 0.0f;
	float posIni; 							//position initiale de la caméra
	
	
	// Use this for initialization
	void Start () 
	{
		posIni = gameObject.transform.position.z;
	}
	
	// Update is called once per frame
	void Update () 
	{
		
		switch(state)
		{
		case "init":		UpadateInit();		break;
		case "GamePart":	UpdateGamePart();	break;
		case "quit":		UpdateQuit();		break;	
		}
	}
	
	void OnGUI()
	{
		switch (state)
		{
		case "menu":		OnGUIMenu();		break;
		case "level":		OnGUILevel();		break;
		case "help":		OnGUIHelp();		break;
		case "GamePart":	OnGUIGamePart();	break;
		case "win":			OnGUIWin();			break;
		case "loose":		OnGUILoose();		break;
		}
		
	}
	
	
	
	void OnGUIMenu()
	{
		GUI.contentColor = Color.black;
		
		GUI.DrawTexture(new Rect(Screen.width/2-160,40,320,100), titre);
		
		GUI.DrawTexture(new Rect(Screen.width/2-200,Screen.height/2-25,100,50), btn);
		GUI.DrawTexture(new Rect(Screen.width/2-50,Screen.height/2-25,100,50), btn);
		GUI.DrawTexture(new Rect(Screen.width/2+100,Screen.height/2-25,100,50), btn);
			
		if (GUI.Button(new Rect(Screen.width/2-200,Screen.height/2-25,100,50), "Jouer"))
		{
			state = "level";
		}
		
		if (GUI.Button(new Rect(Screen.width/2-50,Screen.height/2-25,100,50), "Aide"))
		{
			state = "help";
		}
		
		if (GUI.Button(new Rect(Screen.width/2+100,Screen.height/2-25,100,50), "Quitter"))
		{
			state = "quit";
		}
	}
	
	
	
	public Texture2D textAide1;
	public Texture2D textAide2;
	public Texture2D textAide3;
	int page = 1;
	
	void OnGUIHelp() //Pages d'aide
	{		
		GUI.contentColor = Color.black;
		
		GUI.DrawTexture(new Rect(Screen.width-200,Screen.height-75,150,50), btn);
		
		if (page == 1)
		{
			GUI.DrawTexture (new Rect(Screen.width/2-350,Screen.height/2-300,700,560), textAide1);
			GUI.DrawTexture(new Rect(Screen.width/2+10,Screen.height-75,100,50), btn);
			
			if (GUI.Button(new Rect(Screen.width/2+10,Screen.height-75,100,50), "Suivant"))
			{
				page = 2;
			}
		}
		
		if (page == 2)
		{
			GUI.DrawTexture (new Rect(Screen.width/2-350,Screen.height/2-300,700,560), textAide2);
			GUI.DrawTexture(new Rect(Screen.width/2-110,Screen.height-75,100,50), btn);
			GUI.DrawTexture(new Rect(Screen.width/2+10,Screen.height-75,100,50), btn);
			
			if (GUI.Button(new Rect(Screen.width/2-110,Screen.height-75,100,50), "Precedent"))
			{
				page = 1 ;
			} 
			if (GUI.Button(new Rect(Screen.width/2+10,Screen.height-75,100,50), "Suivant"))
			{
				page = 3 ;
			}
		}
		
		if (page == 3)
		{
			GUI.DrawTexture (new Rect(Screen.width/2-350,Screen.height/2-300,700,560), textAide3);
			GUI.DrawTexture(new Rect(Screen.width/2-110,Screen.height-75,100,50), btn);
			
			if (GUI.Button(new Rect(Screen.width/2-110,Screen.height-75,100,50), "Precedent"))
			{
				page = 2 ;
			} 
		}
		
		if (GUI.Button(new Rect(Screen.width-200,Screen.height-75,150,50), "Retour au menu"))
		{
			state = "menu";
		} 
	}
	
	
	
	void UpdateQuit()
	{
		Application.Quit();
	}
	
	
	
	void OnGUILevel()
	{		
		//Sélécetion du level à instancier
		
		GUI.contentColor = Color.black;
		
		GUI.DrawTexture(new Rect(Screen.width/2-200,Screen.height/2-100,100,50), btn);
		GUI.DrawTexture(new Rect(Screen.width/2-50,Screen.height/2-100,100,50), btn);
		GUI.DrawTexture(new Rect(Screen.width/2-75,Screen.height/2+50,150,50), btn);
		
		if (GUI.Button(new Rect(Screen.width/2-200,Screen.height/2-100,100,50), "Niveau 1"))
		{
			lvl = 1;
			state = "init";
		}
		
		if (GUI.Button(new Rect(Screen.width/2-50,Screen.height/2-100,100,50), "Niveau 2"))
		{
			lvl = 2;
			state = "init";
		}
		
		if (record[0] <= 20.0f)
		{
			GUI.DrawTexture(new Rect(Screen.width/2-205,Screen.height/2-25,115,25), btn);
			GUI.Label(new Rect(Screen.width/2-200,Screen.height/2-25,100,25), "Record = " + record[0].ToString("#0.00") + " s");
		}
		
		if (record[1] <= 20.0f)
		{
			GUI.DrawTexture(new Rect(Screen.width/2-55,Screen.height/2-25,115,25), btn);
			GUI.Label(new Rect(Screen.width/2-50,Screen.height/2-25,100,25), "Record = " + record[1].ToString("#0.00") + " s");
		}
		
		if (GUI.Button(new Rect(Screen.width/2-75,Screen.height/2+50,150,50), "Retour au menu"))
		{
			state = "menu";
		} 
	}
	
	void UpadateInit()
	{
		hitToReach = 0;
		
		switch (lvl)
		{
		case 1:		InstLvl1();		break;
		case 2:		InstLvl2();		break;	
		}
		
		hitDetected = 0;
		
		snowBallMagazine = snowBallLvl;
		
		timerDelay = timerDelayLvl;
		
		timer = Time.time + timerDelay;
		
		startTime = Time.time;
		
		state = "GamePart";
	}
	
	public GameObject cible;
	
	void InstLvl1()
	{
		for(int i=0; i<posBdN.Length; i++)
		{			
			GameObject pos = posBdN[i];
			
			GameObject newObject = (GameObject) GameObject.Instantiate(cible,pos.transform.position,pos.transform.rotation);
		
			listOfInstanciedObjects.Add(newObject);
			
			hitToReach = listOfInstanciedObjects.Count;
			
			snowBallLvl = 3;
			
			timerDelayLvl = 20.0f;
		}
	}
	
	void InstLvl2()
	{
		for(int i=0; i<posBdN.Length; i++)
		{			
			GameObject pos = posBdN[i];
			
			GameObject newObject = (GameObject) GameObject.Instantiate(cible,pos.transform.position,pos.transform.rotation);
		
			listOfInstanciedObjects.Add(newObject);
			
			hitToReach = listOfInstanciedObjects.Count;
		}
		
		for(int i=0; i<posObstLvl2.Length; i++)
		{			
			GameObject pos = posObstLvl2[i];
			
			GameObject obj = obstLvl2[i];	
			                 
			GameObject newObject = (GameObject) GameObject.Instantiate(obj,pos.transform.position,pos.transform.rotation);
		
			listOfInstanciedObjects.Add(newObject);
		}
			
		snowBallLvl = 3;
			
		timerDelayLvl = 20.0f;
	}
	
	
	public GameObject snowBall;
	GameObject fire;
	
	
	void UpdateGamePart()
	{
		
		if (Input.GetMouseButtonDown(0))
		{
			if(snowBallMagazine > 0)
			{
				// Construct a ray from the current mouse coordinates
				Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		
				// Create a bullet if hit
				fire = (GameObject) Instantiate(snowBall, transform.position, transform.rotation);
		
				// launch the bullet
				fire.rigidbody.AddForce(ray.direction * 5000.0f);
				
				snowBallMagazine --; 
			}
		}
		
		if (Input.GetKey(KeyCode.LeftArrow) && transform.position.z > posIni-50.0f)
		{
			transform.Translate(Vector3.left);
		}
		
			if (Input.GetKey(KeyCode.RightArrow) && transform.position.z < posIni+50.0f)
		{
			transform.Translate(Vector3.right);
		}
			
		if (Time.time > timer)
		{
			//out of time
			DestroyAll();
			listOfInstanciedObjects.Clear() ;
			transform.position = new Vector3(transform.position.x,transform.position.y,posIni);
			state = "loose";	
		}
		else
		{
			if (hitDetected == hitToReach)
			{
				DestroyAll();
				listOfInstanciedObjects.Clear() ;
				playTime = Time.time - startTime;
				transform.position = new Vector3(transform.position.x,transform.position.y,posIni);
				state = "win";
			}
		
		}
	}
	
	
	void OnGUIGamePart()
	{
		GUI.contentColor = Color.black;
		
		GUI.DrawTexture(new Rect(10,20,100,25), btn);
		GUI.DrawTexture(new Rect(10,80,130,25), btn);
		GUI.DrawTexture(new Rect(Screen.width-200,250,100,25), btn);
		
		
		GUI.Label(new Rect(10,20,100,25), "Cibles : " + hitDetected + " / " + hitToReach + "");
		GUI.Label(new Rect(10,80,130,25), "Temps restant : " + (int)(timer - Time.time + 1) + " s");
		
		GUI.DrawTexture(new Rect(Screen.width-250,25,200,200), ammoZone);
		GUI.Label(new Rect(Screen.width-200,250,100,25), "Chargeur : " + snowBallMagazine + " / " + snowBallLvl + "");
		
	
		
		
		if(snowBallMagazine == 0)
		{
			//Debug.Log ("init");
			clignStart = Time.time;
			if (Time.time < clignStart + 10.0f)
			{
				if ((Time.time * 3) % 2 < 1)
				{
				GUI.DrawTexture(new Rect(Screen.width/2-75,Screen.height/2-75,150,150),reloadImg);
				}
			}
		}
		else
		{
				//Debug.Log ("reset");
	
		}
		}
	
	
	bool rec = false;
	void OnGUIWin()
	{
		Debug.Log(record[lvl-1]);
		Debug.Log("playTime" + playTime);
		GUI.contentColor = Color.black;
		GUI.DrawTexture(new Rect(Screen.width/2-50,50,100,25), btn);
		GUI.DrawTexture(new Rect(Screen.width/2-200,Screen.height/2+50,100,50), btn);
		GUI.DrawTexture(new Rect(Screen.width/2-50,Screen.height/2+50,100,50), btn);
		GUI.DrawTexture(new Rect(Screen.width/2+100,Screen.height/2+50,100,50), btn);
		
		GUI.Label(new Rect(Screen.width/2-25,50,50,25), "Gagné");
		
		
			if (playTime <= record[lvl-1] )
			{
				record[lvl-1] = playTime ;
				rec = true;
			}
			else
			{
				rec = false;
			}
	
		
			//Debug.Log ("record");
			
			GUI.DrawTexture(new Rect(Screen.width/2-55,Screen.height/2-50,110,25), btn);
			GUI.DrawTexture(new Rect(Screen.width/2-30,Screen.height/2,60,25), btn);
			if(rec == true)
			{
				GUI.Label (new Rect(Screen.width/2-55,Screen.height/2-50,110,25),"Nouveau Record !");
				GUI.Label (new Rect(Screen.width/2-25,Screen.height/2,50,25),"" + record[lvl-1].ToString("#0.00") + " s");
			}
			else
			{
				GUI.Label (new Rect(Screen.width/2-45,Screen.height/2-50,80,25),"Votre temps :");
				GUI.Label (new Rect(Screen.width/2-25,Screen.height/2,50,25),"" + playTime.ToString("#0.00") + " s");
			}
			
			
		
		
		if (GUI.Button(new Rect(Screen.width/2-200,Screen.height/2+50,100,50), "Rejouer"))
		{
			state = "init";
		}
		
		if (GUI.Button(new Rect(Screen.width/2-50,Screen.height/2+50,100,50), "Niveau Suivant"))
		{
			lvl = lvl+1;
			state = "init";
		}
		
		if (GUI.Button(new Rect(Screen.width/2+100,Screen.height/2+50,100,50), "Choix Niveau"))
		{
			state ="level";
		}
	}
	
	
	
	void OnGUILoose()
	{
		GUI.contentColor = Color.black;
		
		GUI.DrawTexture(new Rect(Screen.width/2-25,50,50,25), btn);
		GUI.DrawTexture(new Rect(Screen.width/2-175,Screen.height/2+50,100,50), btn);
		GUI.DrawTexture(new Rect(Screen.width/2-25,Screen.height/2+50,200,50), btn);
		
		GUI.Label(new Rect(Screen.width/2-25,50,50,50), "Perdu");
		
		if (GUI.Button(new Rect(Screen.width/2-175,Screen.height/2+50,100,50), "Reessayer"))
		{
			state = "init";
		}
		
		if (GUI.Button(new Rect(Screen.width/2-25,Screen.height/2+50,200,50), "Retour au choix des niveaux"))
		{
			state ="level";
		}
	}
	
	void DestroyAll()
	{
		for(int i=0; i<listOfInstanciedObjects.Count; i++)
		{
			GameObject.Destroy((GameObject) listOfInstanciedObjects[i]);
		}
		
	}
}