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

public class playerScript : MonoBehaviour {

	playerControllerScript playerOneController;
	playerTwoControllerScript playerTwoController;

	public GameObject R2Image;

	// Player 1
	Vector3 pOneInput;
	Vector3 pOneVelocity;
	float pOneVelocityXSmoothing;
	float pOneVelocityYSmoothing;

	public string axisNameX;
	public string axisNameY;
	public string sprintButton;

	public GameObject cooldownButt;

	// Player 2
	Vector3 pTwoInput;
	Vector3 pTwoVelocity;
	float pTwoVelocityXSmoothing;
	float pTwoVelocityYSmoothing;


	public float pOneSpeed = 5.0f;
	public float pTwoSpeed = 5.0f;
	public float sprintSpeed = 10.0f;
	public float sprintTime = 0.2f;
	float sprintCooldown = 5.0f;
	float pOneSprintCooldownTimer;
	float pTwoSprintCooldownTimer;
	bool pOneSprinted = false;
	bool pTwoSprinted = false;

	bool facingRight = false;

	public float speed = 5.0f;
	private float defSpeed;
	public float ghostSpeed = 2f;
	private bool reverse;

	// Smooth Movement
	public float smoothTime = 0.3f;

	private Animator anim;

	// Use this for initialization
	void Start () {

		R2Image.GetComponent<Image> ().enabled = false;

		anim = gameObject.GetComponent<Animator> ();

		playerOneController = gameObject.GetComponent<playerControllerScript> ();
		playerTwoController = gameObject.GetComponent<playerTwoControllerScript> ();

		pOneSprintCooldownTimer = sprintCooldown;

		defSpeed = speed;

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

	//	if (gameObject.name == "Player 1") {

			// Player One Input and Movement
			/*pOneInput = new Vector3 (Input.GetAxisRaw ("j1Horizontal"), Input.GetAxisRaw("j1Vertical"), 0);
			pOneVelocity = (new Vector3(pOneInput.x, pOneInput.y, pOneInput.z) * speed) * Time.deltaTime;
			playerOneController.Move (pOneVelocity);*/

			pOneInput = new Vector3 (Input.GetAxisRaw (axisNameX), Input.GetAxisRaw (axisNameY), 0);

			if (reverse)
				pOneInput = pOneInput * -1;

			if (pOneInput.x != 0 || pOneInput.y != 0) {
				anim.SetBool ("Running", true);
			} else {
				anim.SetBool ("Running", false);
			}

			float targetXVel = pOneInput.x * pOneSpeed;
			float targetYVel = pOneInput.y * pOneSpeed;
			pOneSmoothMovement (targetXVel, targetYVel);

			playerOneController.Move (pOneVelocity * Time.deltaTime);

			if(Input.GetButtonDown(sprintButton) && pOneSpeed != sprintSpeed && playerOneController.holding == false && pOneSprinted == false){

				StartCoroutine(pOneSprint());

			}

			if(pOneSprinted == true){

				pOneSprintCooldownTimer -= Time.deltaTime;
				
			}

			if(pOneSprintCooldownTimer <= 0){

				pOneSprinted = false;
				pOneSprintCooldownTimer = sprintCooldown; 

			}

			if (pOneInput.x > 0 && !facingRight)		{ Flip(); }
			else if (pOneInput.x < 0 && facingRight) 	{ Flip(); }


		if (pOneSprinted == true) {
			R2Image.GetComponent<Image> ().enabled = false;
		} else {
			R2Image.GetComponent<Image> ().enabled = true;
		}
			//Debug.Log (sprintCooldownTimer);


	/*	} else if (gameObject.name == "Player 2"){

			// Player Two Input and Movement
			pTwoInput = new Vector3 (Input.GetAxis ("j2Horizontal"), Input.GetAxis("j2Vertical"), 0);
			if (pTwoInput.magnitude < 1) {
				pTwoInput = Vector3.zero;
			}

			if (reverse)
				pTwoInput = pTwoInput * -1;

			if (pTwoInput.x != 0 || pTwoInput.y != 0) {
				anim.SetBool ("Running", true);
			} else {
				anim.SetBool ("Running", false);
			}

			float targetXVel = pTwoInput.x * pTwoSpeed;
			float targetYVel = pTwoInput.y * pTwoSpeed;
			pTwoSmoothMovement (targetXVel, targetYVel);

			playerTwoController.Move (pTwoVelocity * Time.deltaTime);

			if(Input.GetButtonDown("j2Sprint") && pTwoSpeed != sprintSpeed && playerTwoController.holding == false && pTwoSprinted == false){

				StartCoroutine(pTwoSprint());

			}

			if(pTwoSprinted == true){

				pTwoSprintCooldownTimer -= Time.deltaTime;

			}

			if(pTwoSprintCooldownTimer <= 0){

				pTwoSprinted = false;
				pTwoSprintCooldownTimer = sprintCooldown; 

			}

			if (pTwoInput.x > 0 && !facingRight)		{ Flip(); }
			else if (pTwoInput.x < 0 && facingRight) 	{ Flip(); }


		}
*/
	}

	void pOneSmoothMovement(float targetXVelocity, float targetYVelocity){

		// Smoothing the movement
		pOneVelocity.x = Mathf.SmoothDamp (pOneVelocity.x, targetXVelocity, ref pOneVelocityXSmoothing, smoothTime);
		pOneVelocity.y = Mathf.SmoothDamp (pOneVelocity.y, targetYVelocity, ref pOneVelocityYSmoothing, smoothTime);

	}

	void pTwoSmoothMovement(float targetXVelocity, float targetYVelocity){

		// Smoothing the movement
		pTwoVelocity.x = Mathf.SmoothDamp (pTwoVelocity.x, targetXVelocity, ref pTwoVelocityXSmoothing, smoothTime);
		pTwoVelocity.y = Mathf.SmoothDamp (pTwoVelocity.y, targetYVelocity, ref pTwoVelocityYSmoothing, smoothTime);

	}

	IEnumerator pOneSprint(){

		float origSpeed = pOneSpeed;

		pOneSpeed = sprintSpeed; 

		pOneSprinted = true;

		yield return new WaitForSeconds (sprintTime);

		pOneSpeed = origSpeed;

	}

	IEnumerator pTwoSprint(){

		float origSpeed = pTwoSpeed;

		pTwoSpeed = sprintSpeed; 

		pTwoSprinted = true;

		yield return new WaitForSeconds (sprintTime);

		pTwoSpeed = origSpeed;

	}

	private void Flip ()
	{
		// Switch the way the player is labelled as facing.
		facingRight = !facingRight;

		// Multiply the player's x local scale by -1.
		Vector3 theScale = transform.localScale;
		theScale.x *= -1;
		transform.localScale = theScale;
	}

	void Slow(){
		StartCoroutine (SlowGhost ());
	}

	void Reverse(){
		StartCoroutine (ReverseGhost());
		Debug.Log ("reverse it");
	}

	IEnumerator SlowGhost(){
		pOneSpeed = ghostSpeed;
		pTwoSpeed = ghostSpeed;
		yield return new WaitForSeconds (5);
		pOneSpeed = defSpeed;
		pTwoSpeed = defSpeed;
	}

	IEnumerator ReverseGhost(){
		reverse = true;
		yield return new WaitForSeconds (5);
		reverse = false;
	}

}
