using UnityEngine;
using System.Collections;

public class AstroControls : MonoBehaviour {

	public float rotationSpeed = 1.0f;

	public int playerID = 1;
	public float Speed = 5.0f;
	public GameObject pulse0, pulse1, pulse2, pulse12;

	float pulseTimer = 0.0f;
	float pulseCoolDown = 0.5f;
	bool canPulse = true;
	public float pulseStrength = 0.0f;
	public float buildUpSpeed = 0.5f;
	float pulseLimit = 3.0f;
	public Material transparentBlue;
	bool pushed = false;
	Vector2 pushedDirection;
	float pushedcount = 0.0f;
	float countLimit = 1.0f;

	void pulsePushed(Vector2 given){
		pushed = true;
		pushedDirection = given;
	}

	void pulse(){
		pulseStrength += 1.0f * Time.deltaTime * buildUpSpeed;
		if (pulseStrength >= pulseLimit) {
			releasePulse ();
			canPulse = false;
		}
	}

	void releasePulse(){

		pulse0.transform.position = new Vector3 (1000.0f, 1000.0f, 0.0f);

		if (playerID == 1) {
						pulse1.GetComponent<pulsevelocity> ().thrust = 0.5f + pulseStrength / 1f;
						//Debug.Log ("pStrength: " + pulseStrength);
						//pulse1.renderer.material.color = new Color (pulseStrength/5f, 0f, 1f);
						pulse1.renderer.material = transparentBlue;
						pulse0.transform.localScale = new Vector3 (1.2f, 1.2f, 1.20f);
						Instantiate (pulse1, this.transform.position, this.transform.rotation);
				} else if (playerID == 2) {
						pulse12.GetComponent<pulsevelocity> ().thrust = 0.5f + pulseStrength / 1f;
						//Debug.Log ("pStrength: " + pulseStrength);
						//pulse1.renderer.material.color = new Color (pulseStrength/5f, 0f, 1f);
						pulse12.renderer.material = transparentBlue;
						pulse0.transform.localScale = new Vector3 (1.2f, 1.2f, 1.20f);
						Instantiate (pulse12, this.transform.position, this.transform.rotation);
				}
		pulseStrength = 0.0f;
		pulseTimer = 0.0f;
		canPulse = false;
	}

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

		if (pushed) {
			if(pushedcount < countLimit)
			{
				transform.position += new Vector3(pushedDirection.x, pushedDirection.y, 0.0f) * (1.0f - pushedcount);
				pushedcount += 1.0f * Time.deltaTime;
			}
			else if(pushedcount >= countLimit){
				pushedcount = 0.0f;
				pushed = false;
			}

		
		}

		pulseTimer += 1.0f * Time.deltaTime;
		if (pulseTimer >= pulseCoolDown && !canPulse)
						canPulse = true;

		if (playerID == 1) {
			transform.Rotate(Vector3.up, rotationSpeed * Input.GetAxis("Horizontal"));
			transform.Rotate(Vector3.right, rotationSpeed * Input.GetAxis("Vertical"));
			transform.Rotate(Vector3.up, rotationSpeed * Input.GetAxis("Horizontal2"));
			transform.Rotate(Vector3.right, rotationSpeed * Input.GetAxis("Vertical2"));

			if ((Input.GetKey(KeyCode.Joystick1Button0)||(Input.GetKey(KeyCode.LeftShift)))&&canPulse){
				pulse0.transform.position = transform.position;
				pulse0.transform.localScale *= 0.999f;
				pulse();
			}
			else if ((Input.GetKeyUp(KeyCode.Joystick1Button0)||(Input.GetKeyUp(KeyCode.LeftShift)))&&canPulse){
				releasePulse();
				audio.Play();
			}

			if ((Input.GetAxis ("Horizontal") > 0.0f)||(Input.GetKey (KeyCode.D))) {
				//rigidbody.AddForce(Vector3.right * Speed * Time.deltaTime * 10000.0f);
				transform.position += (Vector3.right * Speed * Time.deltaTime * 10.0f);
			}
			if ((Input.GetAxis ("Horizontal") < 0.0f)||(Input.GetKey (KeyCode.A))) {
				//rigidbody.AddForce(Vector3.right * Speed * Time.deltaTime * -10000.0f);
				transform.position += (Vector3.right * Speed * Time.deltaTime * -10.0f);
			}
			if ((Input.GetAxis ("Vertical") > 0.0f)||(Input.GetKey (KeyCode.W))) {
				//rigidbody.AddForce(Vector3.up * Speed * Time.deltaTime * 10000.0f);
				transform.position += (Vector3.up * Speed * Time.deltaTime * 10.0f);
			}
			if ((Input.GetAxis ("Vertical") < 0.0f)||(Input.GetKey (KeyCode.S))) {
				//rigidbody.AddForce(Vector3.up * Speed * Time.deltaTime * -10000.0f);
				transform.position += (Vector3.up * Speed * Time.deltaTime * -10.0f);
			}
		}
		else if (playerID == 2) {

			transform.Rotate(rotationSpeed * Input.GetAxis ("VerticalII"), rotationSpeed * Input.GetAxis("HorizontalII"),0.0f);

			if ((Input.GetKey (KeyCode.Space)||(Input.GetKey(KeyCode.Joystick2Button0))&&canPulse)){
				pulse0.transform.position = transform.position;
				pulse0.transform.localScale *= 0.999f;
				pulse();
			}
			else if ((Input.GetKeyUp (KeyCode.Space)||(Input.GetKeyUp(KeyCode.Joystick2Button0))&&canPulse)){
				releasePulse();
				audio.Play();
			}
			
			if ((Input.GetAxis ("HorizontalII") > 0.0f)||(Input.GetKey (KeyCode.RightArrow))) {
				transform.position += (Vector3.right * Speed * Time.deltaTime * 10.0f);
				transform.Rotate(Vector3.up * -1.0f, rotationSpeed);
			}
			if ((Input.GetAxis ("HorizontalII") < 0.0f)||(Input.GetKey (KeyCode.LeftArrow))) {
				transform.position += (Vector3.right * Speed * Time.deltaTime * -10.0f);
				transform.Rotate(Vector3.up, rotationSpeed);
			}
			if ((Input.GetAxis ("VerticalII") > 0.0f)||(Input.GetKey (KeyCode.UpArrow))) {
				transform.position += (Vector3.up * Speed * Time.deltaTime * 10.0f);
				transform.Rotate(Vector3.right * -1.0f, rotationSpeed);
			}
			if ((Input.GetAxis ("VerticalII") < 0.0f)||(Input.GetKey (KeyCode.DownArrow))) {
				transform.position += (Vector3.up * Speed * Time.deltaTime * -10.0f);
				transform.Rotate(Vector3.right, rotationSpeed);
			}

			transform.Rotate(Vector3.up, rotationSpeed * Input.GetAxis("Horizontal2II"));
			transform.Rotate(Vector3.right, rotationSpeed * Input.GetAxis("Vertical2II"));

		}
	
	}
}
