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

public class MainMenu : MonoBehaviour {
    [SerializeField]
    private float blastSpeed = 10;
    private float blastTimer = 0;
    [SerializeField]
    private Vector3 blastPos;

    private Slider numGorgons, mazeWidth, mazeLength;
    private Text numGorgonsText, mazeWidthText, mazeLengthText;

    bool initialized = false;

    private void Awake()
    {
        var settings = GetComponentInChildren<Canvas>().transform.FindChild("Settings");
        var gorgons = settings.FindChild("Gorgons");
        numGorgons = gorgons.GetComponentInChildren<Slider>(true);
        numGorgons.value = PlayerPrefs.GetInt("NumGorgons", 1);
        numGorgonsText = gorgons.FindChild("Value").GetComponent<Text>();
        numGorgonsText.text = numGorgons.value.ToString();

        var width = settings.FindChild("MazeWidth");
        mazeWidth = width.GetComponentInChildren<Slider>(true);
        mazeWidth.value = PlayerPrefs.GetInt("MazeWidth", 10);
        mazeWidthText = width.FindChild("Value").GetComponent<Text>();
        mazeWidthText.text = mazeWidth.value.ToString();

        var length = settings.FindChild("MazeLength");
        mazeLength = length.GetComponentInChildren<Slider>(true);
        mazeLength.value = PlayerPrefs.GetInt("MazeLength", 10);
        mazeLengthText = length.FindChild("Value").GetComponent<Text>();
        mazeLengthText.text = mazeLength.value.ToString();
        initialized = true;
    }
	void Update () {
        blastTimer -= Time.deltaTime;
        if (blastTimer <= 0)
        {
            StartCoroutine(SoundBlastGenerator.instance.PerformClap(blastPos, SoundBlast.Type.Menu));
            blastTimer = blastSpeed;
        }
	}

    public void BeginClear()
    {
        SceneManager.LoadScene("clear");
    }
    public void BeginFog()
    {
        SceneManager.LoadScene("fog");
    }
    public void BeginDarkness()
    {
        SceneManager.LoadScene("dark");
    }

    public void SetNumGorgons()
    {
        if (!initialized) return;
        PlayerPrefs.SetInt("NumGorgons", (int)numGorgons.value);
        numGorgonsText.text = numGorgons.value.ToString();
    }
    public void SetMazeWidth()
    {
        if (!initialized) return;
        PlayerPrefs.SetInt("MazeWidth", (int)mazeWidth.value);
        mazeWidthText.text = mazeWidth.value.ToString();
    }
    public void SetMazeLength()
    {
        if (!initialized) return;
        PlayerPrefs.SetInt("MazeLength", (int)mazeLength.value);
        mazeLengthText.text = mazeLength.value.ToString();
    }

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