using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PlayerMoveScript : MonoBehaviour {

	public float speed = 0.0f;
	public float max_speed = 140f;
	public float origx = 0.0f;
	public Sprite dash;
	public Sprite hide;
	public int pressed = 0;
	public int start = 0;
	public float timer = 0f;
	public int goal = 0;
	public int total_scare = 0;
	public int max_scares = 10;
	public float St = 25.5f;
	public float At = 30.5f;
	public float Bt = 40.5f;
	public Sprite s_rank;
	public Sprite a_rank;
	public Sprite b_rank;
	public Sprite c_rank;
	AudioSource audsrc;
	public int isPlaying = 0;

	// Use this for initialization
	void Start () {
		audsrc = GetComponent<AudioSource> ();
	}

	// Update is called once per frame
	void Update () {
		if (start >= 0) {
			if (Input.GetKeyDown (KeyCode.W) || Input.GetKeyDown (KeyCode.E) || Input.GetKeyDown (KeyCode.O) || Input.GetKeyDown (KeyCode.P) || Input.GetKeyDown (KeyCode.UpArrow) || Input.GetKeyDown (KeyCode.LeftArrow) || Input.GetKeyDown (KeyCode.RightArrow) || Input.GetKeyDown (KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Return)) {
				speed = max_speed;
				origx = transform.position.x;
				GetComponent<SpriteRenderer> ().sprite = dash;
				if (start == 0) {
					start = 1;
				}
			}
			if (Input.GetKeyDown (KeyCode.W) || Input.GetKeyDown (KeyCode.UpArrow)) {
				pressed = 1;
			}
			if (Input.GetKeyDown (KeyCode.E) || Input.GetKeyDown (KeyCode.RightArrow)) {
				pressed = 2;
			}
			if (Input.GetKeyDown (KeyCode.O) || Input.GetKeyDown (KeyCode.DownArrow)) {
				pressed = 3;
			}
			if (Input.GetKeyDown (KeyCode.P) || Input.GetKeyDown (KeyCode.LeftArrow)) {
				pressed = 4;
			}
		}

		if (start == 1) {
			timer += Time.deltaTime;
			GameObject.Find("Timer").GetComponent<Text>().text = string.Format("{0:00}:{1:00}:{2:000}", (int)(timer / 60), (int)(timer % 60), (int)((timer * 1000) % 1000));
		}

		if (start < 0) {
			if (Input.GetKeyDown(KeyCode.Space))
			{
				Application.LoadLevel(Application.loadedLevel);
			}
			
			if (Input.GetKeyDown(KeyCode.Escape))
			{
				Application.LoadLevel("Title");
			}
		}

		if (start == -2) {
			if (GameObject.Find("Timer").GetComponent<Text>().fontSize < 180)
			{
				GameObject.Find("Timer").GetComponent<Text>().fontSize += (int)(850 * Time.deltaTime);
			}

			GameObject.Find("Restart_Text").GetComponent<Text>().enabled = true;



			GameObject[] scares =  GameObject.FindGameObjectsWithTag("citizen");
			foreach (GameObject scare in scares)
			{
				CitizenScript citScript = scare.GetComponent<CitizenScript>();
				total_scare += citScript.isScared;
			}

			if (max_scares == total_scare)
			{
				GameObject.Find("Scare_Rank").GetComponent<SpriteRenderer>().sprite = s_rank;
			}
			if ((max_scares - total_scare) > 0 && (max_scares - total_scare) < (int)(max_scares / 4))
			{
				GameObject.Find("Scare_Rank").GetComponent<SpriteRenderer>().sprite = a_rank;
			}
			if ((max_scares - total_scare) > (int)(max_scares / 4) && (max_scares - total_scare) < (int)(max_scares / 2))
			{
				GameObject.Find("Scare_Rank").GetComponent<SpriteRenderer>().sprite = b_rank;
			}
			if ((max_scares - total_scare) > (int)(max_scares / 2))
			{
				GameObject.Find("Scare_Rank").GetComponent<SpriteRenderer>().sprite = c_rank;
			}

			if ((timer % 60) <= St)
			{
				GameObject.Find("Speed_Rank").GetComponent<SpriteRenderer>().sprite = s_rank;
			}
			if ((timer % 60) > St && (timer % 60) <= At)
			{
				GameObject.Find("Speed_Rank").GetComponent<SpriteRenderer>().sprite = a_rank;
			}
			if ((timer % 60) > At && (timer % 60) <= Bt)
			{
				GameObject.Find("Speed_Rank").GetComponent<SpriteRenderer>().sprite = b_rank;
			}
			if ((timer % 60) > Bt)
			{
				GameObject.Find("Speed_Rank").GetComponent<SpriteRenderer>().sprite = c_rank;
			}
			
			if (GameObject.Find("bg_bar").GetComponent<RectTransform>().sizeDelta.y < 1000)
			{
				GameObject.Find("bg_bar").GetComponent<RectTransform>().sizeDelta = new Vector2(2500, GameObject.Find("bg_bar").GetComponent<RectTransform>().sizeDelta.y + 100);
			}
			if (GameObject.Find("bg_bar").GetComponent<RectTransform>().sizeDelta.y >= 1000)
			{

				GameObject.Find("Speed_Rank").GetComponent<SpriteRenderer>().enabled = true;
				GameObject.Find("Scare_Rank").GetComponent<SpriteRenderer>().enabled = true;
				GameObject.Find("rank_label_1").GetComponent<Text>().enabled = true;
				GameObject.Find("rank_label_2").GetComponent<Text>().enabled = true;
			}



		}

		if (goal == 0) {
			GetComponent<Rigidbody2D> ().AddForce (Vector3.right * speed);
		} else {
			GetComponent<Rigidbody2D>().AddForce(Vector3.left * speed);
		}
	}
	void OnTriggerEnter2D(Collider2D coll) {
		if (coll.gameObject.tag == "hide_object") {
			speed = 0f;
			GetComponent<Rigidbody2D>().velocity = new Vector3(0, 0);
			GetComponent<SpriteRenderer>().sprite = hide;

			GameObject theCam = GameObject.Find("Main Camera");
			CameraScript camScript = theCam.GetComponent<CameraScript>();
			 camScript.target = (theCam.transform.position.x + (transform.position.x - origx)) / 1.0f;

			ObjectScript objScript = coll.GetComponent<ObjectScript>();

			foreach (Transform child in coll.transform) {
				if (child.tag == "key")
				{
					GameObject theKey = child.gameObject;
					KeyScript keyScript = theKey.GetComponent<KeyScript>();
					if (keyScript.key == pressed && start == 1)
					{
						objScript.isHide = 1;
					}
					if (keyScript.key != pressed && start == 1)
					{
						objScript.isError = 1;
						if (isPlaying == 0)
						{
							audsrc.Stop();
							audsrc.Play();
							isPlaying = 1;
						}

						GameObject.Find("game_over").GetComponent<SpriteRenderer>().enabled = true;
						GameObject.Find("Restart_Text").GetComponent<Text>().enabled = true;
						start = -1;
					}
				}
			}


		}
	}

	void OnTriggerExit2D(Collider2D coll) {
		if (coll.gameObject.tag == "hide_object") {
			ObjectScript objScript = coll.GetComponent<ObjectScript>();
			objScript.isHide = 0;
		}
	}

}

	
	
