using UnityEngine;
using System.Collections;

public class EnemyController : MonoBehaviour {
	//0="standing",1="attacking",2="random walk", 3="dialog"
	public int state;
	public float speed;
	public float visionAngle;
	public float coneDist;
	public GameObject[] nodes;
	public float[] durations;
	public bool nodeLoop;
	private PlayerController player;
	private GUIController gui;
	private int roomNum;
	private int FPS;
	public Transform begin;
	public Transform end;
	public int nodeIndex;
	private bool walkingFinished;

	private float elapsedTime;
	// Use this for initialization
	void Start () {
		state= 0;
		player = GameObject.Find("Player").GetComponent<PlayerController>();
		gui = GameObject.Find("GUICamera").GetComponent<GUIController>();
		FPS = Mathf.CeilToInt(1f/Time.fixedDeltaTime);
		if( nodes.Length == durations.Length && nodes.Length > 1){
			begin = nodes[0].transform;
			end = nodes[1].transform;
			nodeIndex = 1;
			//transform.position = begin.position;
			//transform.forward = end.position-begin.position;
			elapsedTime = 0f;
			walkingFinished = false;
		}
	}

	void FixedUpdate(){
		if( state==1 ){ AttackState(); }
		else if( state==2 ){ SearchState(); }
		else if( state==3 ){ DialogState(); }
	}

	public void EnterRoom( int room ){
		roomNum = room;
		int playerItem = player.GetItemType();
		if( room != 3 ){
			if(playerItem == 1){ state = 1; gameObject.AddComponent<EnemyAttack>(); }
			else if(playerItem == 2){ state = 2; if( nodes.Length > 0){ transform.position = begin.position; transform.forward = end.position-begin.position; } }
			else if(playerItem == 3){ state = 3; }
		}
		else{
			//play dialog
			gui.SetDialog();
		}
	}

	void AttackState(){
		//chase Player
		Vector3 playerDirection = (player.transform.position - transform.position).normalized;
		Vector3 planarDir = new Vector3(playerDirection.x*speed, 0f, playerDirection.z*speed);
		rigidbody.velocity = planarDir;
		transform.forward = planarDir;
		//steering behavior
	}

	void SearchState(){
		elapsedTime += Time.fixedDeltaTime;
		//turn on spotlight
		Light l = gameObject.GetComponentInChildren<Light>();
		l.enabled = true;
		//walk around
		if(!walkingFinished){ NodeWalk(); }
		//if player in cone of vision kill
		Debug.Log ("Checkpoint");
		if( Vector3.Angle(player.transform.position-transform.position ,transform.forward) < 30f ){
			//also check that player is within a distance from enemy
			if( Vector3.Distance(transform.position, player.transform.position) < coneDist ){
				Debug.Log ("Player in cone!");
				l.color = Color.red;
				//Display the caught dialog.
				gui.isCaught = true;
				StartCoroutine(Die());
			}
		}
	}

	IEnumerator Die(){
		yield return new WaitForSeconds(2);
		Application.LoadLevel("Dead");
	}

	void NodeWalk(){
		if( nodes.Length == durations.Length &&nodes.Length > 0 ){
			if(elapsedTime >= durations[nodeIndex-1]){
				elapsedTime = 0f;
				begin = end;
				nodeIndex++;
				if( nodeIndex == nodes.Length ){  
					if(nodeLoop){nodeIndex = 1;}
					else{walkingFinished = true;}
				}
				if( !walkingFinished ){  end = nodes[nodeIndex].transform; transform.forward = end.position-begin.position; }
			}
			else{ rigidbody.velocity = (end.position-begin.position)/durations[nodeIndex-1]; }

		}
		else{ }
	}

	void DialogState(){
		//set dialog flags in GUIController
		gui.SetDialog();
	}
}
