using UnityEngine;
using System.Collections;

public class gestionMinion : MonoBehaviour {
	
	// gestion des timer
	public float timerNiveau;
	public bool timerActiveNiveau = false;
	
	//mesure le temps pour lancer le deuxième minion
	public float timerMinion2;
	
	//nombre de mignon en cours
	public static int nbMinion;
	
	// timer pour la vitesse
	public static float timerVitesse;
	
	// vérifie la présence d'un minion
	public static bool minion; 
	
	//donne le niveau actuel
	public static int niveau = 0;
	
	public GameObject mm1;
	public GameObject mm2;
	public GameObject m1;
	public GameObject m2;
	
	// variable de position de sortie du minion
	public Vector3 position;
	public int randPosition;
	
	
	// variable aléatoire minion
	public int randMinion;
	public static int minionGentilSortie = 0;
	public static int minionMechantSortie = 0;
	public static bool actuMinionGentil;
			

	// Use this for initialization
	void Start () 
	{
		minion = false;
	}
	
	// Update is called once per frame
	void Update () 
	{
		if (niveau == 1)
		{
			niveau1 ();
		}
		
		if (niveau == 2)
		{
			niveau2 ();
		}
		
		
	}
	
	//créer un minion
	void creationMinion ()
	{
		positionMinion ();
		
		// créer le minion
		randMinion = Random.Range(0,4);
		
		// limite la différence entre le nombre de minion méchant et gentil de sortie
		if (minionGentilSortie - minionMechantSortie > 3)
		{
			randMinion = Random.Range(0,2);
		}
		
		if (minionMechantSortie - minionGentilSortie > 3)
		{
			randMinion = Random.Range(2,4);
		}
		
		// instantie le minion
		switch (randMinion)
		{
		case 0:
			Instantiate (mm1,position,Quaternion.Euler (270,270,0));
			minionMechantSortie++;
			actuMinionGentil = false;
			GameObject.Find ("prefabMinionmechant1(Clone)").name="Minion"+randPosition;
			break;
			
		case 1:
			Instantiate (mm2,position,Quaternion.Euler (270,270,0));
			minionMechantSortie++;
			actuMinionGentil = false;
			GameObject.Find ("prefabMinionmechant2(Clone)").name="Minion"+randPosition;
			break;
			
		case 2:
			Instantiate (m1,position,Quaternion.Euler (270,270,0));
			minionGentilSortie++;
			actuMinionGentil = true;
			GameObject.Find ("prefabMiniongentil1(Clone)").name="Minion"+randPosition;
			break;
			
			
		case 3:
			Instantiate (m2,position,Quaternion.Euler (270,270,0));
			minionGentilSortie++;
			actuMinionGentil = true;
			GameObject.Find ("prefabMiniongentil2(Clone)").name="Minion"+randPosition;
			break;
		}
		
		Debug.Log("mm sortie"+minionMechantSortie);
		Debug.Log("score" + gestionAffichage.score);
		
		minion = true;
	}
	
	// gère la position aléatoire du minion
	void positionMinion ()
	{
		randPosition = Random.Range(0,12);
		
		while(GameObject.Find ("Minion"+randPosition)!=null)
		{
		randPosition = Random.Range(0,12);	
		}
		
		switch (randPosition)
		{
			case 0 :
			{
				position = new Vector3(0,-12,0);
				break;
			}
			case 1 :
			{
				position = new Vector3(0,-12,40);
				break;
			}
			case 2 :
			{
				position = new Vector3(0,-12,80);
				break;
			}
			case 3 :
			{
				position = new Vector3(40,-12,0);
				break;
			}
			case 4 :
			{
				position = new Vector3(40,-12,40);
				break;
			}
			case 5 :
			{
				position = new Vector3(40,-12,80);
				break;
			}
			case 6 :
			{
				position = new Vector3(80,-12,0);
				break;
			}
			case 7 :
			{
				position = new Vector3(80,-12,40);
				break;
			}
			case 8 :
			{
				position = new Vector3(80,-12,80);
				break;
			}
			case 9 :
			{
				position = new Vector3(120,-12,0);
				break;
			}
			case 10 :
			{
				position = new Vector3(120,-12,40);
				break;
			}
			case 11 :
			{
				position = new Vector3(120,-12,80);
				break;
			}
			
		}
		
		
	}
	
	
	void niveau1 ()
	{
		if (timerActiveNiveau == false)
			{
				timerVitesse = Time.time;
				timerNiveau = Time.time + 90;
				timerActiveNiveau = true; 
				gestionAffichage.actuAffichageScore =true;
			}
			if (Time.time <= timerNiveau)
			{
				if (minion == false )
				{
					creationMinion ();
					nbMinion++;
					//Debug.Log(nbMinion);
				}
					
				
				//vérifie actualisation du score et du temps
				gestionAffichage.timeLeft = timerNiveau - Time.time;
				
			}
			else 
			{
				timerActiveNiveau = false;
				niveau = 0;
				//gestionAffichage.score = 0;
				//minionMechantSortie = 0;
				minionGentilSortie = 0;
			
				if ( gestionAffichage.scoreFinal > 70)
				{
					gestionAffichage.etape = "fin niveau1";
				}
				else 
				{
					gestionAffichage.etape = "score insuffisant";
					gestionSon.sonActif = false;
				}
			}
	}
	
	void niveau2 ()
	{
		if (timerActiveNiveau == false)
			{
				timerVitesse = Time.time;
				timerNiveau = Time.time + 90;
				timerActiveNiveau = true;
				gestionAffichage.actuAffichageScore =true;
				
			}
			if (Time.time <= timerNiveau)
			{
				if (gestionAffichage.actuScore == false)
				{
						/*creationMinion ();
						nbMinion++;*/
				
					if (nbMinion < 1)
					{
						creationMinion ();
						nbMinion++;
					}
					
					//Debug.Log(nbMinion);
				}
					
				
				//vérifie actualisation du score et du temps
				gestionAffichage.timeLeft = timerNiveau - Time.time;
				
			}
			else 
			{
				timerActiveNiveau = false;
				if ( gestionAffichage.scoreFinal > 70)
				{
					gestionAffichage.etape = "fin niveau2";
				}
				else 
				{
					gestionAffichage.etape = "score insuffisant";
					gestionSon.sonActif = false;
				}
				niveau = 0;
				//gestionAffichage.score = 0;
				//minionMechantSortie = 0;
				minionGentilSortie = 0;
			
			}
	}
	
}
