using UnityEngine;
using System.Collections;

public class Human : MonoBehaviour {

	public Transform[] trWaypoints;
	public float flSpeed;
	Transform tr; 
	int currentArea;
	public int newArea;
	int travelArea;
	int callFromRoom;
	Animator ator;
	AudioSource aus;
	public GameObject ExcMark;

	public string strCurrentRoom;
	public string strPreviousRoom;

	// Use this for initialization
	void Start () {
		tr = transform;
		ator = GetComponent<Animator>();
		aus = GetComponent<AudioSource>();
		currentArea = 0;
		newArea = 0;
		travelArea = 0;
		//ExcMark = GetComponent<SpriteRenderer> ();
	}
	
	public void doChangePos (int _intPos)
	{

		newArea = _intPos;

		ExcMark.SetActive (true);
		CancelInvoke ("setExcMark");
		Invoke ("setExcMark", 1);

		setTravelArea ();
		setCurrentArea ();
		StopCoroutine ("moveToArea");
		StartCoroutine ("moveToArea");
	}

	void Update() {

		if ( currentArea != newArea ) {

			ator.SetBool("isWalking", true);

			aus.volume = 1;

		} else {

			ator.SetBool("isWalking", false);

			aus.volume = 0;

		}

		if ( tr.position.x < (trWaypoints[travelArea].position.x-0.01f) ) {

			transform.localScale = Vector3.one -Vector3.right*2;

		} else  if ( tr.position.x > (trWaypoints[travelArea].position.x +0.01f) ) {

			transform.localScale = Vector3.one;

		}

		setTravelArea ();
		setCurrentArea ();

	}

	void setExcMark()
	{
		ExcMark.SetActive (false);
	}

	void setTravelArea()
	{
		if (currentArea == travelArea) {
			if (currentArea == 0 && (newArea == 1 || newArea == 2)) {
				travelArea = 11;
			} else if (currentArea == 0 && newArea == 4) {
				travelArea = 8;
			} else if (currentArea == 0 && (newArea == 5 || newArea == 6 || newArea == 7 || newArea == 9)) {
				travelArea = 10;
			} else if (currentArea == 1 && newArea != 11) {
				travelArea = 11;
			} else if (currentArea == 2 && newArea != 11) {
				travelArea = 11;
			} else if (currentArea == 3 && newArea != 0) {
				travelArea = 0;
			} else if (currentArea == 4 && newArea != 8) {
				travelArea = 8;
			} else if (currentArea == 5 && newArea != 10) {
				travelArea = 10;
			} else if (currentArea == 6 && newArea != 9) {
				travelArea = 9;
			} else if (currentArea == 7 && newArea != 9) {
				travelArea = 9;
			} else if (currentArea == 8 && newArea != 4) {
				travelArea = 0;
			} else if (currentArea == 9 && newArea != 6 && newArea != 7) {
				travelArea = 10;
			} else if (currentArea == 10 && (newArea == 6 || newArea == 7)) {
				travelArea = 9;
			} else if (currentArea == 10 && (newArea == 1 || newArea == 2 || newArea == 3 || newArea == 4 || newArea == 8 || newArea == 11)) {
				travelArea = 0;
			} else if (currentArea == 11 && newArea != 1 && newArea != 2) {
				travelArea = 0;
			} else {
				travelArea = newArea;
			}
		}
	}

	void setCurrentArea()
	{
		if (tr.position == trWaypoints [travelArea].position) {
			currentArea = travelArea;
		}
	}

	IEnumerator moveToArea()
	{
		while (tr.position != trWaypoints [newArea].position) {
			tr.position = Vector3.MoveTowards (tr.position, trWaypoints [travelArea].position, flSpeed * Time.deltaTime);
			yield return 0;
		}
	}

	void OnTriggerEnter2D(Collider2D _col)
	{

		if (_col.gameObject.layer == 9) {

			doChangeCurrentRoom(_col.gameObject.name);

		}
	}

	void OnTriggerExit2D(Collider2D _col)
	{

		if (_col.gameObject.layer == 9 && _col.gameObject.name == strCurrentRoom) {

			doChangeCurrentRoom("");

		}
	}

	void doChangeCurrentRoom(string _strRoom) {

		if ( _strRoom == "" ) {

			strCurrentRoom = strPreviousRoom;

		} else {

			strPreviousRoom = strCurrentRoom;
			strCurrentRoom = _strRoom;

		}

	}
}