using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Level2Manag : MonoBehaviour {
	public float tiempo, tiempototal;
	public int score;
	public GameObject camara;
	public bool siperdio;
	public Scrollbar barratiempo;
	public Text lblscore;
	public GameObject btnnext;
	public Animator scoreanim,btnanimator,starsanimator;
	public int SceneChange;

	public GameObject luces;
	public GameObject sol;
	// Use this for initialization

	public GameObject interfaz,btnAtras;
	//public Image presplash;

	public bool preescena;
	public GameObject preescen,postescen;
	public float timerpre,timermaxpre;
	public float timerpost,timermaxpost;
	public bool post;
	public Text scoretxt;
	void Start () {
		post = false;
		postescen.SetActive (false);
		PlayerPrefs.SetInt ("preescena", 0);

		timermaxpre=5;
		timerpre = 0;
		timermaxpost = 5;
		timerpost = 0;


		sol.SetActive (true);
		luces.SetActive (false);	
		tiempototal = tiempo;
		siperdio = true;
		interfaz = GameObject.FindGameObjectWithTag ("lvl2UI");
		interfaz.SetActive (false);
		camara = GameObject.FindGameObjectWithTag ("MainCamera");
		PlayerPrefs.SetInt("pauselvl2", 0);
		//presplash.GetComponent<Animator> ().SetBool ("mostrar", true);

		//StartCoroutine (presplashx ());
	}
	//IEnumerator presplashx (){
	//	yield return new WaitForSeconds (10);
	//}

	
	// Update is called once per frame
	void Update () {
		scoretxt.text = score.ToString ();
		if (Input.GetKeyDown (KeyCode.Escape)) {
			SceneManager.LoadScene ("MenuLevels");
		}
		if (post) {
			postescen.SetActive (true);
			postescen.GetComponent<Animator> ().enabled = true;
			timerpost += Time.deltaTime;

			if (timerpost > timermaxpost) {

				SceneManager.LoadScene("MenuLevels");
			}
		}



		if (timerpre > timermaxpre) {
			PlayerPrefs.SetInt ("preescena", 1);
			preescen.SetActive (false);
		}
		if (PlayerPrefs.GetInt("preescena")==1) {
			if (PlayerPrefs.GetInt ("pauselvl2") != 1) {
				tiempo -= Time.deltaTime;
				barratiempo.size = (tiempo * 1) / tiempototal;
				if (tiempo <= 0) {
					Debug.Log ("Puntuar");
					PlayerPrefs.SetInt ("pauselvl2", 1);
					camara.GetComponent<CameraWireFrame> ().tiempoinicial = camara.GetComponent<CameraWireFrame> ().tiempofinal;

				}
			} else {
				if (siperdio) {
					siperdio = false;
					Mostrarinterfaz ();
					Puntuarpuntos ();
				}
			}
		} else {
			timerpre += Time.deltaTime;
		}
	}


	private void Puntuarpuntos(){
		lblscore.text = score.ToString();
		// = GameObject.Find ("score");
		scoreanim.GetComponent<Animator> ().SetBool ("showscore", true);

		int numestrellas = numstars (score);
		StartCoroutine (breaktime(numestrellas));
		StartCoroutine (breakbotones (numestrellas));
		if (PlayerPrefs.GetInt ("Level" + (SceneChange - 1)) <= numestrellas) {
			PlayerPrefs.SetInt ("Level" + (SceneChange - 1).ToString () + "_stars", numestrellas);
		}


	}
	IEnumerator breaktime(int numestrellas){
		yield return new WaitForSeconds (1);
		starsanimator.SetInteger ("numstars", numestrellas);
	}
	IEnumerator breakbotones(int numestrellas){
		yield return new WaitForSeconds (2);
		if (numestrellas == 0) {
			btnnext.SetActive (false);
		}
		btnanimator.SetBool ("showboton", true);
		//starsanimator.SetInteger ("numstars", numestrellas);
	}

	int numstars(int score){

		if (score > 1000 && score < 3000) {
			PlayerPrefs.SetInt ("Level"+SceneChange.ToString(), 1);
			return 1;
		}

		if (score >= 3000 && score < 6000) {
			PlayerPrefs.SetInt ("Level"+SceneChange.ToString(), 1);
			return 2;
		}

		if (score >= 6000) {
			PlayerPrefs.SetInt ("Level"+SceneChange.ToString(), 1);
			return 3;
		}
		return 0;

	}

	private void Mostrarinterfaz()
	{
		scoretxt.gameObject.SetActive (false);
		interfaz.SetActive(true);
		btnAtras.SetActive (false);
		barratiempo.gameObject.SetActive (false);
	}



	public void Wireframe(float tiempo){
		camara.GetComponent<CameraWireFrame>().tiempowire(tiempo);
	}


	public void returnMenu(){
		SceneManager.LoadScene ("MenuLevels");
	}
	public void Repeat(){
		int actual = SceneManager.GetActiveScene ().buildIndex;
		SceneManager.LoadScene(actual);
	}
	public void Next(){
		btnAtras.SetActive (false);
		post = true;
	}
}
