using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour 
{
	public static float freqBoat;		//Frequence d'apparition des ennemis en seconde 
	
	public static bool perdu1vie;		//Permet de verifier si on perd une vie
	public static bool betweenLevel;	//Permet de verifier si je passe d'un niveau à l'autre
	public static bool win;				//Permet de verifier si j'ai terminé le jeu
	public static bool gameOver;		//Permet de verifier si je suis en GameOver
	public static bool loose;			//Permet de verifier si j'ai perdu une vie 
	
	public static float timerLevel;		//Temps du niveau 
	public static int life;				//Nombre de vies
	public static int alcohol;			//Le taux d'alcool (la jauge de vie)
	public static int level;			//Le niveau dans lequel on se trouve
	public static int score;			//Le score de la partie 
	public static int ammunition;		//Le nombre de munitions 
	
	public GameObject boat;				//le prefab "Ensemble bateau"
	
	public static bool fireOK;			//Frequence d'apparition des ennemis en seconde 
	

	void Start () //Initialisation des variables
	{	
		RenderSettings.fog = false;
	
		ammunition = 100;
		fireOK = true;
		loose = false;
		gameOver = false;
		win = false;
		betweenLevel = false;
		perdu1vie = false;
		alcohol = 100;
		score = 0;
		timerLevel = 45.0f;
		level = 4;
		life = 3;
		freqBoat = 3;
		RenderSettings.fog = true;
		StartCoroutine(Compteur());
	}
	
	IEnumerator Compteur()
	{
		while(1==1)
		{
			yield return new WaitForSeconds(freqBoat);
			InstantiateBoat();
		}
	}
	
	// Update is called once per frame
	void Update () 
	{	
		Ammunition();
		Alcohol();
		Level();
		GameOver();
	}
	
	//Gestion du taux d'alcool
	void Alcohol()
	{	
		if(alcohol <=0)
		{
			perdu1vie = true;
			loose = true;
			alcohol = 100;
		}
	}
	
	//Gestion des vies
	public static void Life()
	{
		if(perdu1vie)
		{
			life -=1;
			perdu1vie = false;
		}
	}
	
	//Gestion d'instance "Ensemble bateau"
	void InstantiateBoat()
	{
		if(!loose && !betweenLevel && !gameOver && !win)
		Instantiate (boat,new Vector3(Random.Range(-20,20),0,60),boat.transform.rotation);
	}
	
	//Gestion des munitions
	void Ammunition()
	{
		if( ammunition == 0)
		{
			fireOK = false;
		}
		else 
		{
			fireOK = true;
		}
	}
	//Gestion des Niveaux (Temps)
	void Level()
	{
		if(level >= 1 && level <=5)
		{
			timerLevel -= Time.deltaTime;
			
			if(timerLevel <= 0.0f)
			{
				betweenLevel = true;
			}
		}
		if(level == 1)
		{
			Material skybox1 = (Material) Resources.Load("Skyboxes/DawnDusk Skybox", typeof(Material));
			RenderSettings.skybox = skybox1;
		}
		else if(level == 2)
		{
			Material skybox2 = (Material) Resources.Load("Skyboxes/Sunny2 Skybox", typeof(Material));
			RenderSettings.skybox = skybox2;
		}
		else if(level == 3)
		{
			Material skybox3 = (Material) Resources.Load("Skyboxes/Overcast2 Skybox", typeof(Material));
			RenderSettings.skybox = skybox3;
		}
		else if(level == 4)
		{
			Material skybox4 = (Material) Resources.Load("Skyboxes/Eerie Skybox", typeof(Material));
			RenderSettings.skybox = skybox4;
		}
		else if(level == 5)
		{
			Material skybox5 = (Material) Resources.Load("Skyboxes/MoonShine Skybox", typeof(Material));
			RenderSettings.skybox = skybox5;
		}
	}
	
	//Initialisation des niveaux
	public static void LevelInit()
	{
		Screen.showCursor = false;
		betweenLevel = false;
		
		if(level == 1)
		{
			RenderSettings.fog = false;
			level = 2;
			alcohol = 100;
			freqBoat = 3;
			BoatManager.boatSpeed = -3;
			ammunition = 100;
			PirateManager.timePirateShoot = Random.Range(1.0f,5.0f);
		}
		else if(level == 2)
		{
			RenderSettings.fog = true;
			level = 3;
			alcohol = 100;
			freqBoat = 2;
			BoatManager.boatSpeed = -4;
			ammunition = 100;
			PirateManager.timePirateShoot = Random.Range(1.0f,4.0f);
		}
		else if(level == 3)
		{
			RenderSettings.fog = false;
			level = 4;
			alcohol = 100;
			freqBoat = 2;
			BoatManager.boatSpeed = -4;
			ammunition = 100;
			PirateManager.timePirateShoot = Random.Range(1.0f,3.0f);
		}
		else if(level == 4)
		{
			RenderSettings.fog = true;
			level = 5;
			alcohol = 100;
			freqBoat = 1.5f;
			BoatManager.boatSpeed = -5;
			ammunition = 150;
			PirateManager.timePirateShoot = Random.Range(1.0f,3.0f);
		}
	}
	
	//Quand on à perdu un niveau 
	public static void Loose()
	{
		Life();
		timerLevel = 45.0f;
		ammunition = 100;
		
		if(level == 5)
			ammunition = 150;
	}
	
	//quand on a perdu le jeu
	void GameOver()
	{
		if(life ==0)
			gameOver = true;
	}
}
