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

namespace SzafaKitEditor.VisualElements {
    public class CVarPropertyField : VisualElement {
        private SerializedProperty _cvarProperty;
        private SerializedProperty _nameProperty;
        private SerializedProperty _defaultValueProperty;
        private SerializedProperty _defaultValueKindProperty;
        private SerializedProperty _defaultValueNumberProperty;
        private SerializedProperty _defaultValueStringProperty;

        private VisualElement _details;

        private bool _expand;

        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();
        }

        private 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(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);
        }

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

        private void CreateDetailsBox() {
            PropertyField nameField = new(_nameProperty);
            PropertyField defaultValueKindField = new(_defaultValueKindProperty);
            PropertyField numberField = new(_defaultValueNumberProperty);
            PropertyField stringField = new(_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);
        }
    }
}