using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace RebootKit.Editor.VisualElements {
    public class CVarPropertyField : VisualElement {
        readonly SerializedProperty _cvarProperty;
        readonly SerializedProperty _defaultValueKindProperty;
        readonly SerializedProperty _defaultValueNumberProperty;
        readonly SerializedProperty _defaultValueProperty;
        readonly SerializedProperty _defaultValueStringProperty;

        VisualElement _details;

        bool _expand;
        readonly SerializedProperty _nameProperty;

        public CVarPropertyField(SerializedProperty cvarProperty) {
            _cvarProperty = cvarProperty;
            _nameProperty = _cvarProperty.FindPropertyRelative("Name");
            _defaultValueProperty = _cvarProperty.FindPropertyRelative("DefaultValue");
            _defaultValueKindProperty = _defaultValueProperty.FindPropertyRelative("Kind");
            _defaultValueNumberProperty = _defaultValueProperty.FindPropertyRelative("NumberValue");
            _defaultValueStringProperty = _defaultValueProperty.FindPropertyRelative("StringValue");

            CreateInlineField();
            CreateDetailsBox();
        }

        void CreateInlineField() {
            VisualElement box = new VisualElement {
                style = {
                    height = 48
                }
            };

            Label nameLabel = new Label("NAME") {
                style = {
                    unityFontStyleAndWeight = FontStyle.Bold,
                    backgroundColor = new Color(0.1f, 0.1f, 0.1f, 1.0f)
                }
            };
            box.Add(nameLabel);

            Button button = new Button(ToggleDetails) {
                text = "...",
                style = {
                    width = new StyleLength(new Length(64.0f, LengthUnit.Pixel)),
                    height = new StyleLength(new Length(64.0f, LengthUnit.Pixel))
                }
            };
            box.Add(button);

            Add(box);
        }

        void ToggleDetails() {
            _expand = !_expand;
            _details.visible = _expand;
            _details.style.display = DisplayStyle.None;
        }

        void CreateDetailsBox() {
            PropertyField nameField = new PropertyField(_nameProperty);
            PropertyField defaultValueKindField = new PropertyField(_defaultValueKindProperty);
            PropertyField numberField = new PropertyField(_defaultValueNumberProperty);
            PropertyField stringField = new PropertyField(_defaultValueStringProperty);

            _details = new VisualElement {
                style = {
                    backgroundColor = new Color(0.1f, 0.1f, 0.1f, 1.0f)
                }
            };
            _details.Add(nameField);
            _details.Add(defaultValueKindField);
            _details.Add(numberField);
            _details.Add(stringField);

            _details.visible = false;
            _details.style.display = DisplayStyle.None;

            Add(_details);
        }
    }
}