using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

	public float vitesseRotation;
	public float vitesseDeplacements;
	public float puissanceBoost;
	public float puissanceVol;
	public int score=0;
	public Texture2D[] texturesButton;
    public int batterie;
	public float essence;
	
	public static Player instance;
	public GUIStyle styleButtons;
	public GameObject meshBoost;
	public GameObject meshFly;
	public GameObject meshSpeed;
	public GameObject flammes;
	public GameObject[] helices;
	
    void Awake(){
		instance =this;
	}
	
	void Start(){
		if (Game.instance.boost){
			meshBoost.SetActive(true);
			puissanceBoost *=2;
		
		}
		if (Game.instance.fly){
			meshFly.SetActive(true);
			puissanceVol = 2.0f;
		}
		if (Game.instance.speed){
			meshSpeed.SetActive(true);
			vitesseDeplacements *=2;
			vitesseRotation *=2;
		}
		
	
	}
	public bool TestBoost(){
		bool boost =false;
		foreach(Upgrade up in Game.instance.upgradeTab){
			if(up.name=="Boost" && up.niveauUpgrade==1)
				boost = true;
			
		}
		return boost;
	}
	void FixedUpdate()
	{
		bool animHelices =false;
		bool boolFlammes =false;
		if (Input.GetKey(KeyCode.Q))
		{
			this.transform.Rotate(Vector3.up,- Time.deltaTime*vitesseRotation*3.0f);
		}
		if (Input.GetKey(KeyCode.D))
		{
			this.transform.Rotate(Vector3.up, Time.deltaTime*vitesseRotation*3.0f);
		}
		if (Input.GetKey(KeyCode.Z))
		{
			transform.Translate(Vector3.back * Time.deltaTime*vitesseDeplacements);
        }
		if (Input.GetKey(KeyCode.S))
		{
			transform.Translate(Vector3.forward * Time.deltaTime*vitesseDeplacements);
        }
		if (Input.GetKey(KeyCode.F) && essence>0.0f && Game.instance.boost)
		{
			transform.rigidbody.AddForce(puissanceBoost * -transform.forward, ForceMode.Impulse);
			essence-=0.1f;
			boolFlammes=true;
		}
		if (Input.GetKey(KeyCode.Space) && transform.position.y<4.4f && Game.instance.fly)
		{
			animHelices=true;
			transform.rigidbody.AddForce(puissanceVol * Vector3.up, ForceMode.Impulse);
			Debug.Log(transform.position+"");
		}
		if(animHelices && !helices[0].animation.isPlaying){
			helices[0].animation.Play();
			helices[1].animation.Play();
			helices[2].animation.Play();
		}
		if(!animHelices && helices[0].animation.isPlaying){
			helices[0].animation.Stop();
			helices[1].animation.Stop();
			helices[2].animation.Stop();
		}
		if(boolFlammes)
			flammes.SetActive(true);
		else
			flammes.SetActive(false);
        
    }
    void OnGUI(){
		bool animHelices =false;
		bool boolFlammes =false;
		Graphics.DrawTexture(Game.MyRect(70.0f, 340.0f, 50, 50f), texturesButton[0]);
		Graphics.DrawTexture(Game.MyRect(70.0f, 420.0f, 50, 50f), texturesButton[1]);
		Graphics.DrawTexture(Game.MyRect(10.0f, 380.0f, 50, 50f), texturesButton[2]);
		Graphics.DrawTexture(Game.MyRect(130.0f, 380.0f, 50, 50f), texturesButton[3]);
		if(Game.instance.fly)
			Graphics.DrawTexture(Game.MyRect(660.0f, 380.0f, 50, 50f), texturesButton[4]);
		if(Game.instance.boost)
			Graphics.DrawTexture(Game.MyRect(730.0f, 380.0f, 50, 50f), texturesButton[5]);
		foreach ( Touch t in Input.touches )
		{
			//GUITexture haut= new GUITexture(Game.MyRect(70.0f, 340.0f, 50, 50f),texturesButton[0]);
		
			Vector2 vec = t.position;
			vec.y = Screen.height - vec.y;
			
			if(Game.MyRect(70.0f, 340.0f, 50, 50f).Contains(vec))
				transform.Translate(Vector3.back * Time.deltaTime*(vitesseDeplacements/4.0f));
			if(Game.MyRect(70.0f, 420.0f, 50, 50f).Contains(vec))
				transform.Translate(Vector3.forward * Time.deltaTime*(vitesseDeplacements/4.0f));
			if(Game.MyRect(10.0f, 380.0f, 50, 50f).Contains(vec))
				this.transform.Rotate(Vector3.up,- Time.deltaTime*vitesseDeplacements*20.0f);
			if(Game.MyRect(130.0f, 380.0f, 50, 50f).Contains(vec))
				this.transform.Rotate(Vector3.up, Time.deltaTime*vitesseDeplacements*20.0f);
			if(Game.MyRect(660.0f, 380.0f, 50, 50f).Contains(vec) && transform.position.y<4.4f && Game.instance.fly){
				animHelices=true;
				transform.rigidbody.AddForce(3.1f * Vector3.up, ForceMode.Impulse);
				Debug.Log(transform.position+"");
			}
			
			if(Game.MyRect(730.0f, 380.0f, 50, 50f).Contains(vec) && Game.instance.boost && essence>0.0f){
				transform.rigidbody.AddForce(puissanceBoost * -transform.forward, ForceMode.Impulse);
				essence-=0.1f;
				boolFlammes=true;
			}
			if(animHelices && !helices[0].animation.isPlaying){
				helices[0].animation.Play();
				helices[1].animation.Play();
				helices[2].animation.Play();
			}
			if(!animHelices && helices[0].animation.isPlaying){
				helices[0].animation.Stop();
				helices[1].animation.Stop();
				helices[2].animation.Stop();
			}
			if(boolFlammes)
				flammes.SetActive(true);
			else
				flammes.SetActive(false);
			
		}
    }
    void OnTriggerEnter(Collider collision) {
    	Debug.Log("Col : "+collision.gameObject.name);
		if(Level.instance.dicoPoints.ContainsKey(collision.gameObject.name)){
			// Double check nom !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			Destroy(collision.gameObject);
			if(collision.gameObject.name=="Poussiere"){
				score+=10;
			}
			
		}
		if(collision.gameObject.name=="Batterie" && batterie<50){
			batterie+=5;
			Destroy(collision.gameObject);	
		}
		if(collision.gameObject.name=="Essence" && essence<50){
			essence+=5;
			Destroy(collision.gameObject);	
		}
		
		if(collision.gameObject.name=="BouleMag2"){
			
			Destroy(collision.gameObject);	
			Game.instance.Win();
			
		}
	}
	
	
	
	
    
}
