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

public class PlayerMoveScript : MonoBehaviour {
	
	private Vector3 movement;
	public int score = 0;
	public int multiplier = 1;
	public float bpm = 140.0f;
	public int xdir = 1;
	public int ydir = 0;
	public float speed = 5.0f;
	private int start = 0;
	private GameObject hitstyle;
	private Text txt;
	private Text scoretxt;
	public float distance;
	//private GameObject playerspr;
	public int pressed = 1;
	public float released = 0.0f;
	public int life = 3;
	public float sec = 2;

	protected Animator animator;
	// Use this for initialization
	void Start () {
		StartCoroutine (Wait());

		sec = 1.5f;
		Wait ();
		txt = GameObject.Find ("Lives").GetComponent<Text> ();
		scoretxt = GameObject.Find ("Score").GetComponent<Text> ();
		//playerspr = GameObject.Find ("PlayerSprite");
		animator = GameObject.Find("PlayerSprite").GetComponent<Animator>();
	}
	// Update is called once per frame
	void Update () {

		if (start == 1) 
		{
			movement = new Vector3 (speed * xdir, speed * ydir, 0);
			//movement = new Vector2 (speed, 0);
			//movement *= Time.deltaTime - ((60.0f/bpm) * 4.0f);
		
			transform.Translate (movement);



			if (Input.GetKey(KeyCode.Return) || Input.GetKey(KeyCode.DownArrow))
			{
					pressed = 1;
			}
			else
			{
				pressed = 0;
			}





			/*switch (life)
			{
			case 10 : txt.text = "Ten\nLives"; break;
			case 9 : txt.text = "Nine\nLives"; break;
			case 8 : txt.text = "Eight\nLives"; break;
			case 7 : txt.text = "Seven\nLives"; break;
			case 6 : txt.text = "Six\nLives"; break;
			case 5 : txt.text = "Five\nLives"; break;
			case 4 : txt.text = "Four\nLives"; break;
			case 3 : txt.text = "Three\nLives"; break;
			case 2 : txt.text = "Two\nLives"; break;
			case 1 : txt.text = "One\nLife"; break;
			case 0 : txt.text = "Game\nOver"; start = 0; break;
			}*/

		} 

		if (transform.localPosition.x >= 749.4f && transform.localPosition.y == 472.7f) {
			
			//transform.Rotate(0,0,90 * -xdir);
			transform.localPosition = new Vector3 (749.4f, 472.7f, -16);
			// go Down
			ydir = -1;
			xdir = 0;
			
		}
		else if (transform.localPosition.x == 749.4f  && transform.localPosition.y <= 320.6f) {
			
			//transform.Rotate(0,0,90 * -xdir);
			transform.localPosition = new Vector3 (749.4f, 320.6f, -16);
			// go Right
			ydir = 0;
			xdir = 1;
			
		}
		else if (transform.localPosition.x >= 889.5f  && transform.localPosition.y == 320.6f) {
			
			//transform.Rotate(0,0,90 * -xdir);
			transform.localPosition = new Vector3 (889.5f, 330.6f, -16);
			// go Up
			ydir = 1;
			xdir = 0;
			
		}
		else if (transform.localPosition.x == 889.5f  && transform.localPosition.y >= 379.4f) {
			
			//transform.Rotate(0,0,90 * -xdir);
			transform.localPosition = new Vector3 (889.5f, 379.4f, -16);
			// go Left
			ydir = 0;
			xdir = -1;
			
		}
		else if (transform.localPosition.x <= 450.8f  && transform.localPosition.y == 379.4f) {
			
			//transform.Rotate(0,0,90 * -xdir);
			transform.localPosition = new Vector3 (450.8f, 379.4f, -16);
			// go Up
			ydir = 1;
			xdir = 0;
			
		}
		else if (transform.localPosition.x == 450.8f  && transform.localPosition.y >= 472.7f) {
			
			//transform.Rotate(0,0,90 * -xdir);
			transform.localPosition = new Vector3 (450.8f, 472.7f, -16);
			// go Right
			ydir = 0;
			xdir = 1;
			
		}

		animator.SetInteger("pressed", pressed);


		scoretxt.text = score.ToString("0000000000");
		if (multiplier > 0) {
						txt.text = "x" + multiplier.ToString ();
				} else {
			txt.text = "";
				}
		
	}
	
	void OnTriggerEnter2D (Collider2D other) 
	{
		/*if (other.name == "TurnD") {
			
			//transform.Rotate(0,0,90 * -xdir);
			ydir = -1;
			xdir = 0;
			
		}
		else if (other.name == "TurnU") {
			//transform.Rotate(0,0,90 * (xdir));
			ydir = 1;
			xdir = 0;
			
		}
		else if (other.name == "TurnR") {
			//transform.Rotate(0,0,90 * (-ydir));
			ydir = 0;
			xdir = 1;
		}
		else if (other.name == "TurnL") {
			//transform.Rotate(0,0,90 * (ydir));
			ydir = 0;
			xdir = -1;
		} */

		/* if (other.tag == "Note") {
			if (pressed == 1)
			{
				//life -= 1;
				score += 55 * multiplier + 1;
				DestroyObject(GameObject.Find(other.name));
				multiplier += 1;
			}

		}
		if (other.tag == "NegNote") {
			if (pressed == 1)
			{
				//life -= 1;
				multiplier = 0;
				DestroyObject(GameObject.Find(other.name));
			}
			
		} */


	}




	IEnumerator Wait() {
		yield return new WaitForSeconds((60.0f/bpm) * 4.0f);
		start = 1;
	}


}



