using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class MainMenuController : MonoBehaviour
{
    public string creditsScene;
    public string optionsScene;
    public string playScene;

    public GameObject creditsObj;

    private List<string> validScenes = new List<string>();
    private Canvas mainCanvas;
    private Canvas creditsCanvas;

    private void Start()
    {
        mainCanvas = GetComponent<Canvas>();
        creditsCanvas = creditsObj.GetComponent<Canvas>();
        creditsCanvas.enabled = false;

        for (int i = 0; i < SceneManager.sceneCountInBuildSettings; i++)
        {
            string path = SceneUtility.GetScenePathByBuildIndex(i);
            int last = path.LastIndexOf('/');
            validScenes.Add(path.Substring(last + 1, path.LastIndexOf(".") - last - 1));
        }
    }

    public void Play()
    {
        if (IsSceneValid(playScene))
            SceneManager.LoadScene(playScene);
        else
            Debug.Log("Invalid playScene path");
    }

    public void Options()
    {
        if (IsSceneValid(optionsScene))
            SceneManager.LoadScene(optionsScene);
        else
            Debug.Log("Invalid optionsScene path");
    }

    public void Credits()
    {
        creditsCanvas.enabled = true;
        creditsCanvas.GetComponent<Credits>().StartCredits();
        Invoke("CloseCredits", 10f);
    }

    public void CloseCredits()
    {
        creditsCanvas.GetComponent<Credits>().StopCredits();
        creditsCanvas.enabled = false;
    }

    public void Quit()
    {
        Debug.Log("Quitting");
#if UNITY_EDITOR
        EditorApplication.isPlaying = false;
#endif
        Application.Quit();
    }

    private bool IsSceneValid(string scenePath)
    {
        if (validScenes.Contains(scenePath))
            return true;
        return false;
    }

}
