using UnityEngine;
using System.Collections;

public class lancer : MonoBehaviour 
{
	
	public Texture ensemble;
	public Texture jouer;
	Vector3 posDepart;
	Quaternion rotDepart;
	Vector3 vectorShoot;
	public GameObject posDepartCam;
	public GameObject posFinalCam;
	public GameObject serge;
	bool go = false;
	float goTime;
	bool respawn=false;
	bool playable;
	public Camera vueEnsemble;
	bool grosseVue;
	public Camera normal;
	public Transform lama;
	int score;
	float tempsRestant;
	int secondesRestantes;
	public Font fofi;
	int taillePolice;
	int touchePerso;
	string nomJoueur;
	bool enAttente;

	
	void Start () 
	{
		//INITIALISATION DES VARIABLES
		posDepart.x = -2.681483f;
		posDepart.y = 1.3663691f;
		posDepart.z = 0.0f;
		rotDepart.x = 270.0f;
		rotDepart.y = 308.45f;
		rotDepart.z = 0.0f;
		
		grosseVue=false;
		playable=true;
		
		tempsRestant=60.0f;
		taillePolice=55;
		
		touchePerso=0;
		enAttente=true;
		
	}
	
	void Update () 
	{
		
		if (go)
		{//SI LE LAMA EST LANCE / DEPLACEMENT DE LA CAMERA
			Camera.main.transform.position = Vector3.Lerp(posDepartCam.transform.position,posFinalCam.transform.position,Time.time - goTime);
			if (Time.time - goTime > 1.0f) 
				go = false;
		}
		// FONCTION RESPAWN APRES 4 SECONDES
		if (Time.time >= goTime+4.0f && Time.time <= goTime+4.1f)
		{
			Respawn ();
		}
		
		tempsRestant -= Time.deltaTime;
		// ON PERD APRES 1 MIN
		if (tempsRestant<=0.0f)
		{
			Application.LoadLevel("perdu");
		}
		
		//FORMATAGE DE LA DATE § A LA DURE
		secondesRestantes = (int) tempsRestant;
		
		Win();
		Debug.Log(touchePerso);
	}
	
	// FONCTION DE VICTOIRE SELON LE NIVEAU QUE L ON VIENT DE GAGNER
	void Win()
	{
		if (Application.loadedLevelName=="angryNiveau1" )
		{
			if (touchePerso==1)
			{
				Scoring ();
				touchePerso=0;
				Application.LoadLevel("levels");
			}
		}
		if (Application.loadedLevelName=="angryNiveau2")
		{
			if (touchePerso==3)
			{
				Scoring ();
				touchePerso=0;
				Application.LoadLevel("levels");
			}
		}
		if (Application.loadedLevelName=="angryNiveau3")
		{
			if (touchePerso==4)
			{
				Scoring ();
				touchePerso=0;
				Application.LoadLevel("gagne");
			}
		}
	}
	// FONCTION QUI PERMET D ENREGISTRER LES SCORES NIVEAU PAR NIVEAU
	void Scoring () 
	{
		if (Application.loadedLevelName=="angryNiveau1" )
		{
			PlayerPrefs.SetInt("score1",score);
			PlayerPrefs.Save();
		}
		if (Application.loadedLevelName=="angryNiveau2" )
		{
			PlayerPrefs.SetInt("score2",score);
			PlayerPrefs.Save();
		}
		// SI ON FINIT LE NIVEAU 3 ALORS ON VA ENREGISTRER LE TOTAL DES TROIS NIVEAUX POUR LE MEME JOUEUR
		// REMPLACER LE SCORE PRECEDENT SI ON L A BATTU
		if (Application.loadedLevelName=="angryNiveau3" )
		{
			PlayerPrefs.SetInt("score3",score);
			PlayerPrefs.Save();
			
			if (PlayerPrefs.HasKey("scoreTotal"))
			{
				if (PlayerPrefs.GetInt("scoreTotal") < (PlayerPrefs.GetInt("score1") + PlayerPrefs.GetInt("score2") + PlayerPrefs.GetInt("score3")))
				{
					PlayerPrefs.SetInt("scoreTotal", (PlayerPrefs.GetInt("score1") + PlayerPrefs.GetInt("score2") + PlayerPrefs.GetInt("score3")));
					PlayerPrefs.SetString ("winneur", nomJoueur);
					PlayerPrefs.Save();
				}
			}
			else
			{
				PlayerPrefs.SetInt("scoreTotal", (PlayerPrefs.GetInt("score1") + PlayerPrefs.GetInt("score2") + PlayerPrefs.GetInt("score3")));
				PlayerPrefs.SetString ("winneur", nomJoueur);
				PlayerPrefs.Save();
			}
			
			PlayerPrefs.DeleteKey("score1");
			PlayerPrefs.DeleteKey("score2");
			PlayerPrefs.DeleteKey("score3");
			PlayerPrefs.Save();	
			
				
		}
		
	}
	
	// FONCTION QUI REPLACE SERGE A SA PLACE
	void Respawn () 
	{
		enAttente=true;
		if (playable)
		{
			transform.rigidbody.useGravity=false;
			serge.transform.position=posDepart;
			serge.transform.rotation = lama.transform.rotation;
			
				
			transform.rigidbody.velocity = Vector3.zero;
		  	transform.rigidbody.angularVelocity = Vector3.zero;
	
				
			Camera.main.transform.position = Vector3.Lerp(posFinalCam.transform.position,posDepartCam.transform.position,Time.time - goTime);
			respawn=false;
		}

	}
	
	
	// QUAND JE LACHE LE BOUTON DE LA SOURIS APRES AVOIR DRAG
	void OnMouseUp()
	{
		if (playable)
		{
			transform.rigidbody.useGravity=true;
			Vector3 posLache = transform.position;
			vectorShoot=posDepart-posLache;
	
			transform.rigidbody.AddForce(vectorShoot*15, ForceMode.Impulse);
		
			
			go =true;
			goTime = Time.time;
		}
	}
 	
	
	
	void OnMouseDrag()
	{
		if (playable)
		{
			enAttente=false;
			transform.rigidbody.velocity = Vector3.zero;
		    transform.rigidbody.angularVelocity = Vector3.zero;
			transform.rigidbody.useGravity=false;
		 
			//Drag du lama en arriere
	   		Vector3 curScreenPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
	    	Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
			curPosition.z=0;
	    	transform.position = curPosition;
		}
	}	
	
	void OnCollisionEnter (Collision collision) 
	{
		//COLLISION AVEC LE DECOR
		if ( collision.gameObject.tag == "decor")
		{
			Debug.Log(score);
			score+=50;
			Debug.Log("toucheDecor");
		}
		//COLLISION AVEC UN PERSO
		if ( collision.gameObject.tag == "perso")
		{
			Debug.Log(score);
			score+=500;
			Debug.Log("touchePerso");
			touchePerso++;
		}
	}
	
	
	void OnGUI ()
	{//SI SERGE EST A SON PT DE DEPART
		if (enAttente)
		{// ET SI JE NE SUIS PAS DEJA EN VUE DENSEMBLE
			if (!grosseVue)
			{//ALORS JACTIVE LA CAMERA DE VUE D ENSEMBLE
				if (GUI.Button (new Rect(20,422,100,50), ensemble))
				{
					vueEnsemble.enabled=true;
					Camera.main.enabled=false;
					grosseVue = true;
					playable=false;
					
				}	
			}
		}//SI JE SUIS EN VUE D ENSEMBLE
		if (grosseVue)
		{// JE REACTIVE LA PREMIERE CAMERA
			if (GUI.Button (new Rect(20,422,100,50), jouer))
			{
				normal.enabled=true;
				vueEnsemble.enabled=false;
				
				grosseVue=false;
				playable=true;
			}
		}
		// AFFICHAGE TIMER ET SCORE
		GUI.skin.font = fofi;
		GUI.skin.label.fontSize=taillePolice;
		GUI.Label(new Rect(660, 30, 100, 100),""+ (secondesRestantes));
		GUI.Label(new Rect(470, 420, 300, 100),"Score : "+ (score));
	}
	
	
	
}
