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

using UnityEngine;

public class EnemySound : MonoBehaviour {
	public bool sm, med, larg;
	public string size;
	public float timer;
	public bool canSound = true;
	public Vector3 myVec;
	public GameObject prefab;
	public GameObject me;
	#region New Var
	public float dTimer;
	public bool over;
	public Texture2D Red;
	#endregion
	public AudioSource audi;
	public int i;

	//public int layerNo;
	// Use this for initialization
	void Start () {
		audi = this.GetComponent<AudioSource>();

		if(!sm&&!med&&!larg)
		{
			larg = true;
		}
		if(sm)
		{
			size = "Small";
		}
		if(med)
		{
			size = "Medium";
		}
		if(larg)
		{
			size = "Large";
		}
		prefab = Resources.Load("Prefab/"+size+"/Trigger_"+size+"_Enemy")as GameObject;
		//layerNo = this.gameObject.layer;
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(!canSound)
		{
			timer += Time.deltaTime;
			if(timer >= 1.7f)
			{
				canSound = true;
				timer = 0;
			}
		}

		if(over)
		{
			if(i < 1)
			{
				audi.Play();
				Time.timeScale = 0;
				i++;
			}
		}

		
	}

	void OnTriggerEnter(Collider other)
	{
		if(other.gameObject.tag == "Player")
		{
			if(canSound)
			{
				GameObject myObject = null;
				myVec = new Vector3(this.transform.position.x,0,this.transform.position.z);

					myObject = Instantiate(prefab,myVec,Quaternion.identity);
				myObject.transform.parent = this.transform;
				canSound = false;
			}


			if(me.layer == 12)
			{
				me.layer = 11;
			}

			

		}
	}

	#region New area
	
	void OnTriggerStay(Collider other)
	{
		if(other.gameObject.tag == "Player")
		{
			dTimer += Time.deltaTime;
			if(dTimer >=3)
			{
				over = true;
				dTimer = 3;
			}
		}
	}
	#endregion

	void OnTriggerExit(Collider other)
	{
		if(other.gameObject.tag == "Player")
		{
			if(me.layer == 11)
			{
				me.layer = 12;

			}
			#region New time
			
			dTimer = 0;
			#endregion
		}

	}
	#region New
	void OnGUI()
	{
		float scrw = Screen.width/16;
		float scrh = Screen.height/9;
		
		GUI.DrawTexture(new Rect(scrw*6,scrh*8.25f,dTimer*(scrw*4)/3,scrh*0.5f),Red);
		
		if(over)
		{

			GUI.Box(new Rect(scrw*6,scrh*1.75f,scrw*4,scrh*2),"You Dead");
			if(GUI.Button(new Rect(scrw*6,scrh*3.75f,scrw*2,scrh*1),"Restart"))
			{
				SceneManager.LoadScene(1);
			}
			if(GUI.Button(new Rect(scrw*8,scrh*3.75f,scrw*2,scrh*1),"End"))
			{
				SceneManager.LoadScene(0);
				
			}
			
		}
	}
	#endregion
}
