using RebootKit.Engine.Services.Console;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;

namespace RebootKitEditor.Inspectors {
    [CustomEditor(typeof(CVarAsset))]
    public class CVarAssetInspector : Editor {
        public override VisualElement CreateInspectorGUI() {
            SerializedProperty cvarProperty = serializedObject.FindProperty("_cvar");
            CVarSerializedProperties properties = CVarSerializedPropertiesFinder.Find(cvarProperty);

            VisualElement container = new();

            container.Add(new PropertyField(properties.Flags));
            container.Add(new PropertyField(properties.Name));

            PropertyField descriptionField = new PropertyField(properties.Description);
            container.Add(descriptionField);
            
            container.Add(new PropertyField(properties.ValueKind));
            container.Add(new PropertyField(properties.ValueNumber));
            container.Add(new PropertyField(properties.ValueString));
            return container;
        }
    }

    public struct CVarSerializedProperties {
        public SerializedProperty Flags;
        public SerializedProperty Name;
        public SerializedProperty Description;

        public SerializedProperty ValueKind;
        public SerializedProperty ValueNumber;
        public SerializedProperty ValueString;
    }

    public static class CVarSerializedPropertiesFinder {
        public static CVarSerializedProperties Find(SerializedProperty cvar) {
            CVarSerializedProperties properties = new();
            properties.Flags = cvar.FindPropertyRelative("Flags");
            properties.Name = cvar.FindPropertyRelative("Name");
            properties.Description = cvar.FindPropertyRelative("Description");

            SerializedProperty value = cvar.FindPropertyRelative("DefaultValue");
            if (value != null) {
                properties.ValueKind = value.FindPropertyRelative("Kind");
                properties.ValueNumber = value.FindPropertyRelative("NumberValue");
                properties.ValueString = value.FindPropertyRelative("StringValue");
            }

            return properties;
        }
    }
}