using UnityEngine;
using System.Collections;

public class Items : MonoBehaviour
{
	public enum ITEM_TYPE
	{
		CHAMPI_ROUGE,
		CHAMPI_VERT,
		CHAMPI_BLEU,
		FLEUR,
		FEUILLE,
		OEUF,
		PIECE,
		BLOC,
		CARAPACE,
		FLEURGLACE,
		BANANE,
		
		ETOILE,
		BOUH,
		BOWSER
		
	};
	
	public ITEM_TYPE type;
	public GameObject item ;
	public Levels level;
	public Croix croix;
	public Menu menu;
	
	private static string idVerif1="";
	private static string idVerif2 = "";
	public string id ="";
	public GameObject bubules;
	
	
	/*private float timerBouh;
	private Vector3 posBouh;
	private Vector3 moveBouh;
	private bool bouhItem=false;*/
	
	//public static bool etoileItem=false;
	
	private int randNb;
	private Vector3 acceleration=new Vector3(0,0,0);
	private float posX=0.0f;
	private float posY=0.0f;
	public static GameObject gObj1=null;
	public static GameObject gObj2=null;
	private bool perdu = false;
	
	public static int score=0;
	
	public static int nombreCoup=0;
	
	
	
	
	
	
	
	
	
	
	float alarm;
	//float upTime = -1;
	
	public bool  up = false;
	
	void Start()
	{
		level = Levels.instance;
	}
	
	
	void Update () 
	{
		TranslateMe ();
		
	}
	
	
	
	
	void TranslateMe()
	{
		if ((up || item.transform.position.z > -35)/*&&bouhItem==false &&bowserItem==false&&etoileItem==false*/) {
			RotateMe ();
		}
		
		
		/*if (Time.time > alarm) {
						up = false;
						up2=false;
						//upTime = Time.time;
						alarm = Time.time + 5;

				}*/
	}
	
	
	
	void OnMouseDown() 
	{
		up = true;
		nombreCoup++;
		
		
		if (idVerif1 == "" && idVerif2 != "") {
			gObj1 = gameObject;
			if (gObj1.transform.position.z < -32) { //L'objet est il en haut?//Si non tout va bien
				
				idVerif1 = id;
				gObj1 = gameObject;
				
				
			} else {//S'il est en haut rien ne se passe
				gObj1 = null;
			}
			/*if(idVerif1=="B") //"B" correspond à l'ID du Boo
			{
				bouhAction ();
				idVerif1="";
			}*/
			
		}
		if (idVerif1 == "" && idVerif2 == "") {
			idVerif2 = id;
			gObj2 = gameObject;
		}
		/*if (idVerif2 == "B") {
			bouhAction ();
			idVerif2="";
				}*/
		
		if (idVerif1 != "" && idVerif2 != "") {
			Comparaison ();
		}
	}
	
	
	
	void RotateMe()
	{
		if (up)
		{
			if (item.transform.position.z < -21)
			{
				renderer.enabled = true;
				item.transform.Translate (new Vector3 (0, 0, 20 * Time.deltaTime));
				
			}
			if(perdu==true && item.transform.position.z>-28)
			{
				gObj1.GetComponent<Items>().up = false;
				gObj2.GetComponent<Items>().up = false;
				gObj1=null;
				gObj2=null;
				
				perdu=false;}
		}
		
		
		else
		{
			//upTime = -1;
			
			if (item.transform.position.z > -34 )
			{
				item.transform.Translate (new Vector3 (0, 0, -20 * Time.deltaTime));
				
			}
			else
			{
				renderer.enabled = false;
			}
		}
		item.transform.Rotate (new Vector3 (0, 0, 2));
	}
	
	void Comparaison()
	{
		
		if (idVerif1 == idVerif2/* && etoileItem==false*/) {
			
			//croix.TuyauVide();
			GameObject bubulesInst = Instantiate(bubules,new Vector3(gObj1.transform.position.x,gObj1.transform.position.y,gObj1.transform.position.z+10),Quaternion.identity) as GameObject;
			GameObject bubulesInst2 = Instantiate(bubules,gObj2.transform.position,Quaternion.identity) as GameObject;
			Destroy(gObj1,1);
			Destroy(gObj2,1);
			
			Destroy(bubulesInst,1.5f);
			Destroy(bubulesInst2,1.5f);
			
			
			score+=10;
			
		}
		
		else{
			perdu=true;	
			Handheld.Vibrate();
		}
		
		idVerif1 = "";
		idVerif2 = "";
		
	}		
	
	public void Clear()
	{
		for (int childIndex=0; childIndex<transform.childCount; childIndex++) {
			Transform child=transform.GetChild (childIndex);
			Destroy (child.gameObject);
		}
		
	}
	
	
	
	
}


