using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using UnityEngine.UI;

public class MenuManager : MonoBehaviour {

	private GameObject audioInputObject0;
	private GameObject audioInputObject1;
	private MicrophoneInput micIn0;
	private MicrophoneInput micIn1;
	public float startThreshold = 0.6f;
	private float timeLeft = 5.0f;

	//UI Texte
	public Text timeLeftText;
	public Text micInText0;
	public Text micInText1;
	public Text micInText0White;
	public Text micInText1White;

	// Use this for initialization
	void Start () {
		audioInputObject0 = GameObject.Find ("MicInput0");
		micIn0 = (MicrophoneInput) audioInputObject0.GetComponent("MicrophoneInput");
		audioInputObject1 = GameObject.Find ("MicInput1");
		micIn1 = (MicrophoneInput) audioInputObject1.GetComponent("MicrophoneInput");
	}
	
	// Update is called once per frame
	void Update () {
		if (SceneManager.GetActiveScene ().name == "MainMenu") {
			if (micIn0.loudness > startThreshold) {
				timeLeftText.enabled = true;
				timeLeft -= Time.deltaTime;
				timeLeftText.text = "Starting in " + Mathf.Round (timeLeft) + " seconds";
				if (timeLeft <= 0.0f) {
					SceneManager.LoadScene ("Race");
					Debug.Log ("Loading");	
				}
			} else {
				timeLeft = 5f;
				timeLeftText.enabled = false;
			}
		}

		micInText0.text = Microphone.devices [0];
		micInText1.text = Microphone.devices [1];
		micInText0White.text = micInText0.text;
		micInText1White.text = micInText1.text;
	}

	public void QuitGame(){
		Application.Quit ();
	}
}
