﻿using UnityEngine.UIElements;

namespace JLGames.RocketDriver.Editor.Windows.Main.Service
{
    public class ServiceNodeInspector : VisualElement
    {
        public new class UxmlFactory : UxmlFactory<ServiceNodeInspector, UxmlTraits>
        {
        }

        private UnityEditor.Editor m_Editor;

        internal void UpdateSelection(ServiceNodeView nodeView)
        {
            if (null != m_Editor)
            {
                Clear();
                UnityEngine.Object.DestroyImmediate(m_Editor);
                m_Editor = null;
            }

            if (null == nodeView || nodeView.NodeData == null) return;

            m_Editor = UnityEditor.Editor.CreateEditor(nodeView.NodeData);

            var container = new IMGUIContainer(() => { m_Editor.OnInspectorGUI(); });
            Add(container);
        }
    }
}