using UnityEngine;
using System.Collections;
/*
Samedi minuit apk ou webplayer
filezilla
ftp.kacem.fr
login : kacem-uni
mdp : unity2014
*/
public class Game : MonoBehaviour {

	public enum STATES
	{
		MENU,
		LOADING_IN_PROGRESS,
		PLAY,
		SCORE,
		UPGRADE,
		FIN	
	};
	
	public STATES state = STATES.MENU;
	public static Game instance;
	public Texture2D texturebatterie;
	public Texture2D textureEssence;
	public Texture2D[] texturesMenu;
	public GameObject playerPrefab;
	public GameObject cameraVictoire;
	public GameObject bouleMagique;
	//public GUIText debugText;
	public AudioClip bouleMagiqueSound;
	public Transform depart;
	public GUIStyle[] stylesMenu;
	public GameObject cameraMenu;

	public bool boost=false;
	public bool fly=false;
	public bool speed=false;
	
	/****************************************************************************/
	//Handheld.Vibrate();
	/****************************************************************************/
	public GUIStyle styleTexte;
	public Player playerScript;
	public float consoBatterie = 1.0f;
	private int scoreTotal;
	public float coefTempsTune;
	public Upgrade[] upgradeTab;
	private float delay=0.0f;
	private bool debloqueBouleMagique=false;
	void Start(){
		styleTexte.fontSize=setFontSize(20.0f);
		instance =this;
		//PlayerPrefs.SetInt("ScoreTotal",2000000);
		scoreTotal = PlayerPrefs.GetInt("ScoreTotal");
	//	cameraVictoire.enabled = false;
		Screen.fullScreen = true;
		initializeUpgrade();
	}
	
	static public Rect MyRect(float x, float y, float sizeX, float sizeY){
		return new Rect(
			x * Screen.width/800.0f,
			y * Screen.height/480.0f,
			sizeX * Screen.width/800.0f,
			sizeY * Screen.height/480.0f
		);
	}
	public int setFontSize(float x){
		return (int)(x * (float)((float)Screen.width/800.0f));
	} 
	
	void Update(){
		
		//Debug.Log("Score" + scoreTotal);
		switch (state)
		{
		case STATES.PLAY:
		{
			if(cameraMenu.activeSelf){
				cameraMenu.SetActive(false);
			
			}
			if (Player.instance.batterie==0){
				
				scoreTotal += Player.instance.score;
				scoreTotal += (int)((Time.time-delay)*coefTempsTune*3.0f);
				PlayerPrefs.SetInt("ScoreTotal",scoreTotal);
				CancelInvoke();
				Clear ();
				state = STATES.MENU;
				
			}
			break;
		}
		case STATES.MENU:
		{
			
			if(!cameraMenu.activeSelf){
				cameraMenu.SetActive(true);
				audio.Stop();
			}
			if(cameraVictoire.activeSelf)
				cameraVictoire.SetActive(false);
			break;
		}
		
		case STATES.UPGRADE:
		{
			if(!cameraMenu.activeSelf)
				cameraMenu.SetActive(true);
			
			break;
		}
		case STATES.LOADING_IN_PROGRESS:
		{
			break;
		}
		case STATES.SCORE:
		{
			
			break;
		}
		}
	
	}
	void OnGUI()
	{
		switch (state)
		{
			case STATES.PLAY:
			{
				// score !!!
				GUI.Label(MyRect(600.0f,5.0f,100.0f,20.0f),"Score : " + Player.instance.score, styleTexte);
				GUI.DrawTexture (MyRect(30.0f,10.0f,10.0f * Player.instance.batterie,10.0f), texturebatterie);
				if(Player.instance.TestBoost() && Player.instance.essence>0.0f)
					GUI.DrawTexture (MyRect(30.0f,25.0f,10.0f * Player.instance.essence,10.0f), textureEssence);
				
                break;
            }
            case STATES.MENU:
			{
			
				GUI.Label(MyRect(600.0f,210.0f,100.0f,200.0f),"Score Total : \n\n" + scoreTotal,styleTexte);
				if(GUI.Button(MyRect(310, 200.0f, 200, 50.0f), "", stylesMenu[3])){
					PlayerPrefs.SetInt("ScoreTotal",0);
					scoreTotal=0;
					//Mettre à jour les coefs des upgrades
					foreach(Upgrade  up in upgradeTab){
						up.niveauUpgrade=0;
						PlayerPrefs.SetInt(up.name,0);
					}
					
                    
                }
                if(GUI.Button( MyRect(310, 260.0f, 200, 50.0f), "", stylesMenu[0])){
					state=STATES.LOADING_IN_PROGRESS;
					initializeGame();
					
                }
				if(GUI.Button( MyRect(310, 320.0f, 200, 50.0f), "", stylesMenu[1])){
					state=STATES.UPGRADE;
                    
                }
				if(GUI.Button(MyRect(310, 380.0f, 200, 50.0f), "", stylesMenu[2])){
					Application.Quit();
					
				}
				
                break;
            }
            case STATES.LOADING_IN_PROGRESS:
	         {
			GUI.Label( MyRect(0, 0, 400, 300), "Loading in progress...");
	             break;
	         }
			case STATES.SCORE:
			{
			GUI.Label( MyRect(0, 0, 400, 300), "SCORE...");
				GUI.Label(MyRect(200.0f,10.0f,100.0f,200.0f),"Score Total : " + scoreTotal);
			if(GUI.Button( MyRect(200, 35.0f, 300, 30f), "MENU")){
					state=STATES.MENU;
					
				}
			if(GUI.Button( MyRect(200, 80.0f, 300, 30f), "PLAY")){
					state=STATES.LOADING_IN_PROGRESS;
					initializeGame();
				
				}
				break;
			}
			
			case STATES.UPGRADE:
			{
			int nbUpgrades=0;
			int nbUpgradesTotal=0;
			foreach(Upgrade up in upgradeTab){
				nbUpgrades += up.niveauUpgrade;
				nbUpgradesTotal += up.prixUpgrade.Length;
			}
//			Debug.Log("Up : "+nbUpgrades);
			if(nbUpgrades==nbUpgradesTotal){
				Debug.Log("Boule magique debloquee !!!");
				GUI.Label( MyRect(250, 430.0f, 300, 30f),"Vous avez débloqué la BOULE MAGIQUE !!!\nEssayez de la trouver pour finir le jeu",styleTexte);
				if(!debloqueBouleMagique){
					debloqueBouleMagique=true;
				}
			}
			if(GUI.Button( MyRect(600, 430.0f, 300, 30f), texturesMenu[3],styleTexte)){
					state=STATES.MENU;
					
			}
			if(GUI.Button( MyRect(50, 160.0f, 100, 100f),upgradeTab[0].etatUpgrade[upgradeTab[0].niveauUpgrade],styleTexte)){
				if(upgradeTab[0].niveauUpgrade<5)
					acheterUpgrade(0);
			}
			if(upgradeTab[0].niveauUpgrade<5)
				GUI.Label( MyRect(50, 230.0f, 100, 100f),upgradeTab[0].prixUpgrade[upgradeTab[0].niveauUpgrade]+"",styleTexte);
				
			if(GUI.Button( MyRect(200, 160.0f, 100, 100f),upgradeTab[1].etatUpgrade[upgradeTab[1].niveauUpgrade],styleTexte)){
				if(upgradeTab[1].niveauUpgrade<5 && upgradeTab[4].niveauUpgrade==1)	
					acheterUpgrade(1);
			}
			if(upgradeTab[1].niveauUpgrade<5)
				GUI.Label( MyRect(200, 230.0f, 100, 100f),  upgradeTab[1].prixUpgrade[upgradeTab[1].niveauUpgrade]+"",styleTexte);
				
			if(GUI.Button( MyRect(50, 320.0f, 100, 100f),upgradeTab[2].etatUpgrade[upgradeTab[2].niveauUpgrade],styleTexte)){
				if(upgradeTab[2].niveauUpgrade<5)	
					acheterUpgrade(2);
			}
			if(upgradeTab[2].niveauUpgrade<5)	
				GUI.Label( MyRect(50, 390.0f, 100, 100f), upgradeTab[2].prixUpgrade[upgradeTab[2].niveauUpgrade]+"",styleTexte);
			
				
			if(GUI.Button( MyRect(200, 320.0f, 100, 100f),upgradeTab[3].etatUpgrade[upgradeTab[3].niveauUpgrade],styleTexte)){
				if(upgradeTab[3].niveauUpgrade<5)	
					acheterUpgrade(3);
					
			}
			if(upgradeTab[3].niveauUpgrade<5)
				GUI.Label( MyRect(200, 390.0f, 100, 100f),  upgradeTab[3].prixUpgrade[upgradeTab[3].niveauUpgrade]+"",styleTexte);
			
			
			if(GUI.Button( MyRect(500, 160.0f, 100, 100f),upgradeTab[4].etatUpgrade[upgradeTab[4].niveauUpgrade],styleTexte)){
				if(upgradeTab[4].niveauUpgrade<1){
					acheterUpgrade(4);
					
				}
				
			}	
			
			if(upgradeTab[4].niveauUpgrade<1)
				GUI.Label( MyRect(500, 230.0f, 100, 100f),  upgradeTab[4].prixUpgrade[upgradeTab[4].niveauUpgrade]+"",styleTexte);	
			if(GUI.Button( MyRect(650, 160.0f, 100, 100f),upgradeTab[5].etatUpgrade[upgradeTab[5].niveauUpgrade],styleTexte)){
				if(upgradeTab[5].niveauUpgrade<1){
					acheterUpgrade(5);
					
				}
				
				
			}
			if(upgradeTab[5].niveauUpgrade<1)
				GUI.Label( MyRect(650, 230.0f, 100, 100f),  upgradeTab[5].prixUpgrade[upgradeTab[5].niveauUpgrade]+"",styleTexte);	
			if(GUI.Button( MyRect(500, 320.0f, 100, 100f),upgradeTab[6].etatUpgrade[upgradeTab[6].niveauUpgrade],styleTexte)){
				if(upgradeTab[6].niveauUpgrade<1){
					acheterUpgrade(6);
					
				}
				
				
			}
			if(upgradeTab[6].niveauUpgrade<1)
				GUI.Label( MyRect(500, 390.0f, 100, 100f),  upgradeTab[6].prixUpgrade[upgradeTab[6].niveauUpgrade]+"",styleTexte);	
			
				
				
				break;
			}
			
			case STATES.FIN:
			{
			if(GUI.Button( MyRect(200, 10.0f, 300, 30f), "MENU",styleTexte)){
					state=STATES.MENU;
				}
				break;
			}
        }
    }
    void initializeGame(){
    
		GameObject clonePlayer = Instantiate(playerPrefab, depart.position,  Quaternion.identity) as GameObject;
		clonePlayer.name=playerPrefab.name;
		
		initializeUpgrade();
		
		cameraVictoire.SetActive(false);
		Level.instance.GenererItems();
		InvokeRepeating("consommationBatterie", 0.5f, consoBatterie);
		state=STATES.PLAY;
		delay=Time.time;
		int nbUpgrades=0;
		int nbUpgradesTotal=0;
		foreach(Upgrade up in upgradeTab){
			nbUpgrades += up.niveauUpgrade;
			nbUpgradesTotal += up.prixUpgrade.Length;
		}
		//			Debug.Log("Up : "+nbUpgrades);
		if(nbUpgrades==nbUpgradesTotal)
			Level.instance.GenererBouleMagique();
		Debug.Log("Delay : " +delay);
		
    }
    
    void initializeUpgrade(){
    
    	foreach ( Upgrade u in upgradeTab){
    		Debug.Log("InitializeUpdate : " + u.state);
			u.niveauUpgrade=PlayerPrefs.GetInt(u.name);
			switch (u.state)
			{
				
				case Upgrade.STATES.TEMPS_TUNE:
				{	
					coefTempsTune = Mathf.Pow(2.0f,u.niveauUpgrade);
					break;
				}
				case Upgrade.STATES.SPAWN_PILE:
				{	
					
					u.coeff = Mathf.Pow(1.45f,u.niveauUpgrade);
					break;
				}
				case Upgrade.STATES.SPAWN_ESSENCE:
				{	
					u.coeff = Mathf.Pow(1.2f,u.niveauUpgrade);
					break;
				}
				case Upgrade.STATES.SPAWN_POUSSIERE:
				{	
					u.coeff = Mathf.Pow(1.3f,u.niveauUpgrade);
					break;
				}
				case Upgrade.STATES.BOOST:
				{	
					if (u.niveauUpgrade==1){
						boost = true;
					}
					else{
						boost = false;
					}
					break;
				}
				case Upgrade.STATES.FLY:
				{	
					if (u.niveauUpgrade==1){
						fly = true;
					}
					else{
						fly = false;
					}
					break;
				}
				case Upgrade.STATES.SPEED:
				{	
					if (u.niveauUpgrade==1){
						speed = true;
					}
					else{
						speed = false;
					}
					break;
				}
				
			}
    	
    	}
    
    
    }
    
   	void consommationBatterie(){
   		
		
   		Player.instance.batterie--;
   	
   	}
	
   	void acheterUpgrade(int numUpgrade){
   	
   		
		if (scoreTotal >= upgradeTab[numUpgrade].prixUpgrade[upgradeTab[numUpgrade].niveauUpgrade]){
			scoreTotal -= upgradeTab[numUpgrade].prixUpgrade[upgradeTab[numUpgrade].niveauUpgrade];
			upgradeTab[numUpgrade].niveauUpgrade++;
			PlayerPrefs.SetInt(upgradeTab[numUpgrade].name,upgradeTab[numUpgrade].niveauUpgrade);
			PlayerPrefs.SetInt("ScoreTotal",scoreTotal);
			cameraMenu.audio.Play();
   		} 
		
		
   	}
   	
   	public void Win(){
   		
		cameraVictoire.SetActive(true);
		//bouleMagique.animation.Play("AnimationBouleMagique");
		bouleMagique.animation.Play();
		
		audio.PlayOneShot(bouleMagiqueSound);
		Clear();
		state = STATES.FIN;
		
   		
   		
   	}
   	
	public void Clear(){
		
		Destroy(GameObject.Find("conteneur"));
		Destroy(Player.instance.gameObject);
		
	}
    
	
}
