﻿using UnityEngine;
using System.Collections;

public class NoteScript : MonoBehaviour {

	// Use this for initialization
	PlayerMoveScript playerscript;
	private Vector3 movement;
	public float bpm = 140.0f;
	//private int direction;
	public float angle = 30.0f;
	public float speed = 7.0f;
	public float life = 3f;
	public float deathtime = 0.0f;
	private int hit = 0;
	private int hitting = 1;
	private int pressed = 0;
	private float timer = 0.0f;
	private int applied = 0;

	void Start () {

		playerscript = GameObject.Find ("Player").GetComponent<PlayerMoveScript> ();

		life = ((60.0f/bpm) * 4.0f) + 0.65f;

		//rigidbody.velocity = new Vector2 (5, 0);
		if (transform.localPosition.x > 0) {
			if (transform.localPosition.y > 4.2f)
			{
				transform.Rotate (0f, 0f, Random.Range(-angle, 0.0f) + 180f);
			}
			else if (transform.localPosition.y < -4.2f)
			{
				transform.Rotate (0f, 0f, Random.Range(0.0f, angle) + 180f);
			}
			else
			{
				transform.Rotate (0f, 0f, Random.Range(-angle, angle) + 180f);
			}
			//direction = 1;
		}
		else if (transform.localPosition.x < 0) {
			if (transform.localPosition.y > 4.2f)
			{
				transform.Rotate (0f, 0f, Random.Range(0.0f, angle));
			}
			else if (transform.localPosition.y < -4.2f)
			{
				transform.Rotate (0f, 0f, Random.Range(-angle, 0.0f));
			}
			else
			{
				transform.Rotate (0f, 0f, Random.Range(-angle, angle));
			}
			//direction = 0;
		}
	}
	
	// Update is called once per frame
	void Update () {
		/*if (transform.localPosition.y <= -4.5f) 
		{
			// if going left
			if (direction == 1)
			{
				transform.Rotate(0f, 0f, transform.localRotation.z + 90f);
			}
			else
			{
				transform.Rotate(0f, 0f, transform.localRotation.z - 90f);
			}
		}
		else if (transform.localPosition.y >= 4.5f) 
		{
			// if going left
			if (direction == 1)
			{
				transform.Rotate(0f, 0f, transform.localRotation.z - 90f);
			}
			else
			{
				transform.Rotate(0f, 0f, transform.localRotation.z + 90f);
			}
		} */

		if (transform.localRotation.z > 180f || transform.localRotation.z < 0f) {
				movement = new Vector3 (-speed, 0, 0);
		} 
		else {
				movement = new Vector3 (speed, 0, 0);
		}

		movement *= Time.deltaTime;
		
		transform.Translate (movement);
		deathtime += Time.deltaTime;

		if (gameObject.tag == "Note" && hit == 1 && hitting == 0 && applied == 0) {
			playerscript.multiplier = 0;
			applied = 1;
		}
		else if (gameObject.tag == "Note" && hit == 1 && pressed == 1 && applied == 0) {
			playerscript.score += 55 * playerscript.multiplier + 1;
			playerscript.multiplier += 1;
			applied = 1;
			DestroyObject(gameObject, 0);
		}
		if (gameObject.tag == "NegNote" && hit == 1 && hitting == 0 && applied == 0) {
			playerscript.score += 55 * playerscript.multiplier + 1;
			playerscript.multiplier += 1;
			applied = 1;
			//DestroyObject(gameObject, 0);
		}
		else if (gameObject.tag == "NegNote" && hit == 1 && pressed == 1 && applied == 0) {
			playerscript.multiplier = 0;
			DestroyObject(gameObject, 0);
			applied = 1;
		}

		DestroyObject (gameObject, life);



		if (timer > 0.1f) {
			hitting = 0;
			pressed = 0;
			timer = 0;
		}

		timer += Time.deltaTime;

	}

	void OnTriggerEnter2D (Collider2D other) 
	{
		if (other.name == "PlayerSprite") {
			hit = 1;
			hitting = 1;
			pressed = playerscript.pressed;
			
		}
		
		
	}
	
}
