using UnityEngine;
using System.Collections;

public class MoveTo : MonoBehaviour 
{
	//script is for enemy behaviour.


	public Transform target;
	public Transform playerPos;
	public GameObject Target = null; //We will feed a target into this later.
	#region New
	public Vector3 faceTarget;
	public float speed;
	#endregion
	
	//Waypoint//
	public Transform[] waypoints;

	public int curWaypoint;
	public float distancePlayer; //distance between enemy and player
	public float distancePlayerToWaypoint; //distance between player and health
	/// 
	public bool sm, med, larg;
	public string size;
	public float timer;
	public bool canSound = true;
	public Vector3 myVec;
	public GameObject prefab;
	public string StateName;
	public int idleTime;
	public bool canIdle;
	public float hearing;
	public GameObject markerPrefab;
	public GameObject myMarker;
	void Start ()
	{
		playerPos = GameObject.Find("Player").GetComponent<Transform>(); //getting player's transform values.
		target = waypoints[0];
		Target = null;
		idleTime = Random.Range(3,20);

		Idle();
		canIdle = true;
		StateName = "Idle";

	}


	// Update is called once per frame
	void Update () 
	{
		UnityEngine.AI.NavMeshAgent agent = this.GetComponent<UnityEngine.AI.NavMeshAgent>();
		#region New
		faceTarget = new Vector3(playerPos.position.x,0,playerPos.position.z);
		
		Vector3 targetDir = faceTarget - transform.position;
		float step = speed * Time.deltaTime;
		Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F);
		Debug.DrawRay(transform.position, newDir, Color.red);
		transform.rotation = Quaternion.LookRotation(newDir);
		#endregion

		if(target == null)
		{
			agent.stoppingDistance = 0;
			timer = 0;

			Idle();
			StateName = "Idle";
		}
		agent.destination = target.position;
		
		distancePlayer = Vector3.Distance(playerPos.position,transform.position);
		distancePlayerToWaypoint = Vector3.Distance(playerPos.position, target.transform.position);



			
		if(StateName == "Idle")
		{
			timer += Time.deltaTime;
			if(timer >= idleTime)
			{
				timer = 0;
				canIdle = false;

				idleTime = Random.Range(3,20);

			}
		}

		if(GameObject.FindGameObjectWithTag("Mark") != null && distancePlayer > 2)
		{
			float fireDist = Vector3.Distance(this.transform.position, GameObject.FindGameObjectWithTag("Mark").transform.position);
			if(fireDist <= 15)
			{				
				StateName = "Mark";


				target = GameObject.FindGameObjectWithTag("Mark").transform;

			}
			if(target == null)
			{
				agent.stoppingDistance = 0;
				timer = 0;

				Idle();
				StateName = "Idle";
			}
		}
		else
		{
			if(target == null)
			{
				agent.stoppingDistance = 0;
				timer = 0;

				Idle();
				StateName = "Idle";
			}
			if(!canIdle)
			{
				if(playerPos.gameObject.GetComponentInChildren<MakePing>().Loudness == "Loud")
				{
					if(distancePlayer <= 8)
					{
						if(myMarker == null)
						{
							myMarker = Instantiate(markerPrefab,playerPos.position,Quaternion.identity);
							
						}
						else
						{
							Destroy(myMarker);
							myMarker = Instantiate(markerPrefab,playerPos.position,Quaternion.identity);
						}
					}
				}
				else if(playerPos.gameObject.GetComponentInChildren<MakePing>().Loudness == "Medium")
				{
					if(distancePlayer <= 5)
					{
						if(myMarker == null)
						{
							myMarker = Instantiate(markerPrefab,playerPos.position,Quaternion.identity);
							
						}
						else
						{
							Destroy(myMarker);
							myMarker = Instantiate(markerPrefab,playerPos.position,Quaternion.identity);
						}
					}
				}
				else if(playerPos.gameObject.GetComponentInChildren<MakePing>().Loudness == "Quiet")
				{
					if(distancePlayer <= 3)
					{
						if(myMarker == null)
						{
							myMarker = Instantiate(markerPrefab,playerPos.position,Quaternion.identity);
							
						}
						else
						{
							Destroy(myMarker);
							myMarker = Instantiate(markerPrefab,playerPos.position,Quaternion.identity);
						}
					}
				}
				else
				{
					if(distancePlayer <= 2)
					{
						Destroy(myMarker);
					}
				}
				
				if(myMarker == null)
				{
					if(distancePlayer <= 2)
					{
						agent.stoppingDistance = 1;
						
						Debug.DrawLine(playerPos.position,transform.position); //can visually see the line between player position vs. enemy. (in scene view in Unity)
						Chase();
						StateName = "Chase";
					}
					
					
					else
					{
						agent.stoppingDistance = 0;
						
						Patrol();
						StateName = "Patrol";
						timer += Time.deltaTime;
						if(timer >= idleTime)
						{
							timer = 0;
							canIdle = true;
							
							idleTime = Random.Range(3,20);
						}
						
					}
				}
				if(myMarker != null)
				{
					agent.stoppingDistance = 0;
					
					Seek ();
					StateName = "Seek";
					
				}
				
			}
			
			if(canIdle)
			{
				if(playerPos.gameObject.GetComponentInChildren<MakePing>().Loudness == "Loud")
				{
					if(distancePlayer <= 8)
					{
						if(myMarker == null)
						{
							myMarker = Instantiate(markerPrefab,playerPos.position,Quaternion.identity);
							
						}
						else
						{
							Destroy(myMarker);
							myMarker = Instantiate(markerPrefab,playerPos.position,Quaternion.identity);
						}
					}
				}
				else if(playerPos.gameObject.GetComponentInChildren<MakePing>().Loudness == "Medium")
				{
					if(distancePlayer <= 5)
					{
						if(myMarker == null)
						{
							myMarker = Instantiate(markerPrefab,playerPos.position,Quaternion.identity);
							
						}
						else
						{
							Destroy(myMarker);
							myMarker = Instantiate(markerPrefab,playerPos.position,Quaternion.identity);
						}
					}
				}
				else if(playerPos.gameObject.GetComponentInChildren<MakePing>().Loudness == "Quiet")
				{
					if(distancePlayer <= 3)
					{
						if(myMarker == null)
						{
							myMarker = Instantiate(markerPrefab,playerPos.position,Quaternion.identity);
							
						}
						else
						{
							Destroy(myMarker);
							myMarker = Instantiate(markerPrefab,playerPos.position,Quaternion.identity);
						}
					}
				}
				else
				{
					if(distancePlayer <= 2)
					{
						Destroy(myMarker);
					}
				}
				
				if(distancePlayer >= 2)
				{	
					if(myMarker == null)
					{
						agent.stoppingDistance = 0;
						Idle();
						StateName = "Idle";
					}
					if(myMarker != null)
					{
						agent.stoppingDistance = 0;
						
						Seek ();
						StateName = "Seek";
					}
				}
				else if(distancePlayer <= 2)
				{
					
					agent.stoppingDistance = 1;
					
					Debug.DrawLine(playerPos.position,transform.position); //can visually see the line between player position vs. enemy. (in scene view in Unity)
					Chase();
					StateName = "Chase";
					
					
				}
				
			}
		}

			

	}

	void Patrol()
	{

	
		target = waypoints[curWaypoint];
		target.position = waypoints[curWaypoint].transform.position;
		if(transform.position.x == waypoints[curWaypoint].transform.position.x && transform.position.z == waypoints[curWaypoint].transform.position.z)
		{
			curWaypoint ++;
		}


		if(curWaypoint >= waypoints.Length)
		{
			curWaypoint = 0;
		}


	}

	void Chase ()
	{

		target = playerPos;
		target.position = playerPos.position;
	}

	void Idle ()
	{
		target = this.gameObject.transform;

	}
	void Seek ()
	{

		target = myMarker.transform;
		if(transform.position.x == myMarker.transform.position.x && transform.position.z == myMarker.transform.position.z)
		{
			Instantiate(Resources.Load("Prefab/Small/Trigger_Small_Enemy"),this.transform.position,Quaternion.identity);//pulse
			Destroy(myMarker);
			timer = 0;
			Idle ();
			StateName = "Idle";

		}
	}


	void Dead ()
	{
		Destroy (this.gameObject);
	}


}
