using UnityEngine;
using System.Collections;

public class InputController : MonoBehaviour 
{
	public bool running = true;

	public bool[] playersActiveKey;
	private KeyCode[] keycodes = new [] 
	{
		KeyCode.A, 
		KeyCode.F, 
		KeyCode.J, 
		KeyCode.L
	};

	private void Awake ()
	{
		playersActiveKey = new bool[keycodes.Length];

		//StartCoroutine (ControlThrust ());
	}

	void Update () 
	{
		for (int i=0; i<keycodes.Length; i++)
		{

			if (Input.GetKeyDown (keycodes[i]))
			{
				playersActiveKey[i] = true;
			}

			if (Input.GetKeyUp (keycodes[i]))
			{
				playersActiveKey[i] = false;
			}
		}

		//BodyRigidbody.AddForce(new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")), ForceMode2D.Impulse);
	}

	/*public Transform baseShip;
	private const float ROTATE_FORCE = 3.0f;
	private void LateUpdate ()
	{
		if (Input.GetAxis("Vertical") == -1.0f)
			BodyRigidbody.AddRelativeForce(-Vector2.up * 120, ForceMode2D.Force);
		
		if (Input.GetAxis("Vertical") == 1.0f)
			BodyRigidbody.AddRelativeForce(Vector2.up * 120, ForceMode2D.Force);

		if (Input.GetAxis("Horizontal") == 1.0f)
			BodyRigidbody.AddRelativeForce(Vector2.right * 200, ForceMode2D.Force);
		
		if (Input.GetAxis("Horizontal") == -1.0f)
			BodyRigidbody.AddRelativeForce(-Vector2.right * 200, ForceMode2D.Force);

		if (Input.GetKey (KeyCode.E))
			baseShip.Rotate (0, 0, -2);

		if (Input.GetKey(KeyCode.Q))
			baseShip.Rotate (0, 0, 2);
			//BodyRigidbody.AddForceAtPosition(Vector2.right * ROTATE_FORCE, 
			                                 //new Vector2(baseShip.Hardpoints[2].HardpointTransform.position.x, 
			            								 //baseShip.Hardpoints[2].HardpointTransform.position.y), ForceMode2D.Force);
	}

	[SerializeField]
	private Rigidbody2D BodyRigidbody;
	
	[SerializeField]
	private Rigidbody2D ControlRigidbody;
	
	private const float GRAVITY_SCALE = 500.0f;
	
	private float[] X_Vectors = new [] {0.0f,  0.0f, -GRAVITY_SCALE, GRAVITY_SCALE};
	private float[] Y_Vectors = new [] {GRAVITY_SCALE, -GRAVITY_SCALE,  0.0f, 0.0f} ;
	
	public IEnumerator ControlThrust ()
	{
		//while (running)
		//{
			//for (int i=0; i<keycodes.Length; i++)
			//{
			//	if (playersActiveKey[i] && i < 4)
			//	{

			if (Input.GetAxis("Vertical") == -1.0f)
				BodyRigidbody.AddRelativeForce(-Vector2.up, ForceMode2D.Impulse);

			if (Input.GetAxis("Vertical") == 1.0f)
				BodyRigidbody.AddRelativeForce(Vector2.up, ForceMode2D.Impulse);

			if (Input.GetAxis("Horizontal") == 1.0f)
				BodyRigidbody.AddRelativeForce(Vector2.right, ForceMode2D.Impulse);

			if (Input.GetAxis("Horizontal") == -1.0f)
				BodyRigidbody.AddRelativeForce(-Vector2.right, ForceMode2D.Impulse);

				//BodyRigidbody.AddForce(new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")), ForceMode2D.Impulse);

					//BodyRigidbody.AddForce(new Vector2(X_Vectors[i], Y_Vectors[i]), ForceMode2D.Impulse);
					//ControlRigidbody.AddForce(new Vector2(X_Vectors[i], Y_Vectors[i]), ForceMode2D.Impulse);
			//	}				
			//}
			yield return null;
		//}
	}*/
}
