using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

public class SceneSwitcherEditorWindow : EditorWindow
{
    [MenuItem("Examples/My Editor Window")]
    public static void ShowExample()
    {
        var editorWindow = GetWindow<SceneSwitcherEditorWindow>();
        editorWindow.titleContent = new GUIContent("SceneSwitcherEditor");
    }

    public void CreateGUI()
    {
        // Each editor window contains a root VisualElement object
        VisualElement root = rootVisualElement;
        root.Clear();

        // Create a ScrollView
        ScrollView scrollView = new ScrollView();
        root.Add(scrollView);

        // reload button
        Button reloadButton = new Button();
        reloadButton.text = "Reload";
        reloadButton.clicked += () => CreateGUI();
        scrollView.Add(reloadButton);

        // space
        scrollView.Add(new Label(""));

        // Get the SceneSwitcher component
        if (SceneSwitcher.Instance == null)
        {
            Debug.LogError("SceneSwitcher component not found in the scene.");
            return;
        }


        // Get the list of scenes
        List<(string name, int index)> scenes = SceneSwitcher.Instance.GetScenes();

        // Create a button for each scene
        foreach (var scene in scenes)
        {
            Button button = new Button();
            button.text = scene.name;
            int sceneIndex = scene.index; // Capture the index in a local variable
            button.clicked += () => SceneSwitcher.Instance.SwitchScene(sceneIndex);
            scrollView.Add(button);
        }
    }

}
