using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MakePing : MonoBehaviour {
	public AudioClip clip;
	public float clipLegnth;
	public GameObject prefabPulse,fadeLight;
	public float timer;
	public Vector3 myVec;
	public Light light;
	public bool MP,M,P,W,E;
	public bool small,medium,large;
	public float lightSize,smaLight, medLight,larLight;
	private string size;
	public bool lightfading = false;
	public GameObject PlayerLight;
	public Vector3 lightPos;
	public float smoothTime = 0.5F,pulseSpeed;
	private Vector3 velocity = Vector3.zero;
	public string Loudness;
	//55.25765
	// Use this for initialization
	void Awake()
	{
		smaLight = 15;
		medLight = 35;
		larLight = 55;
		if(MP)
		{
			lightPos = new Vector3(0,8,0);
			PlayerLight.GetComponent<Light>().enabled = false;

		}

		if(small)
		{
			size = "Small";
			lightSize = smaLight;
			if(E)
			{
				fadeLight = Resources.Load("Prefab/Small/Red")as GameObject;
			}
			if(M || MP)
			{
				fadeLight = Resources.Load("Prefab/Small/Yellow")as GameObject;
			}
			if(W)
			{
				fadeLight = Resources.Load("Prefab/Small/Blue")as GameObject;
			}
			if(P)
			{
				fadeLight = Resources.Load("Prefab/Small/Green")as GameObject;
			}
		}
		if(medium)
		{
			size = "Medium";

			lightSize = medLight;
			if(E)
			{
				fadeLight = Resources.Load("Prefab/Medium/Red")as GameObject;
			}
			if(M || MP)
			{
				fadeLight = Resources.Load("Prefab/Medium/Yellow")as GameObject;
			}
			if(W)
			{
				fadeLight = Resources.Load("Prefab/Medium/Blue")as GameObject;
			}
			if(P)
			{
				fadeLight = Resources.Load("Prefab/Medium/Green")as GameObject;
			}
		}
		if(large)
		{
			size = "Large";

			lightSize = larLight;
			if(E)
			{
				fadeLight = Resources.Load("Prefab/Large/Red")as GameObject;
			}
			if(M || MP)
			{
				fadeLight = Resources.Load("Prefab/Large/Yellow")as GameObject;
			}
			if(W)
			{
				fadeLight = Resources.Load("Prefab/Large/Blue")as GameObject;
			}
			if(P)
			{
				fadeLight = Resources.Load("Prefab/Large/Green")as GameObject;
			}
		}
	}
	void Start () 
	{
		prefabPulse = Resources.Load("Prefab/"+size+"/Pulse")as GameObject;
		light = this.GetComponentInChildren<Light>();
		light.spotAngle = 0;
		clip = this.GetComponent<AudioSource>().clip;
		clipLegnth = clip.length;

	}
	
	// Update is called once per frame
	void Update () 
	{
		if(small)
		{
			size = "Small";
			lightSize = smaLight;
			if(E)
			{
				fadeLight = Resources.Load("Prefab/Small/Red")as GameObject;
			}
			if(M || MP)
			{
				fadeLight = Resources.Load("Prefab/Small/Yellow")as GameObject;
				pulseSpeed = 0.45f;
			}
			if(W)
			{
				fadeLight = Resources.Load("Prefab/Small/Blue")as GameObject;
				pulseSpeed = 0.29f;

			}
			if(P)
			{
				fadeLight = Resources.Load("Prefab/Small/Green")as GameObject;
			}
		}
		if(medium)
		{
			size = "Medium";
			
			lightSize = medLight;
			if(E)
			{
				fadeLight = Resources.Load("Prefab/Medium/Red")as GameObject;
			}
			if(M || MP)
			{
				fadeLight = Resources.Load("Prefab/Medium/Yellow")as GameObject;
				pulseSpeed = 0.37f;
			}
			if(W)
			{
				fadeLight = Resources.Load("Prefab/Medium/Blue")as GameObject;
			}
			if(P)
			{
				fadeLight = Resources.Load("Prefab/Medium/Green")as GameObject;
			}
		}
		if(large)
		{
			size = "Large";
			
			lightSize = larLight;
			if(E)
			{
				fadeLight = Resources.Load("Prefab/Large/Red")as GameObject;
			}
			if(M || MP)
			{
				fadeLight = Resources.Load("Prefab/Large/Yellow")as GameObject;
				pulseSpeed = 0.25f;
			}
			if(W)
			{
				fadeLight = Resources.Load("Prefab/Large/Blue")as GameObject;
			}
			if(P)
			{
				fadeLight = Resources.Load("Prefab/Large/Green")as GameObject;
			}
		}
		prefabPulse = Resources.Load("Prefab/"+size+"/Pulse")as GameObject;

		myVec = new Vector3(this.transform.position.x,0.1f,this.transform.position.z);

		if(!(MP || M || (W && small)))
		{
			if(light.spotAngle < lightSize)
			{
				light.spotAngle += 0.9f * Time.deltaTime * 60;
			}
			if(light.spotAngle > lightSize)
			{
				light.spotAngle = lightSize;
			}
			if(clipLegnth > 0)
			{
				clipLegnth -= Time.deltaTime;
				timer += Time.deltaTime;
				if(timer >= 0.25f)
				{
					GameObject myObject = null;

					timer = 0;
					myObject = Instantiate(prefabPulse,myVec,Quaternion.identity);
					myObject.transform.parent = this.transform;

				}
			}
			else
			{
				myVec = new Vector3(this.transform.position.x,8f,this.transform.position.z);
				GameObject myObject = null;

				myObject = Instantiate(fadeLight,myVec,Quaternion.identity);
				myObject.transform.parent = this.transform;

				Destroy(this.gameObject);
			}
		}
		else if(W && small)
		{
			GameObject myObject = null;
			if(	this.GetComponent<AudioSource>().loop != true )
			{
				this.GetComponent<AudioSource>().Play();
				this.GetComponent<AudioSource>().loop = true;
				
			}
			if(light.spotAngle < lightSize)
			{
				light.spotAngle += 0.9f * Time.deltaTime * 60;
			}
			if(light.spotAngle > lightSize)
			{
				light.spotAngle -= 0.9f * Time.deltaTime * 60;
			}
			timer += Time.deltaTime;
			if(timer >= pulseSpeed)
			{
				timer = 0;
				myObject = Instantiate(prefabPulse,myVec,Quaternion.identity);
				
				myObject.transform.parent = this.transform;
				
			}
		}
		else if(MP)
		{ 

			GameObject target = GameObject.Find("Player");
			GameObject myObject = GameObject.Find("World");
			Vector3 Tget = new Vector3(target.transform.position.x,8,target.transform.position.z);

			if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical")!=0)
			{
				if(large && Loudness != "Loud")
				{
					Loudness = "Loud";
				}
				if(medium && Loudness != "Medium")
				{
					Loudness = "Medium";
				}
				if(small && Loudness != "Quiet")
				{
					Loudness = "Quiet";
				}
				PlayerLight.GetComponent<Light>().enabled = true;
				if(	this.GetComponent<AudioSource>().loop != true )
				{
					this.GetComponent<AudioSource>().Play();
					this.GetComponent<AudioSource>().loop = true;

				}

				lightfading = true;
				if(light.spotAngle < lightSize)
				{
					light.spotAngle += 0.9f * Time.deltaTime * 60;
				}
				if(light.spotAngle > lightSize)
				{
					light.spotAngle -= 0.9f * Time.deltaTime * 60;
				}
				timer += Time.deltaTime;
				if(timer >= pulseSpeed)
				{
					timer = 0;
					Instantiate(prefabPulse,myVec,Quaternion.identity);

					PlayerLight.transform.parent = myObject.transform;

				}


				PlayerLight.transform.position = Vector3.SmoothDamp(PlayerLight.transform.position, Tget, ref velocity, smoothTime);
			}
			else if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical")==0)
			        
			{
				Loudness = "";
				PlayerLight.transform.position = Vector3.SmoothDamp(PlayerLight.transform.position, Tget, ref velocity, smoothTime);
				this.GetComponent<AudioSource>().loop = false;


				if(lightfading)
				{


					if(PlayerLight.GetComponent<Light>().spotAngle > 1)
					{
						int fade = 35;

						PlayerLight.GetComponent<Light>().spotAngle -= Time.deltaTime *fade;
					}
					else					
					{
						PlayerLight.transform.parent = this.transform;
						PlayerLight.transform.position = target.transform.position;
						PlayerLight.GetComponent<Light>().enabled = false;
						lightfading = false;

					}

				}


			}

		}
		else if(M)
		{
			GameObject myObject = null;;

			if(this.GetComponentInParent<MoveTo>().StateName == "Patrol" || this.GetComponentInParent<MoveTo>().StateName == "Mark"|| this.GetComponentInParent<MoveTo>().StateName == "Seek"|| this.GetComponentInParent<MoveTo>().StateName == "Chase")
			{
				light.enabled = true;
				if(	this.GetComponent<AudioSource>().loop != true )
				{
					this.GetComponent<AudioSource>().Play();
					this.GetComponent<AudioSource>().loop = true;
					
				}
				
				lightfading = true;
				if(light.spotAngle < lightSize)
				{
					light.spotAngle += 0.9f * Time.deltaTime * 60;
				}
				if(light.spotAngle > lightSize)
				{
					light.spotAngle -= 0.9f * Time.deltaTime * 60;
				}
				timer += Time.deltaTime;
				if(timer >= pulseSpeed)
				{
					timer = 0;
					myObject = Instantiate(prefabPulse,myVec,Quaternion.identity);
					myObject.transform.parent = this.transform;
					

				}
				
				
			}
			else if(this.GetComponentInParent<MoveTo>().StateName != "Patrol" && this.GetComponentInParent<MoveTo>().StateName != "Mark" && this.GetComponentInParent<MoveTo>().StateName != "Seek" && this.GetComponentInParent<MoveTo>().StateName != "Chase")
				
			{
				
				this.GetComponent<AudioSource>().loop = false;
				
				
				if(lightfading)
				{
					
					
					if(light.spotAngle > 1)
					{
						int fade = 35;
						
						light.spotAngle -= Time.deltaTime *fade;
					}
					else					
					{
						//PlayerLight.transform.parent = this.transform;
						//PlayerLight.transform.position = target.transform.position;
						light.enabled = false;
						lightfading = false;
						
					}
					
				}
				
				
			}

		}

	
		
	}
}
