using UnityEngine;
using System.Collections;

public class GameLogig : MonoBehaviour {

    public item[] recipientes;
    public item[] liquidos1;
    public item[] elementosP1;
    public item[] liquidos2;
    public item[] elementosP2;
    public item[] liquidos3;
    public item[] elementosP3;
    public item[] elementosS1;
    public item[] elementosS2;
    public item[] elementosS3;
	public item[] elementosS4;
	public item[] elementosS5;
	public item[] elementosS6;
	public item[] elementosS7;
	public item[] elementosS8;
	public item[] elementosS9;
    private item[] objetivo1;
	public GameObject[] posit;
    public static int clientes = 0;
    private int correcta = 0;
    private GameObject A;
    private GameObject B;
    private GameObject C;
	private GameObject D;
    private GameObject jugadorSolution;
	private bool encontrado;
	private bool encontrado2;
	private bool encontrado3;
	private bool encontrado4;
	private item[][] liquidosArrays;
	private item[][] elementosPArrays; 
	private item[][] elementosSArrays;
	private int objetivoindice;
	private int objetivoindice2;
	private bool keysenable = true;
	public Efects correcto;
	public Efects incorrecto;
	public AudioSource audioSourceSolution;

	public AudioSource lose;

    // Use this for initialization
    void Start () {

		elementosPArrays = new item[][]{ elementosP1, elementosP2, elementosP3};
		liquidosArrays= new item[][] { liquidos1, liquidos2, liquidos3}; 
		elementosSArrays = new item[][]{ elementosS1, elementosS2, elementosS3, elementosS4, elementosS5, elementosS6, elementosS7, elementosS8, elementosS9 };
        clientes = 0;//Hacer un ranking en la pantalla final.
        objetivo1 = new item[4];
        generateObjective();

    }
	
    public void generateObjective()
    {
		objetivoindice = Random.Range (0, recipientes.Length);


        correcta = 0;
		encontrado = false;
		encontrado2 = false;
		encontrado3 = false;
		encontrado4 = false;
        Destroy(jugadorSolution);
        //GameObject.Destroy(jugadorSolution);
        jugadorSolution = new GameObject("solucion");


        objetivo1[0] = recipientes[objetivoindice];
        objetivo1[1] = liquidosArrays[objetivoindice][Random.Range(0, liquidosArrays[objetivoindice].Length)];
		objetivoindice2 = Random.Range (0, elementosPArrays [objetivoindice].Length);
        objetivo1[2] = elementosPArrays[objetivoindice][objetivoindice2];

        //objetivoelemento = Random.Range(0, elementosPArrays[objetivoindice].Length);

		objetivo1[3] = elementosSArrays[(objetivoindice*3)+objetivoindice2][objetivoindice];

         A = (Instantiate(objetivo1[0], new Vector3(-5,3,0), Quaternion.identity) as item).gameObject;
         B = (Instantiate(objetivo1[1], new Vector3(-5,3,-1), Quaternion.identity) as item).gameObject;
         C = (Instantiate(objetivo1[2], new Vector3(-5,3,-2), Quaternion.identity) as item).gameObject;
		 D = (Instantiate(objetivo1[3], new Vector3(-5,3,-3), Quaternion.identity) as item).gameObject;


		posit [0].SetActive (true);
		Debug.Log(objetivo1[0].key + "," + objetivo1[1].key + "," + objetivo1[2].key + "," + objetivo1[3].key);
    }
	// Update is called once per frame
	void Update () {
		bool primerError = false;

		if (Input.GetKeyDown(KeyCode.Colon))
        {
            if (Time.timeScale == 1)
            {
                Time.timeScale = 0;
            }
            else
            {
                Time.timeScale = 1;
            }

        }

        if(Time.timeScale == 0 || keysenable == false)
        {
            return;
        }

        if (Input.anyKeyDown && Input.GetKey(KeyCode.Colon) == false)
        {
            bool itemfound = false;


			{
				int i = 0;
				if (encontrado == false) {
					i = 0;
				} else if (encontrado2 == false) {
					i = 1;
				} else if (encontrado3 == false) {
					i = 2;
				} else if (encontrado4 == false) {
					i = 3;
				}
				posit[i].SetActive(false);
				//for (int i = 0; i < 3; i++)
				//i??

				if (objetivo1 [i] != null && Input.GetKeyDown (objetivo1 [i].key) == true) {
					AudioSource a = objetivo1 [i].GetComponent<AudioSource> ();
					audioSourceSolution.clip = a.clip;
					if (a != null)
						audioSourceSolution.PlayOneShot (a.clip);

					objetivo1 [i] = null;
					correcta++;

					if(i+1 < 4)
						posit[i+1].SetActive(true);

					itemfound = true;
					primerError = true;
					Debug.Log ("Correcto");
				}
			
            

	            if (itemfound == false)
	            {
					lose.Play ();

	                Debug.Log("Incorrecto");
	                //Dejar tiempo. Sin parar el tiempo global.
	                Debug.Log("Antes de parar");
					incorrecto.Explosion(transform.position);
	                StartCoroutine(tiempoEspera());
					primerError = false;
	                

	            }

			}

            if (correcta == 4)
            {
                clientes++;
				correcto.Explosion(transform.position);
				StartCoroutine (tiempoEspera ());
				/*
                GameObject.Destroy(A);
                GameObject.Destroy(B);
                GameObject.Destroy(C);
                generateObjective();*/
                
            }
        }


        //item[][] items = {recipientes, liquidos1, liquidos2, liquidos3, elementosP1, elementosP2,elementosP3};
        //foreach (item[] itemArray in items)
		if (encontrado == false && primerError == true) {
			for (int i = 0; i < recipientes.Length; i++) {
				if (Input.GetKeyDown (recipientes [i].key)) {
					encontrado = true;
					GameObject G = Instantiate (recipientes [i].gameObject, new Vector3 (0, 2, 0), Quaternion.identity) as GameObject;
					G.transform.localScale = new Vector3(1.5f, 1.5f,1.0f);
					G.transform.parent = jugadorSolution.transform;
				}
			}
		}
		else if (encontrado2 == false && primerError == true) {
			for (int i = 0; i < liquidosArrays[objetivoindice].Length; i++) {
				if (Input.GetKeyDown (liquidosArrays[objetivoindice][i].key)) 
				{
					encontrado2 = true;
					GameObject G = Instantiate (liquidosArrays[objetivoindice][i].gameObject, new Vector3 (0, 2, -1), Quaternion.identity) as GameObject;
					G.transform.localScale = new Vector3(1.5f, 1.5f,1.0f);
					G.transform.parent = jugadorSolution.transform;
				}
			}
		}
		else if (encontrado3 == false && primerError == true) {
			for (int i = 0; i < elementosPArrays[objetivoindice].Length; i++) {
				if (Input.GetKeyDown (elementosPArrays[objetivoindice][i].key)) 
				{
					encontrado3 = true;
					GameObject G = Instantiate (elementosPArrays[objetivoindice][i].gameObject, new Vector3 (0, 2, -2), Quaternion.identity) as GameObject;
					G.transform.localScale = new Vector3(1.5f, 1.5f,1.0f);
					G.transform.parent = jugadorSolution.transform;
				}
			}
		}
		else if (encontrado4 == false && primerError == true) {
			for (int i = 0; i < elementosSArrays[objetivoindice].Length; i++) {
				if (Input.GetKeyDown (elementosSArrays[objetivoindice][i].key))
				{
					encontrado4 = true;
					GameObject G = Instantiate (elementosSArrays[(objetivoindice*3)+objetivoindice2][i].gameObject, new Vector3 (0, 2, -3), Quaternion.identity) as GameObject;
					G.transform.localScale = new Vector3(1.5f, 1.5f, 1.0f);
					G.transform.parent = jugadorSolution.transform;
				}
			}
		}
       }
        
    IEnumerator tiempoEspera()
    {
		keysenable = false;
        yield return new WaitForSeconds(0.5f);
		keysenable = true;
		GameObject.Destroy(A);
        GameObject.Destroy(B);
        GameObject.Destroy(C);
		GameObject.Destroy (D);

        Debug.Log("funciona");
        generateObjective();
    }
}




