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

public class CharacterMovementController : MonoBehaviour {

	[SerializeField]
	private float mWalkForce = 10f;
	[SerializeField]
	private float MaxVelocity = 2.0f;
	private bool sloMo = false;
	public bool canMove = true;
	[SerializeField]
	private float sloMoStartTime = 1f;
	private float sloMoCurrentTime;
	private bool slomoRecharging = false;
	[SerializeField]
	private Image sloImage;
	
	//Following are necessary for directional trap
	public KeyCode moveUp = KeyCode.W;
	public KeyCode moveDown = KeyCode.S;
	public KeyCode moveLeft = KeyCode.A;
	public KeyCode moveRight = KeyCode.D;
	// Use this for initialization
	void Start ()
	{
		Debug.Log ("Speed Mod: " + GetComponent<CharacterRandomiser> ().GetPerson ().GetSpeedMod () +
						" || Size Mod: " + GetComponent<CharacterRandomiser> ().GetPerson ().GetSizeMod ());
		MaxVelocity = MaxVelocity * GetComponent<CharacterRandomiser> ().GetPerson ().GetSpeedMod();
		transform.localScale = new Vector3 (transform.localScale.x * GetComponent<CharacterRandomiser> ().GetPerson ().GetSizeMod (),
		                                   transform.localScale.y * GetComponent<CharacterRandomiser> ().GetPerson ().GetSizeMod (),
		                                   1.0f);
		sloMoCurrentTime = sloMoStartTime;
	}

	void Update ()
	{
		if(Input.GetKey(moveUp))
		{
			if (rigidbody2D.velocity.y > MaxVelocity)
				rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, MaxVelocity);
			else
			rigidbody2D.AddForce(new Vector2(0,mWalkForce));
		}
		if(Input.GetKey(moveLeft))
		{
			if (rigidbody2D.velocity.x < -MaxVelocity)
				rigidbody2D.velocity = new Vector2(-MaxVelocity, rigidbody2D.velocity.y);
			else
			rigidbody2D.AddForce(new Vector2(-mWalkForce,0));
		}
		if(Input.GetKey(moveDown))
		{
			if (rigidbody2D.velocity.y < -MaxVelocity)
				rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, -MaxVelocity);
			else
			rigidbody2D.AddForce(new Vector2(0,-mWalkForce));
		}
		if(Input.GetKey(moveRight))
		{
			if (rigidbody2D.velocity.x > MaxVelocity)
				rigidbody2D.velocity = new Vector2(MaxVelocity, rigidbody2D.velocity.y);
			else
			rigidbody2D.AddForce(new Vector2(mWalkForce,0));
		}
		if(Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))
		{
			sloMo = true;
		}
		if(Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))
		{
			sloMo = false;
		}

		if(sloMo)
		{
			sloMoCurrentTime -= Time.deltaTime*2;
			if(sloMoCurrentTime <= 0)
			{
				sloMo = false;
			}
			else
			{
				Time.timeScale = 0.5f;
			}
		}
		else
		{
			if(sloMoCurrentTime <= sloMoStartTime)
			{
				sloMoCurrentTime += Time.deltaTime/2;
			}
			Time.timeScale = 1f;
		}
		sloImage = GameObject.Find("SlowBar").GetComponent<Image>();
		sloImage.GetComponent<Image>().fillAmount = sloMoCurrentTime/sloMoStartTime;
	}
	
	public void resetDirections()
	{
		moveUp = KeyCode.W;
		moveDown = KeyCode.S;
		moveLeft = KeyCode.A;
		moveRight = KeyCode.D;
	}
}
