using UnityEngine;
using System.Collections;

public class playerTwoScript : MonoBehaviour {

	playerTwoControllerScript playerTwoController;

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

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

	// Smooth Movement
	public float smoothTime = 0.3f;

	// Use this for initialization
	void Start () {

		defSpeed = speed;

		playerTwoController = gameObject.GetComponent<playerTwoControllerScript> ();

	}

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


			// Player Two Input and Movement
			// Player One Input and Movement
			pTwoInput = new Vector3 (Input.GetAxisRaw ("j2Horizontal"), Input.GetAxisRaw("j2Vertical"), 0);
			float targetXVel = pTwoInput.x * speed;
			float targetYVel = pTwoInput.y * speed;
			pTwoSmoothMovement (targetXVel, targetYVel);

			if (reverse)
				pTwoInput = pTwoInput * -1;

			playerTwoController.Move (pTwoVelocity * Time.deltaTime);

	}

	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);

	}

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

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

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

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