using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class PlayerScript : MonoBehaviour {

	public AudioClip[] waveMusic = new AudioClip[3];
	public AudioClip waveChangeSound;
	public AudioClip deathSound;
	public GameObject playerImage;

	private Rigidbody2D rigidbody;
	private int moveSpeed = 5;
	private GlobalEnums.WaveTypes currentWaveType;
	private GlobalEnums.ActivityState currentActivityState;

	private float maxInputDelay = 0.15f;
	private float inputDelayLeft = 0;
	private bool isPaused = false; //used so level "stops"
	private int batteriesCollected = 0;

	//controller vars
	private float stickTol = 0.57f;
	string  controller1MoveAxisH 		 = "HorizontalC";
	string  controller1MoveAxisV 		 = "VerticalC";
	KeyCode controller1SinKey 			 = KeyCode.Joystick1Button2;
	KeyCode controller1SquKey            = KeyCode.Joystick1Button3;
	KeyCode controller1TriKey 	         = KeyCode.Joystick1Button1;


	// Use this for initialization
	void Start () {
		rigidbody = this.gameObject.GetComponent<Rigidbody2D> ();
		StartCoroutine (ChangeSong (0));
		inputDelayLeft = maxInputDelay;

		currentActivityState = GlobalEnums.ActivityState.None;
	}

	// Update is called once per frame
	void Update () {
		if (!isPaused) 
		{
			
		}
	}

	public void UnPause()
	{
		isPaused = false;
	}

	public void Pause()
	{
		isPaused = true;
		this.GetComponent<Rigidbody2D> ().velocity = Vector2.zero;
	}

	void FixedUpdate() {
		if (!isPaused) 
		{
			MovementControllers ();
			MovementKeyboard ();
			ChangeWave ();
		}
	}

	IEnumerator ChangeSong(int songNum)
	{
		this.GetComponent<AudioSource> ().Stop ();
		float lengthLeft = waveChangeSound.length;
		this.GetComponent<AudioSource> ().clip = waveChangeSound;
		this.GetComponent<AudioSource> ().loop = false;
		this.GetComponent<AudioSource> ().Play ();

		while (lengthLeft > 0)
		{
			lengthLeft -= Time.fixedDeltaTime;

			yield return new WaitForFixedUpdate();
		}

		this.GetComponent<AudioSource> ().Stop ();
		this.GetComponent<AudioSource> ().loop = true;
		this.GetComponent<AudioSource> ().clip = waveMusic [songNum];
		this.GetComponent<AudioSource> ().Play ();

	}

	void ChangeWave () {

		inputDelayLeft -= Time.fixedDeltaTime;

		if (inputDelayLeft <= 0)
		{
			if (Input.GetKey (KeyCode.Alpha1) || Input.GetKey (KeyCode.Keypad1) || Input.GetKey (controller1SinKey)) 
			{
				StartCoroutine (ChangeSong (0));
				inputDelayLeft = maxInputDelay;
				currentWaveType = GlobalEnums.WaveTypes.Sin;
			}

			if (Input.GetKey (KeyCode.Alpha2) || Input.GetKey (KeyCode.Keypad2) || Input.GetKey (controller1SquKey)) 
			{
				StartCoroutine (ChangeSong (1));
				inputDelayLeft = maxInputDelay;
				currentWaveType = GlobalEnums.WaveTypes.Square;
			}

			if (Input.GetKey (KeyCode.Alpha3) || Input.GetKey (KeyCode.Keypad3) || Input.GetKey (controller1TriKey)) 
			{

				StartCoroutine (ChangeSong (2));
				inputDelayLeft = maxInputDelay;
				currentWaveType = GlobalEnums.WaveTypes.Triangle;
			}
		}
	}

	void MovementKeyboard () {
		
		if(Input.GetKey(KeyCode.LeftArrow))
		{
			playerImage.transform.eulerAngles  = new Vector3(playerImage.transform.eulerAngles.x, playerImage.transform.eulerAngles.y, 180);
			rigidbody.MovePosition(rigidbody.position + new Vector2(-1 * moveSpeed, 0) * Time.fixedDeltaTime);
		}
		if(Input.GetKey(KeyCode.RightArrow))
		{
			playerImage.transform.eulerAngles  = new Vector3(playerImage.transform.eulerAngles.x, playerImage.transform.eulerAngles.y, 0);
			rigidbody.MovePosition(rigidbody.position + new Vector2(1 * moveSpeed, 0) * Time.fixedDeltaTime);
		}

		if(Input.GetKey(KeyCode.UpArrow))
		{
			playerImage.transform.eulerAngles  = new Vector3(playerImage.transform.eulerAngles.x, playerImage.transform.eulerAngles.y, 90);
			rigidbody.MovePosition(rigidbody.position + new Vector2(0, 1 * moveSpeed) * Time.fixedDeltaTime);
		}
		if(Input.GetKey(KeyCode.DownArrow)) 
		{
			playerImage.transform.eulerAngles  = new Vector3(playerImage.transform.eulerAngles.x, playerImage.transform.eulerAngles.y, -90);
			rigidbody.MovePosition(rigidbody.position + new Vector2(0, -1 * moveSpeed) * Time.fixedDeltaTime);
		}
	}

	void MovementControllers() {
		if (controller1MoveAxisH != null)
		{
			float H = Input.GetAxis (controller1MoveAxisH);
			float V = Input.GetAxis (controller1MoveAxisV);

			// Checks to see if we push a key / stick, then moves
			if (Mathf.Abs (H) < stickTol) 
			{
				H = 0;
			} 

			if (Mathf.Abs (V) < stickTol) 
			{
				V = 0;
			}

			rigidbody.MovePosition (rigidbody.position + new Vector2 (H * moveSpeed, -V * moveSpeed) * Time.fixedDeltaTime);

		}
	}

	public Vector2 GetPosition()
	{
		return rigidbody.position;
	}

	public GlobalEnums.WaveTypes GetCurrentWaveType()
	{
		return currentWaveType;
	}

	public bool isDead()
	{
		return currentActivityState == GlobalEnums.ActivityState.Dead;
	}

	public int BatteryCount()
	{
		return batteriesCollected;
	}

	public void BatteryGrabbed()
	{
		batteriesCollected++;
	}


    void OnCollisionEnter2D(Collision2D col) 
	{
		if(col.gameObject.tag == "Enemy" || col.gameObject.tag == "Wall")
		{
			Physics2D.IgnoreCollision (col.gameObject.GetComponent<Collider2D>(), this.GetComponent<Collider2D>());
			currentActivityState = GlobalEnums.ActivityState.Dead;

			this.GetComponent<AudioSource> ().Stop ();
			this.GetComponent<AudioSource> ().loop = false;
			this.GetComponent<AudioSource> ().clip = deathSound;
			this.GetComponent<AudioSource> ().Play ();

		}
	}

	public void StopAllMusic()
	{
		this.GetComponent<AudioSource> ().Stop ();
	}
}
