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

#if UNITY_EDITOR
using UnityEditor;
#endif

[Serializable]
public class SwitchableScene
{
    public int index;
}


#if UNITY_EDITOR
// custom property drawer

[CustomPropertyDrawer(typeof(SwitchableScene))]
public class SwitchableSceneDrawer : PropertyDrawer
{

    public override VisualElement CreatePropertyGUI(SerializedProperty property)
    {
        // Get the SceneSwitcher component
        if (SceneSwitcher.Instance == null)
        {
            return new Label("SceneSwitcher component not found in the scene.");
        }
        
        // create dropdown based on the SceneSwitcher options
        var root = new VisualElement();
        
        // Get the list of scenes
        List<(string name, int index)> scenes = SceneSwitcher.Instance.GetScenes();
        
        // create dropdown
        var indexProperty = property.FindPropertyRelative("index");
        var dropdown = new PopupField<string>("Scene", scenes.ConvertAll(t => t.name), scenes[indexProperty.intValue].name);
        dropdown.RegisterValueChangedCallback(evt =>
        {
            indexProperty.intValue = scenes.FindIndex(t => t.name == evt.newValue);
            indexProperty.serializedObject.ApplyModifiedProperties();
        });
        
        root.Add(dropdown);
        
        return root;
    }
    
}

#endif
