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

[RequireComponent(typeof(AudioSource))]
public class M_mic_1 : MonoBehaviour {

	public static void SetSensitivity(float sensitivity){
		if (sensitivity >= 5f && sensitivity <= 500f) {
		} else {
			Debug.Log("sensitivity out of range");

		}
	}
	public static void SetThreshold (float threshold){
		if (threshold >= 0f && threshold <= 5f) {
		} else {
			Debug.Log ("Threshold out of range");
		}
	}
	/*public static float micLoudness;
	private string _device;*/
	public int t_talk;
	public int t_maxtalk=200;
	public bool t_bool=true;

	public float sensitivity = 2000;
	public float loudness = 0;
	private AudioSource _audio;
	public FFTWindow fftWindow;

	public int audioSampleRate = 44100;
	public float minThreshold = 0;
	public float frequency;
	public AudioSource audioSource;
	public string microphone;
	public List<string>options =new List<string>();


	public float GetMicVolume(){
		float [] data = new float[256];
		float a = 0;
		_audio.GetOutputData (data, 0);
		foreach (float s in data) {
			a += Mathf.Abs (s);
		}
		return a /256;
	}

	/*public float  GetFundamentalFrequency(){
		float fundamentalFrequency = 0.0f;
		float[] data = new float[samples];
		audioSource.GetSpectrumData (data, 0, fftWindow);
		float s = 0.0f;
		int i = 0;
		for (int j = 1; j < samples; j++) {
				if (s < data [j]) {
					s = data [j];
					i = j;
			}
		}
			fundamentalFrequency = i * audioSampleRate / samples;
			frequency = fundamentalFrequency;
			return fundamentalFrequency;
		}*/
	void volume(){
		loudness = GetMicVolume () * sensitivity;
	}
	public float moveSpeed=10f;
	public Material mat;
	public string Name;
	public GameObject PlayerRot;
	public Transform player;
	public  GameObject sonic;
	public  float ShootForce = 0f;
	public  GameObject Gun;
	public  float SonicTimer;
	public  float SonicTimer_CD;
	bool SonicCD = false;
	// Use this for initialization
	void Awake(){
		_audio = GetComponent<AudioSource>(); //抓取物件下的AudioSource元件
	}

	void Start () {
		foreach(string device in Microphone.devices){
			if (microphone == null) {
				microphone = device;
			}
			options.Add (device);
		}
		SonicTimer = 0;
		_audio.clip=Microphone.Start(null,true,100,audioSampleRate);
		_audio.mute = false;
		while (!(Microphone.GetPosition (null) > 0)) {
		}
		_audio.Play ();
	}
	
	// Update is called once per frame
	void Update () {
		volume ();
		if (SonicTimer >= SonicTimer_CD) {
			SonicCD= false;
		}
		move ();
		Sonic ();

		loudness = GetMicVolume () * sensitivity;
		t_talk ++;
		SonicTimer +=Time.deltaTime ;
		if ((loudness > 5)&&(loudness<25)&&(t_bool=true)) {
			if (SonicCD == false) {
				SonicTimer = 0;
				SonicCD = true;
				GameObject sonicB = (GameObject)Instantiate (
					sonic,  transform.position,  transform.rotation);
				sonicB.GetComponent<Rigidbody> ().AddForce (sonicB.transform.forward * ShootForce);	

				if (SonicTimer >= SonicTimer_CD) {
					SonicCD= false;
				}
			}
		}else if ((loudness > 25)&&(loudness<500)&&(t_bool=true)) {
			if (SonicCD == false) {
				SonicTimer = 0;
				SonicCD = true;
				GameObject sonicC = (GameObject)Instantiate (
					sonic,  transform.position,  transform.rotation);
				sonicC.GetComponent<Rigidbody> ().AddForce (sonicC.transform.forward * ShootForce*2                                                                                                                                                                                                                     );	

				if (SonicTimer >= SonicTimer_CD) {
					SonicCD= false;
				}
			}
		}
		if (player != null) {
			mat.SetVector (Name, player.position);
		} else {
			Debug.Log ("123");
		}
			
	}
	IEnumerator Example()
	{
		yield return new WaitForSeconds (1);
	}
	void move(){
		if (Input.GetKey ("w")) {
			this.transform.position+=transform.forward*Time.deltaTime;
		}
		if (Input.GetKey ("s")) {
			this.transform.position -= transform.forward*Time.deltaTime;
		}
		if (Input.GetKey ("a")) {
			gameObject.transform.Rotate (new Vector3 (0f, -moveSpeed*100*Time.deltaTime, 0f));
		}
		if (Input.GetKey ("d")) {
			gameObject.transform.Rotate (new Vector3 (0f, moveSpeed*100*Time.deltaTime, 0f));
		}

	}
		
	void Sonic(){
		SonicTimer +=Time.deltaTime ;
		if (Input.GetKeyDown (KeyCode.Space) && SonicCD == false) {
			SonicTimer = 0;
			SonicCD = true;
			GameObject sonicB = (GameObject)Instantiate (
				sonic,  transform.position,  transform.rotation);
			sonicB.GetComponent<Rigidbody> ().AddForce (sonicB.transform.forward * ShootForce);	
			if (SonicTimer >= SonicTimer_CD) {
				SonicCD= false;
			}


		}
	}
}