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

public class MainMenuController : MonoBehaviour {

	public GameObject toggleAudioObject, toggleControlObject;

	void Start() {
		if (SceneManager.GetActiveScene().name == "Menu") {
			// Set stored settings value
			if (!PlayerPrefs.HasKey("enableAudio")) {
				PlayerPrefs.SetInt ("enableAudio", 1);
				PlayerPrefs.SetInt ("reverseControl", 0);

				toggleAudioObject.GetComponent<Toggle> ().isOn = true;
				toggleControlObject.GetComponent<Toggle> ().isOn = false;
				PlayerPrefs.Save();
			} 
			else {
				if (PlayerPrefs.GetInt ("enableAudio") == 0)
					toggleAudioObject.GetComponent<Toggle> ().isOn = false;
				else
					toggleAudioObject.GetComponent<Toggle> ().isOn = true;

				if (PlayerPrefs.GetInt ("reverseControl") == 0)
					toggleControlObject.GetComponent<Toggle> ().isOn = false;
				else
					toggleControlObject.GetComponent<Toggle> ().isOn = true;
			}

			// For demo only: reset high score for each session
			PlayerPrefs.SetInt("highScore",0);
		}
	}

	public void toggleAudio() {
		if (toggleAudioObject.GetComponent<Toggle> ().isOn) {
			AudioListener.volume = 1.0f;
		} 
		else {
			AudioListener.volume = 0.0f;
		}
	}

	public void gotoGame() {
		// Save Settings
		if (SceneManager.GetActiveScene ().name == "Menu") {
			if (toggleAudioObject.GetComponent<Toggle> ().isOn) {
				PlayerPrefs.SetInt ("enableAudio", 1);
			} else {
				PlayerPrefs.SetInt ("enableAudio", 0);
			}

			if (toggleControlObject.GetComponent<Toggle> ().isOn) {
				PlayerPrefs.SetInt ("reverseControl", 1);
			} else {
				PlayerPrefs.SetInt ("reverseControl", 0);
			}
			PlayerPrefs.Save ();
		}

		StartCoroutine(fadeScreen());
	}

	IEnumerator fadeScreen() {
		float fadeTime = gameObject.GetComponent<SceneFader>().BeginFade(1);
        yield return new WaitForSeconds(fadeTime);
		SceneManager.LoadScene("MainGame");
	}
}
