using UnityEngine;
using System.Collections;

public class HumanController : MonoBehaviour {

	[SerializeField] protected StoppingPoint[] pathway;
	[SerializeField] protected Animator personAnim;
	public bool stationaryFirst;
	public float speed = 1f;
	public bool isDead = false;

	private int goingToPoint = 1;
	private int originPoint = 0;
	private Vector3 originPosition;
	private Vector3 goingToPosition;
	private bool moving = false;
	private int scareState = 0;

	//variables for the Vector lerping
	private float startWalk;
	private float walkDistance;

	//starts person moving to the person
	void Start () {
		if(!stationaryFirst)
			StartCoroutine(MoveTo (1));
	}

	//moves to the indicated index
	private IEnumerator MoveTo(int index)
	{
		originPoint = goingToPoint;
		goingToPoint = index;
		if (pathway [goingToPoint].adjacentDoor != null && pathway [goingToPoint].adjacentDoor.isClosed) {
			moving = false;
			startWalk = 0;
			yield return new WaitForSeconds (1f);
		}
		moving = true;
		originPosition = transform.position;
		goingToPosition = pathway [goingToPoint].transform.position;

		personAnim.SetBool ("idle", false);

		if (originPosition.x > goingToPosition.x) {
			if (personAnim.GetInteger ("horizontal") >= 0)
				personAnim.SetInteger ("horizontal", -1);
		}
		else if (originPosition.x < goingToPosition.x) {
			if (personAnim.GetInteger ("horizontal") <= 0)
				personAnim.SetInteger ("horizontal", 1);
		}

		startWalk = Time.time;
		walkDistance = Vector3.Distance (originPosition, goingToPosition);
	}

	//proceeds to go through StoppingPoints 
	private IEnumerator ExitTo(StoppingPoint point)
	{
		if (point.adjacentDoor != null && point.adjacentDoor.isClosed) {
			moving = false;
			yield return new WaitForSeconds (1f);
			MoveTo (goingToPoint);
		}
		originPosition = transform.position;
		goingToPosition = point.transform.position;

		personAnim.SetBool ("idle", false);

		if (originPosition.x > goingToPosition.x) {
			if (personAnim.GetInteger ("horizontal") >= 0)
				personAnim.SetInteger ("horizontal", -1);
		}
		else if (originPosition.x < goingToPosition.x) {
			if (personAnim.GetInteger ("horizontal") <= 0)
				personAnim.SetInteger ("horizontal", 1);
		}

		startWalk = Time.time;
		walkDistance = Vector3.Distance (originPosition, goingToPosition);
	}

	private void StopMoving()
	{
		moving = false;
		personAnim.SetBool ("idle", true);
	}

	//gets cued when scare is prompted
	public void ScareHuman()
	{
		scareState++;
		speed += .1f;

		if (scareState == 1) {
			personAnim.SetBool ("scared", true);
			StartCoroutine (MoveTo ((goingToPoint + 1) % pathway.Length));
		}
		if (scareState == 2) {
			//FindExit ();
		}
			
		/*foreach (StoppingPoint stop in closeToExit) {
			if (stop == pathway [originPoint]) {
				ExitTo (originPoint);
			}
			if (stop == pathway [goingToPoint]) {
				ExitTo (goingToPoint);
			}
		}*/
	}

	private void FindExit()
	{
		StoppingPoint direction = pathway[goingToPoint];
		foreach (StoppingPoint adj in pathway[goingToPoint].adjacentPoints) {
			if (adj != null && adj.stepsToExit < direction.stepsToExit)
				direction = adj;
		}
		StartCoroutine(ExitTo (direction));
	}

	private void PrepareToDie()
	{
		isDead = true;
		StopMoving ();
		personAnim.SetTrigger ("dead");
	}

	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown (KeyCode.Space)) {
			ScareHuman ();
		}
		if (moving) {
			float distCovered = (Time.time - startWalk) * speed;
			float fracJourney = distCovered / walkDistance;
			if (fracJourney > 1)
				fracJourney = 1;
			transform.position = Vector3.Lerp (originPosition, pathway [goingToPoint].transform.position, fracJourney);
			if (fracJourney == 1) {

				switch (scareState) {
				case 0:
					if(!stationaryFirst)
					StartCoroutine (MoveTo ((goingToPoint + 1) % pathway.Length));
					break;
				case 1:
					if(!stationaryFirst)
						StartCoroutine (MoveTo (originPoint % pathway.Length));
					break;
				case 2:
					FindExit ();
					break;
				case 3:
					PrepareToDie ();
					break;
				}
					
			}

		}
	}
}
