using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;

public class FabioController : MonoBehaviour {
	public GameObject mainCamera;
	public float acercarPersonaje = -1f;
	public float max = 4.0f;
	public float min = 2.5f;
	public GameObject fabio;
	public float velocidad = 3.0f;
	Animator anim;
	Camera camera;
	public Sprite[] botones;
	public GameObject tabla;
	public int minCharAmount;
	public int maxCharAmount;
	private string myString;
	public string glyphs;
	private int estado;
	private int estado2;
	private int estado3;
	private int estado4;
	public float lastPress = 0.0f; 
	public float keyPressTimer = 3.0f;
	private string cur = ""; //jugada
	public Sprite grito2;
	public Sprite pareja2;
	public Sprite picasso2;
	public Sprite grt1;
	public Sprite prj1;
	public Sprite pcss1;
	private string tag ="";
	//para el audio 
	public AudioSource fondo;
	public AudioClip grito;
	public AudioClip pareja;
	public AudioClip amorfo;
	public AudioClip fail;
	public AudioClip fondoclip;
	public AudioClip final;
	private bool fallo;
	private bool dificultad;
	private float inicio = 0.0f;
	public float tiempofinal = 10.0f;

	void Awake(){
		estado = 1;
		estado2 = 1;
		estado3 = 1;
		estado4 = 1;
		verTabla (false, "npc");
		verTabla (false, "npcpareja");
		verTabla (false, "npcpicasso");
		verTabla (false, "final");
		/*Sprite boton01 = botones[4];
		//botones = Resources.LoadAll<Sprite>("btn");
		GameObject btn1 = GameObject.Find("btn1");
		btn1.GetComponent<SpriteRenderer>().sprite = boton01;*/

		//print (randomCombo());
		//print (randomCombo());
	}

	// Update is called once per frame
	void Update ()
	{

		float translation = Input.GetAxis ("Horizontal") * velocidad;

		if (Input.GetAxis ("Horizontal") > 0) {
			fabio.transform.Translate (Vector2.right * translation * Time.deltaTime);
			anim.SetBool ("caminar",true);
		}else if (Input.GetAxis ("Horizontal") < 0) {
			fabio.transform.Translate (Vector2.right * translation * Time.deltaTime);
			anim.SetBool ("caminaratras",true);
		}else if (Input.GetButton ("LB")){
			alejarCamara ();
		}else{
			anim.SetBool ("caminar",false);
			anim.SetBool ("caminaratras",false);
		}


	

		if (estado == 2 || estado2 == 2 || estado3 == 2 || estado4 == 2) {
			
			if (tiempofinal>0) {
				tiempofinal -= Time.deltaTime;

			} else {
				print (tag);
				verTabla (false, tag);
				alejarCamara ();
				tiempofinal = 10f;
				if (ScoreManager.score <= 30 || ScoreManager.score >= 70)
					tiempofinal = 5.0f;
				switch (tag) {
				case "npc":
					estado = 1;
					break;
				case "npcpareja1":
					estado2 = 1;
					break;
				case "npcpicasso":
					estado3 = 1;
					break;
				case "final":
					estado4 = 1;
					break;
				}
			}

			if(Input.GetButtonDown ("W"))CheckKey("w");
			if(Input.GetButtonDown ("A"))CheckKey("a");
			if(Input.GetButtonDown ("V"))CheckKey("v");
			if(Input.GetButtonDown ("E"))CheckKey("e");
			if(Input.GetButtonDown ("S"))CheckKey("s");
		}

		if (estado4 == 2) {
			/*if (Input.GetButtonDown ("W")) {
				GameObject btn14 = GameObject.Find ("btn14");
				btn14.GetComponent<SpriteRenderer> ().color.a = 255;
			}
			if (Input.GetButtonDown ("A")) {
				GameObject btn24 = GameObject.Find ("btn24");
				btn24.GetComponent<SpriteRenderer> ().color.a = 255;
			}
			if (Input.GetButtonDown ("V")) {
				GameObject btn34 = GameObject.Find ("btn34");
				btn34.GetComponent<SpriteRenderer> ().color.
			}
			if (Input.GetButtonDown ("E")) {
				GameObject btn44 = GameObject.Find ("btn44");
				btn44.GetComponent<SpriteRenderer> ().color.a = 255;
			}
			if (Input.GetButtonDown ("S")) {
				GameObject btn54 = GameObject.Find ("btn54");
				btn54.GetComponent<SpriteRenderer> ().color.a = 255;
			}*/
		}


	}

	// Use this for initialization
	void Start () {
		anim = GetComponent<Animator> ();
		camera = mainCamera.GetComponent<Camera> ();
		cambiarEnabled ("button_question", false);

	}

	void acercarCamara(){
		float fTime = 0.0f;
		float fLerp = 0.0f;
		while (fLerp <= 1f){
			fLerp = fTime * 1f;
			camera.orthographicSize = camera.orthographicSize - fLerp;
			fTime += 1f * Time.deltaTime;
			mainCamera.transform.position = new Vector3 (0.0f, acercarPersonaje, -10f);
			if (camera.orthographicSize < min) {
				camera.orthographicSize = min; // min size
			}
		}
		//fondo.Pause ();
	}



IEnumerator Example() {
	print(Time.time);
	if (fallo)
		yield return new WaitForSeconds(1f);
	fondo.clip = fondoclip;
	fondo.Play ();
	print(Time.time);
}

	void alejarCamara(){
		float fTime = 0.0f;
		float fLerp = 0.0f;
		while (fLerp <= 1f){
			fLerp = fTime * 1f;
			camera.orthographicSize = camera.orthographicSize + fLerp;
			fTime += 100f * Time.deltaTime;
			mainCamera.transform.position = new Vector3 (0.0f, 0.0f, -10f);
			if (camera.orthographicSize > max) {
				camera.orthographicSize = max; // Max size
			}
		}

		StartCoroutine (Example ()); 
				


	}

	void OnTriggerStay2D(Collider2D colision){
		
		if (colision.gameObject.CompareTag ("npc")) {
			if (estado == 1 ) {
				if (Input.GetButton ("Start")) {
					tag = "npc";
					acercarCamara ();
					estado = 2; //Cantando
					miniGame(tag);
					//			GameObject btn1 = GameObject.Find("btn1");
					//			btn1.GetComponent<SpriteRenderer>().sprite = botones[1];
				}
			}
			//show minigame
		}else if (colision.gameObject.CompareTag ("npcpareja")) {
			if (estado2 == 1 ) {
				if (Input.GetButton ("Start")) {
					tag = "npcpareja";
					acercarCamara ();
					estado2 = 2; //Cantando
					miniGame(tag);
					//			GameObject btn1 = GameObject.Find("btn1");
					//			btn1.GetComponent<SpriteRenderer>().sprite = botones[1];
				}
			}
			//show minigame
		}else if (colision.gameObject.CompareTag ("npcpicasso")) {
			if (estado3 == 1 ) {
				if (Input.GetButton ("Start")) {
					tag = "npcpicasso";
					acercarCamara ();
					estado3 = 2; //Cantando
					miniGame(tag);
					//			GameObject btn1 = GameObject.Find("btn1");
					//			btn1.GetComponent<SpriteRenderer>().sprite = botones[1];
				}
			}
			//show minigame
		}else if (colision.gameObject.CompareTag ("final")) {
			if (estado4 == 1 ) {
				if (Input.GetButton ("Start")) {
					tag = "final";
					acercarCamara ();
					estado4 = 2; //Cantando
					miniGame(tag);
					//			GameObject btn1 = GameObject.Find("btn1");
					//			btn1.GetComponent<SpriteRenderer>().sprite = botones[1];
				}
			}
			//show minigame
		}
	}

	void OnTriggerEnter2D(Collider2D colision)
	{
		if (estado==1 )
		cambiarEnabled ("button_question", true);
		if (estado2 == 1)
			cambiarEnabled ("button_question1", true);
		if (estado3 == 1) 
			cambiarEnabled ("button_question2", true);
	}

	void OnTriggerExit2D(Collider2D colision)
	{
		cambiarEnabled ("button_question", false);
		cambiarEnabled ("button_question1", false);
		cambiarEnabled ("button_question2", false);
	}


	void cambiarEnabled(string name, bool cambio){
		GameObject objeto = GameObject.Find (name);
		Renderer render = objeto.GetComponent<Renderer> ();
		render.enabled = cambio;
	}

	string randomCombo(){
		myString = "";
		int charAmount = Random.Range(minCharAmount, maxCharAmount); //set those to the minimum and maximum length of your string
		for(int i=0; i<charAmount; i++)
		{
			
			myString += glyphs[Random.Range(0, glyphs.Length)];
			if (tag == "final")
				myString = "waves";
		}
		return myString;
	}


	void miniGame(string tag){
		StartCoroutine (Tablas (tag));

		string combo = randomCombo();
		if (tag == "final")
			combo = "waves";
		pintarSprites (combo,tag);
		print (combo);
		cur = "";

		//set sprites

		//print (cur.Length);
	}



	void CheckKey(string k){
		if(cur.Length<7){
			cur+=k;
		} else {
			//restar puntos

			cur=k;
		}
		lastPress += Time.deltaTime;
		print (cur);
		if (cur.Length == myString.Length)
			CheckCombos();

	}

	IEnumerator Dificultad() {
		yield return new WaitForSeconds(4f);
	}

	void CheckCombos(){
		if (cur == myString) {
			//puntos++
			if (tag == "npc") {
				estado = 3;
				GameObject grito = GameObject.Find ("grito");
				grito.GetComponent<SpriteRenderer> ().sprite = grito2;
			}
			if (tag == "npcpareja") {
				estado2 = 3;
				GameObject pareja1 = GameObject.Find ("pareja1");
				pareja1.GetComponent<SpriteRenderer> ().sprite = pareja2;
			}
			if (tag == "npcpicasso") {
				estado3 = 3;
				GameObject picasso1 = GameObject.Find ("picasso1");
				picasso1.GetComponent<SpriteRenderer> ().sprite = picasso2;
			}
			if (tag == "final") {
				ScoreManager.score = 100;
			}
			ScoreManager.score += (3 * cur.Length);
			if(ScoreManager.score>= 100)
				SceneManager.LoadScene ("endgame");
			fallo = false;
			tiempofinal = 10f;
			if (ScoreManager.score <= 30 || ScoreManager.score >= 70)
				tiempofinal = 5.0f;
		} else {
			//puntos--
			if (tag == "npc") {
				estado = 1;
			}
			if (tag == "npcpareja") {
				estado2 = 1;
			}
			if (tag == "npcpicasso")
				estado3 = 1;
			if (tag == "final") {
				ScoreManager.score = 1;
				estado = 1;
				estado2 = 1;
				estado3 = 1;
				estado4 = 1;
				GameObject grito = GameObject.Find ("grito");
				grito.GetComponent<SpriteRenderer> ().sprite = grt1;

				GameObject pareja1 = GameObject.Find ("pareja1");
				pareja1.GetComponent<SpriteRenderer> ().sprite = prj1;

				GameObject picasso1 = GameObject.Find ("picasso1");
				picasso1.GetComponent<SpriteRenderer> ().sprite = pcss1;


			}
			fallo = true;

			fondo.clip = fail;
			ScoreManager.score -= (5 * cur.Length);
			tiempofinal = 10f;
			if (ScoreManager.score <= 30 || ScoreManager.score >= 70)
				tiempofinal = 5.0f;
			fondo.Play ();

		}
		alejarCamara ();
		cur = "";
		verTabla (false, tag);
		//randomcombo.Substring (cur.length)
		// if (cur == randomcombo.Substring (0, cur.length))
		//	if (cur.length == randomcombo.length)
		//	sumar puntos 
		// cambiar estado
		// else
		// 	cur == ""
		//   puntos--
		//   marcarfallo 

	}

	void pintarSprites(string combo,string tag){ 
		if (tag == "npc"){
			fondo.clip = grito;
			fondo.Play ();

			if (combo.Length == 4) {
			GameObject btn5 = GameObject.Find ("btn5");
			btn5.GetComponent<SpriteRenderer> ().sprite = null;
			GameObject btn6 = GameObject.Find ("btn6");
			btn6.GetComponent<SpriteRenderer> ().sprite = null;
		}
		if (combo.Length == 5) {
			GameObject btn6 = GameObject.Find ("btn6");
			btn6.GetComponent<SpriteRenderer> ().sprite = null;
		}
			
		for (int i = 0; i < combo.Length; i++) {
			int indice = spriteIndex (combo [i]);

			Sprite boton = botones[indice];

			switch (i) {
			case 0: 
				GameObject btn1 = GameObject.Find ("btn1");
				btn1.GetComponent<SpriteRenderer> ().sprite = boton;
				break;
			case 1: 
				GameObject btn2 = GameObject.Find ("btn2");
				btn2.GetComponent<SpriteRenderer> ().sprite = boton;
				break;
			case 2: 
				GameObject btn3 = GameObject.Find ("btn3");
				btn3.GetComponent<SpriteRenderer> ().sprite = boton;
				break;
			case 3: 
				GameObject btn4 = GameObject.Find ("btn4");
				btn4.GetComponent<SpriteRenderer> ().sprite = boton;
				break;
			case 4: 
				GameObject btn5 = GameObject.Find ("btn5");
				btn5.GetComponent<SpriteRenderer> ().sprite = boton;
				break;
			case 5: 
				GameObject btn6 = GameObject.Find ("btn6");
				btn6.GetComponent<SpriteRenderer> ().sprite = boton;
				break;
			}

		}
		}

		if (tag == "npcpareja"){
			fondo.clip = pareja;
			fondo.Play ();
			if (combo.Length == 4) {
				GameObject btn52 = GameObject.Find ("btn52");
				btn52.GetComponent<SpriteRenderer> ().sprite = null;
				GameObject btn62 = GameObject.Find ("btn62");
				btn62.GetComponent<SpriteRenderer> ().sprite = null;
			}
			if (combo.Length == 5) {
				GameObject btn62 = GameObject.Find ("btn62");
				btn62.GetComponent<SpriteRenderer> ().sprite = null;
			}

			for (int i = 0; i < combo.Length; i++) {
				int indice = spriteIndex (combo [i]);

				Sprite boton = botones[indice];

				switch (i) {
				case 0: 
					GameObject btn12 = GameObject.Find ("btn12");
					btn12.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				case 1: 
					GameObject btn22 = GameObject.Find ("btn22");
					btn22.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				case 2: 
					GameObject btn32 = GameObject.Find ("btn32");
					btn32.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				case 3: 
					GameObject btn42 = GameObject.Find ("btn42");
					btn42.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				case 4: 
					GameObject btn52 = GameObject.Find ("btn52");
					btn52.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				case 5: 
					GameObject btn62 = GameObject.Find ("btn62");
					btn62.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				}

			}
		}

		if (tag == "npcpicasso"){
			fondo.clip = amorfo;
			fondo.Play ();
			if (combo.Length == 4) {
				GameObject btn53 = GameObject.Find ("btn53");
				btn53.GetComponent<SpriteRenderer> ().sprite = null;
				GameObject btn63 = GameObject.Find ("btn63");
				btn63.GetComponent<SpriteRenderer> ().sprite = null;
			}
			if (combo.Length == 5) {
				GameObject btn63 = GameObject.Find ("btn63");
				btn63.GetComponent<SpriteRenderer> ().sprite = null;
			}

			for (int i = 0; i < combo.Length; i++) {
				int indice = spriteIndex (combo [i]);

				Sprite boton = botones[indice];

				switch (i) {
				case 0: 
					GameObject btn13 = GameObject.Find ("btn13");
					btn13.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				case 1: 
					GameObject btn23 = GameObject.Find ("btn23");
					btn23.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				case 2: 
					GameObject btn33 = GameObject.Find ("btn33");
					btn33.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				case 3: 
					GameObject btn43 = GameObject.Find ("btn43");
					btn43.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				case 4: 
					GameObject btn53 = GameObject.Find ("btn53");
					btn53.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				case 5: 
					GameObject btn63 = GameObject.Find ("btn63");
					btn63.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				}

			}
		}

		if (tag == "final"){
			fondo.clip = final;
			fondo.Play ();
			if (combo.Length == 4) {
				GameObject btn54 = GameObject.Find ("btn54");
				btn54.GetComponent<SpriteRenderer> ().sprite = null;
				GameObject btn64 = GameObject.Find ("btn64");
				btn64.GetComponent<SpriteRenderer> ().sprite = null;
			}
			if (combo.Length == 5) {
				GameObject btn64 = GameObject.Find ("btn64");
				btn64.GetComponent<SpriteRenderer> ().sprite = null;
			}

			for (int i = 0; i < combo.Length; i++) {
				int indice = spriteIndex (combo [i]);

				Sprite boton = botones[indice];

				switch (i) {
				case 0: 
					GameObject btn14 = GameObject.Find ("btn14");
					btn14.GetComponent<SpriteRenderer> ().sprite = boton;

					break;
				case 1: 
					GameObject btn24 = GameObject.Find ("btn24");
					btn24.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				case 2: 
					GameObject btn34 = GameObject.Find ("btn34");
					btn34.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				case 3: 
					GameObject btn44 = GameObject.Find ("btn44");
					btn44.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				case 4: 
					GameObject btn54 = GameObject.Find ("btn54");
					btn54.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				case 5: 
					GameObject btn64 = GameObject.Find ("btn64");
					btn64.GetComponent<SpriteRenderer> ().sprite = boton;
					break;
				}

			}
		}

	}



	int spriteIndex(char letra){
		switch (letra) {
		case 'w': 
			return 0;
			break;
		case 'a':
			return 1;
			break;
		case 'v':
			return 2;
			break;
		case 'e':
			return 3;
			break;
		default:
			return 4;
		}
	}



	IEnumerator Tablas(string tag) {
		verTabla (true, tag);
		float tiempo = 5f;
		if (ScoreManager.score <= 30 || ScoreManager.score >= 70)
			tiempo = 2f;
		yield return new WaitForSeconds(tiempo);
		verTabla(false, tag);
	}

	void verTabla(bool valor, string tag){
		if (tag == "npc") {
			
			cambiarEnabled ("tabla", valor);
			cambiarEnabled ("btn1", valor);
			cambiarEnabled ("btn2", valor);
			cambiarEnabled ("btn3", valor);
			cambiarEnabled ("btn4", valor);
			cambiarEnabled ("btn5", valor);
			cambiarEnabled ("btn6", valor);
		}
		if (tag == "npcpareja") {
			cambiarEnabled ("tabla2", valor);
			cambiarEnabled ("btn12", valor);
			cambiarEnabled ("btn22", valor);
			cambiarEnabled ("btn32", valor);
			cambiarEnabled ("btn42", valor);
			cambiarEnabled ("btn52", valor);
			cambiarEnabled ("btn62", valor);
		}
		if (tag == "npcpicasso") {
			cambiarEnabled ("tabla3", valor);
			cambiarEnabled ("btn13", valor);
			cambiarEnabled ("btn23", valor);
			cambiarEnabled ("btn33", valor);
			cambiarEnabled ("btn43", valor);
			cambiarEnabled ("btn53", valor);
			cambiarEnabled ("btn63", valor);
		}
		if (tag == "final") {
			cambiarEnabled ("tabla4", valor);
			cambiarEnabled ("btn14", valor);
			cambiarEnabled ("btn24", valor);
			cambiarEnabled ("btn34", valor);
			cambiarEnabled ("btn44", valor);
			cambiarEnabled ("btn54", valor);
			cambiarEnabled ("btn64", valor);
		}

	}


}
