using UnityEngine;
using System.Collections;

public class GUIManager : MonoBehaviour 
{
	public GameManager GameM;							//Type GameManager me permettant de recuperer les variables du script "GameManager"
	
	public static bool clicClic = false;
	
	public GUIStyle textStyle;							// Pour changer la police des infos affichées durant le jeu
	public GUIStyle winButton;							// Pour changer la texture des etats du bouton "Rentrer feter ça"
	public GUIStyle looseButton;						// Pour changer la texture des etats du bouton "Nouvelle attaque!"
	public GUIStyle gameOverButton;						// Pour changer la texture des etats du bouton "Retour au port"
	public GUIStyle betweenLevelButton;					// Pour changer la texture des etats du bouton "Arrrh!"
	
	private int CrosshairSize = Screen.width/30;		//Taille du reticule
	
	private Texture2D alcoholTexture;					//Texture vide ou est stocké la texture du taux d'alcool à l'instant T
	
	private float timerOffside = 0.5f;					//Temps d'affichage de la texture "Offside" quand un bateau ennemi nous depasse
	private float startTime = 0;						//Depart du temps de d'affichage de la texture "Offside"
	
	private int width = Screen.width;	
	private int height = Screen.height;
	
	void Start () 
	{
		Screen.showCursor = false;
	}
	
	void OnGUI()
	{
		if(!GameManager.loose)
		{
			int timerLevelSec = (int) GameManager.timerLevel;
			Texture2D timerTexture = (Texture2D) Resources.Load("Textures/Interface/Items/Timer", typeof(Texture2D));
			GUI.DrawTexture (new Rect (width*0.87f,height*0.01f,width*0.15f,width*0.15f),timerTexture);
			GUI.Label(new Rect (width*0.915f,height*0.12f, width*0.5f,width*0.5f),"" + timerLevelSec,textStyle);
			
			Texture2D pont = (Texture2D) Resources.Load("Textures/Interface/Items/Pont", typeof(Texture2D));
			GUI.DrawTexture (new Rect (0,0,Screen.width,Screen.height),pont);
			
			Texture2D crosshair = (Texture2D) Resources.Load("Textures/Interface/Items/Crosshair", typeof(Texture2D));
			
			GUI.DrawTexture(new Rect(Input.mousePosition.x-CrosshairSize/2,Screen.height-Input.mousePosition.y-CrosshairSize/2,CrosshairSize,CrosshairSize),crosshair);
			
			Texture2D lifeTexture = (Texture2D) Resources.Load("Textures/Interface/Items/Life", typeof(Texture2D));
			GUI.DrawTexture (new Rect (width*0.02f,height*0.89f,width*0.06f,width*0.06f),lifeTexture);
			GUI.Label(new Rect (width*0.085f,height*0.9f,width*0.06f,width*0.06f),"" + GameManager.life,textStyle);
			
			GUI.DrawTexture (new Rect (width*0.15f,height*0.89f,width*0.06f,width*0.06f),alcoholTexture);
			if(GameManager.alcohol >= 100)
				alcoholTexture = (Texture2D) Resources.Load("Textures/Interface/Items/AlcoholLevel/Alcohol100", typeof(Texture2D));
			else if(GameManager.alcohol >= 90 && GameManager.alcohol < 100)
				alcoholTexture = (Texture2D) Resources.Load("Textures/Interface/Items/AlcoholLevel/Alcohol90", typeof(Texture2D));
			else if(GameManager.alcohol >= 80 && GameManager.alcohol < 90)
				alcoholTexture = (Texture2D) Resources.Load("Textures/Interface/Items/AlcoholLevel/Alcohol80", typeof(Texture2D));
			else if(GameManager.alcohol >= 70 && GameManager.alcohol < 80)
				alcoholTexture = (Texture2D) Resources.Load("Textures/Interface/Items/AlcoholLevel/Alcohol70", typeof(Texture2D));
			else if(GameManager.alcohol >= 60 && GameManager.alcohol < 70)
				alcoholTexture = (Texture2D) Resources.Load("Textures/Interface/Items/AlcoholLevel/Alcohol60", typeof(Texture2D));
			else if(GameManager.alcohol >= 50 && GameManager.alcohol < 60)
				alcoholTexture = (Texture2D) Resources.Load("Textures/Interface/Items/AlcoholLevel/Alcohol50", typeof(Texture2D));
			else if(GameManager.alcohol >= 40 && GameManager.alcohol < 50)
				alcoholTexture = (Texture2D) Resources.Load("Textures/Interface/Items/AlcoholLevel/Alcohol40", typeof(Texture2D));
			else if(GameManager.alcohol >= 30 && GameManager.alcohol < 40)
				alcoholTexture = (Texture2D) Resources.Load("Textures/Interface/Items/AlcoholLevel/Alcohol30", typeof(Texture2D));
			else if(GameManager.alcohol >= 20 && GameManager.alcohol < 30)
				alcoholTexture = (Texture2D) Resources.Load("Textures/Interface/Items/AlcoholLevel/Alcohol20", typeof(Texture2D));
			else if(GameManager.alcohol >= 10 && GameManager.alcohol < 20)
				alcoholTexture = (Texture2D) Resources.Load("Textures/Interface/Items/AlcoholLevel/Alcohol10", typeof(Texture2D));
			else if(GameManager.alcohol >= 0 && GameManager.alcohol < 10)
				alcoholTexture = (Texture2D) Resources.Load("Textures/Interface/Items/AlcoholLevel/Alcohol0", typeof(Texture2D));
			GUI.Label(new Rect (width*0.21f,height*0.9f,width*0.06f,width*0.06f),"" + GameManager.alcohol,textStyle);
			
			GUI.Label(new Rect (width*0.385f,height*0.9f,width*0.06f,width*0.06f),"Lvl " + GameManager.level, textStyle);
			
			Texture2D ammunitionTexture = (Texture2D) Resources.Load("Textures/Interface/Items/Ammunition", typeof(Texture2D));
			GUI.DrawTexture (new Rect (width*0.59f,height*0.89f,width*0.06f,width*0.06f),ammunitionTexture);
			GUI.Label(new Rect (width*0.65f,height*0.9f,width*0.06f,width*0.06f),"" + GameManager.ammunition,textStyle);
			
			Texture2D scoreTexture = (Texture2D) Resources.Load("Textures/Interface/Items/Score", typeof(Texture2D));
			GUI.DrawTexture (new Rect (width*0.78f,height*0.89f,width*0.06f,width*0.06f),scoreTexture);
			GUI.Label(new Rect (width*0.845f,height*0.9f,width*0.06f,width*0.06f),"" + GameManager.score,textStyle);
			
			if(BoatManager.offside)
			{
				startTime +=Time.deltaTime;
				Texture2D offsideTexture = (Texture2D) Resources.Load("Textures/Interface/Items/Offside",typeof (Texture2D));
				GUI.DrawTexture (new Rect(0,0,width,height),offsideTexture);
			
				if(startTime>(timerOffside))
				{
					BoatManager.offside = false;
					startTime = 0;
				}
			}
		}
		
		//si on est entre deux niveaux
		if(GameManager.betweenLevel)
		{
			Screen.showCursor = true;
			if(GameManager.level == 5)
			{
				GameManager.win = true;
			}
			else if (GameManager.level != 5)
			{
				GameManager.timerLevel = 45.0f;
				Texture2D betweenLevelTexture = (Texture2D) Resources.Load("Textures/Interface/Fond/BetweenLevel", typeof(Texture2D));
				GUI.DrawTexture (new Rect (0,0,Screen.width,Screen.height),betweenLevelTexture);
				if(GUI.Button(new Rect (Screen.width*0.52f,Screen.height*0.715f,width*0.275f,height*0.255f),"",betweenLevelButton))
				{
					clicClic = true;
					GameManager.LevelInit();
				}
			}
		}
		
		//si on a perdu le niveau et que l'on a encore des vies
		if(GameManager.loose && GameManager.life !=0)
		{
			Screen.showCursor = true;
			GameManager.Loose();
			Texture2D looseTexture = (Texture2D) Resources.Load("Textures/Interface/Fond/Loose", typeof(Texture2D));
			GUI.DrawTexture (new Rect (0,0,Screen.width,Screen.height),looseTexture);
			if(GUI.Button(new Rect (Screen.width*0.52f,Screen.height*0.715f,width*0.275f,height*0.255f),"",looseButton))
			{
				clicClic = true;
				Screen.showCursor = false;
				GameManager.loose = false;
			}
		}
		
		//si on a perdu le jeu
		if(GameManager.gameOver)
		{
			Screen.showCursor = true;
			GameManager.timerLevel = 45.0f;
			Texture2D gameOvertexture = (Texture2D) Resources.Load("Textures/Interface/Fond/GameOver", typeof(Texture2D));
			GUI.DrawTexture (new Rect (0,0,Screen.width,Screen.height),gameOvertexture);
			if(GUI.Button(new Rect (Screen.width*0.52f,Screen.height*0.715f,Screen.width*0.275f,height*0.255f),"",gameOverButton))
			{
				clicClic = true;
				Screen.showCursor = false;
				Application.LoadLevel("Menu");
			}
		}
		
		//si on a gagné le jeu
		if(GameManager.win)
		{
			Screen.showCursor = true;
			GameManager.timerLevel = 45.0f;
			Texture2D winTexture = (Texture2D) Resources.Load("Textures/Interface/Fond/Win", typeof(Texture2D));
			GUI.DrawTexture (new Rect (0,0,Screen.width,Screen.height),winTexture);
			if(GUI.Button(new Rect (Screen.width*0.52f,Screen.height*0.715f,width*0.275f,height*0.255f),"",winButton))
			{
				clicClic = true;
				PlayerPrefs.SetInt("score1",GameManager.score);
				PlayerPrefs.Save();
				Screen.showCursor = false;
				Application.LoadLevel("Menu");
			}
		}
	}
}