using UnityEngine;
using System.Collections;

public class gestionAffichage : MonoBehaviour {
	
	//gestion de la police d'écriture
	public GUIStyle policeJeu;
	public GUIStyle policeTransition;
	
	// gestion du timer
	public float timer;
	public bool timerActive = false;
	
	
	// temps restant au joueur pour finir le niveau
	public static float timeLeft;
	
	// variable enregistrant le niveau précédant pour gérer le recommencer
	public int precNiveau = 0;
	
	// variable de score
	public static float scoreFinal;
	public static bool actuAffichageScore;
	public static int scoreNiveau1;
	public static int scoreNiveau2;
	public static int mimionMéchantSortieNiveau1;
	public static int mimionMéchantSortieNiveau2;
	public static int score;
	public static bool actuScore;
	
	// variable d'affichage de fonds
	public static string etape = "menu" ;
	
	// texture à afficher
	public Texture2D fondMenu;
	public Texture2D explication;
	public Texture2D fondTransition1;
	public Texture2D fondNiveau1;
	public Texture2D fondFinNiveau1;
	public Texture2D fondTransition2;
	public Texture2D fondNiveau2;
	public Texture2D fondFinNiveau2;
	public Texture2D fondEcranDesScores;
	public Texture2D fondMeilleurScore;
	public Texture2D fondScoreInsuffisant;
	
	// GUI skins à afficher
	public GUISkin skinJouer;
	public GUISkin skinQuitter;
	public GUISkin skinRecommencer;
	public GUISkin skinMenu;
	public GUISkin skinContinuer;
	
	// variables de position des boutons
	public float posXButton1;
	public float posYButton;
	public float posXButton2;
	
	// variables de position du score
	float posXScore;
	float posYScore;
	float LXScore;
	float lYScore;
	
	// variables de position du temps
	float posXTemps; 
	float posYTemps; 
	float LXTemps;
	float lYTemps;
	
	// variables pour la sauvegarde du meilleur score
	public string nom;
	public int saveScore;
	public bool affichVotreNom;
	
	// Use this for initialization
	void Start () 
	{
		// initialisation des variables de position de l'affichage
		
		//boutons
		posXButton1 = Screen.width/2-Screen.width/6-256;
		posYButton = Screen.height-100;
		posXButton2 = Screen.width/2+Screen.width/6;
		
		//Debug.Log(Screen.width);
		//Debug.Log(Screen.height);
		
		// score
		posXScore = Screen.width/8;
		posYScore = Screen.height/18;
		LXScore =Screen.width/7;
		lYScore = Screen.height/4;
		
		// temps
		posXTemps = (Screen.width/9)*8;
		posYTemps = posYScore;
		LXTemps =LXScore;
		lYTemps =lYScore;
		
		// initialisation des scores à 0
		scoreNiveau1 = 0;
		scoreNiveau2 = 0;
		score = 0;
		
		affichVotreNom =false;
	}
	
	// Update is called once per frame
	void Update () {
		
		//Debug.Log(nom);
		gestionAffichageScore ();
		//PlayerPrefs.SetInt("HighScore",0);
	}
	
	
	
	// gère l'actualisation, le calcul et l'affichage du score final
	void gestionAffichageScore ()
	{
		if (etape == "niveau1")
		{
			if (actuAffichageScore == true)
			{
				if (gestionMinion.minionMechantSortie > 0)
				{
					scoreFinal = (scoreNiveau1 * 100/gestionMinion.minionMechantSortie);	
				}
				actuAffichageScore =false;
				
				//Debug.Log("mms = " +gestionMinion.minionMechantSortie);
			}
		}
		
		if (actuAffichageScore == true)
		{
			if (etape == "niveau2")
			{
				if (gestionMinion.minionMechantSortie > 0)
				{
					scoreFinal = (scoreNiveau2 * 100/gestionMinion.minionMechantSortie);
				}
				else 
				{
					scoreFinal =0;
				}
			}
			actuAffichageScore =false;
		}
	}
	
	void gestionScore ()
	{
		if (actuScore == true)
		{	
			// on vient charger la variable score niveau1 ou niveau2 pour l'afficher
			if (gestionAffichage.etape == "niveau1")
			{
				scoreNiveau1 = score;
			}
			if (gestionAffichage.etape == "niveau2")
			{
				scoreNiveau2 = score;
				//Debug.Log("score n2"+scoreNiveau2);
				//Debug.Log("nb minion sortie"+gestionMinion.minionMechantSortie );
			}
			
			//affichage de l'actualisation du score
			actuAffichageScore =true;
			
			actuScore =false;
			
		}
	}
	
	void OnGUI ()
	{
		OnGUIFond ();
		if (etape == "niveau1" || etape == "niveau2")
		{
			OnGUIScore ();
			OnGUITimeLeft ();
		}
	}
	
	void OnGUIScore ()
	{
		// affichage du score final
		 GUI.Label(new Rect(posXScore, posYScore, LXScore , lYScore), scoreFinal + "%",policeJeu);
	}
	
	void OnGUITimeLeft ()
	{
		// affichage du temps restant
		 GUI.Label(new Rect(posXTemps, posYTemps, LXTemps, lYTemps),(int) (timeLeft+1) + "s",policeJeu);
	}
	
	void OnGUIFond ()
	{
		switch (etape)
		{
			case "menu":
				//application fond menu
				GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), fondMenu);
			
				// affichage et chargement du meilleur score
				
				saveScore=PlayerPrefs.GetInt("HighScore");
				GUI.Label(new Rect((Screen.width/18)*15,(Screen.height/2) ,Screen.width/4  ,Screen.height/4 ), PlayerPrefs.GetString("HighScoreName"),policeJeu);
				GUI.Label(new Rect((Screen.width/18)*15,(Screen.height/5)*3 ,Screen.width/4  ,Screen.height/4 ), saveScore + "%",policeJeu);
				
				// bonton jouer
				GUI.skin = skinJouer;
				if (GUI.Button (new Rect (posXButton1,posYButton,256,128),""))
					{
						etape = "explication";
						score = 0;
					}
				
				// bonton quitter
				GUI.skin = skinQuitter;
				if (GUI.Button (new Rect (posXButton2,posYButton,256,128),""))
					{
						Application.Quit();
					}
			
				score =0;
				scoreNiveau1 = 0;
				scoreNiveau2 = 0;
				gestionMinion.minionMechantSortie = 0;
			break;
			
			case "explication":
				GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), explication);
				if (timerActive ==false)
					{
						timer = Time.time + 5;
						timerActive = true;
					}
				if (Time.time > timer)
					{
						etape = "transition1";
						timerActive = false;
					}
			break;
			
			case "transition1":
				if (timerActive ==false)
				{
					timer = Time.time + 5;
					timerActive = true;
				}
				
				GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), fondTransition1);
				
				if (Time.time > timer)
				{
					etape = "niveau1";
					timerActive = false;
					gestionSon.sonActif = false;
				}
			break;
			
			case "niveau1":
				GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), fondNiveau1);
				gestionMinion.niveau = 1;
				precNiveau = 1;
				gestionScore ();
			break;
			
			case "fin niveau1":
			
			// mise en mémoire du nombre de minion méchant sortie au niveau 1
			mimionMéchantSortieNiveau1 = gestionMinion.minionMechantSortie;

			GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), fondFinNiveau1);
			
			// affichage du score du niveau 1
		 	GUI.Label(new Rect((Screen.width/3)*2,(Screen.height/7)*5 ,Screen.width/4  ,Screen.height/4 ), (scoreNiveau1*100/gestionMinion.minionMechantSortie) + "%",policeTransition);
			
			// bonton continuer
			GUI.skin = skinContinuer;
			if (GUI.Button (new Rect (posXButton1,posYButton,256,128),""))
			{
				etape = "transition2";
				gestionSon.sonActif = false;
			}
			
			// bonton menu
			GUI.skin = skinMenu;
			if (GUI.Button (new Rect (posXButton2,posYButton,256,128),""))
			{
				etape = "menu";
			}
			break;
			
			case "transition2":
			if (timerActive ==false)
				{
					timer = Time.time + 5;
					timerActive = true;
					gestionMinion.minionMechantSortie = 0;
					score = 0;
					actuScore =true;
				}
				
				GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), fondTransition2);
				
				if (Time.time > timer)
				{
					etape = "niveau2";
					timerActive = false;
					actuScore =true;
				}
			break;
			
			case "niveau2":
				GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), fondNiveau2);
				gestionMinion.niveau = 2;
				precNiveau = 2;
				gestionScore ();
			break;
			
			case "fin niveau2":
				mimionMéchantSortieNiveau2 = gestionMinion.minionMechantSortie;
			
				saveScore=PlayerPrefs.GetInt("HighScore");
				if (saveScore <= ((scoreNiveau1*100/mimionMéchantSortieNiveau1)+(scoreNiveau2*100/mimionMéchantSortieNiveau2))/2)
				{
					etape = "meilleur score";
					gestionSon.sonActif = false;
				}
				else
				{
					etape = "ecran des scores";
					gestionSon.sonActif = false;
				}
			/*GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), fondFinNiveau2);
			
			// bonton continuer
			GUI.skin = skinContinuer;
			if (GUI.Button (new Rect (posXButton1,Screen.height-100,Screen.width/4,75),""))
				{

				}
			
			// bonton menu
			GUI.skin = skinMenu;
			if (GUI.Button (new Rect (posXButton2,posYButton,Screen.width/4,75),""))
				{
					etape = "menu" ;
				}*/
			break;
			
			case "ecran des scores":
			GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), fondEcranDesScores);
			GUI.Label(new Rect((Screen.width/4)*3,(Screen.height/17) ,Screen.width/4  ,Screen.height/4 ), (((scoreNiveau1*100/mimionMéchantSortieNiveau1)+(scoreNiveau2*100/mimionMéchantSortieNiveau2))/2) + "%",policeTransition);
			// bonton menu
			GUI.skin = skinMenu;
			if (GUI.Button (new Rect (posXButton2,(Screen.height/3),256,128),""))
				{
					gestionSon.sonActif = false;
					etape = "menu" ;
				}
			break;
			
			case "meilleur score":
			if (affichVotreNom ==false)
			{
				nom = "Votre Nom";
				affichVotreNom = true;
			}
			GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), fondMeilleurScore);
			GUI.Label(new Rect((Screen.width/9)*4,(Screen.height/22) ,Screen.width/4  ,Screen.height/4 ), ((scoreNiveau1*100/mimionMéchantSortieNiveau1)+(scoreNiveau2*100/mimionMéchantSortieNiveau2))/2 + "%",policeTransition);
			nom  = GUI.TextField(new Rect((Screen.width/9)*7, (Screen.height/22),Screen.width/4, Screen.height/4), nom, policeTransition);
			

			
			// bonton menu
			GUI.skin = skinMenu;
			if (GUI.Button (new Rect ((Screen.width/7)*3+Screen.width/4,Screen.height/10*2,256,128),""))
				{
					etape = "menu" ;
				
					// sauvegarde
					saveScore = ((scoreNiveau1*100/mimionMéchantSortieNiveau1)+(scoreNiveau2*100/mimionMéchantSortieNiveau2))/2;
					PlayerPrefs.SetString("HighScoreName",nom);
					PlayerPrefs.SetInt("HighScore",saveScore);
					PlayerPrefs.Save();
				
					gestionSon.sonActif = false;
				}
			break;
			
			case "score insuffisant":
			GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), fondScoreInsuffisant);
			
			// bonton recommencer
			GUI.skin = skinRecommencer;
			if (GUI.Button (new Rect (posXButton1,posYButton,256,128),""))
				{
					if(precNiveau == 1)
					{
						scoreNiveau1 = 0;
						gestionMinion.minionMechantSortie = 0;
						score = 0;
						actuAffichageScore =true;
						etape = "niveau1";
					}
					if(precNiveau == 2)
					{
						scoreNiveau2 = 0;
						gestionMinion.minionMechantSortie = 0;
						score = 0;
						actuAffichageScore =true;
						etape = "niveau2";
					}
					
					gestionSon.sonActif = false;
				}
			
			// bouton menu
			GUI.skin = skinMenu;
			if (GUI.Button (new Rect (posXButton2,posYButton,256,128),""))
				{
					etape = "menu" ;
					gestionSon.sonActif = false;
				}
			break;
			
			/*PlayerPrefs.SetString("HighScore",nom);
			PlayerPrefs.SetInt("HighScore",saveScore);
			PlayerPrefs.Save();
			
			nom=PlayerPrefs.GetString("HighScore");
			saveScore=PlayerPrefs.GetInt("HighScore");*/
			
		}
	}
	
	
}
