using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using XGame.Framework.Nodes;

namespace XGame.Editor.Nodes
{
    public class NodeGenWindow : EditorWindow
    {
        [MenuItem("Tools/Node/Node代码生成窗口")]
        public static void ShowWindow()
        {
            NodeGenWindow window = EditorWindow.GetWindowWithRect<NodeGenWindow>(new Rect(0, 0, 400, 300), false, "Node代码生成窗口");
            window.Show();
        }
        GUIStyle _titleStyle;
        GUIStyle _btnStyle;

        private Type _nodeGroupType;

        private Enum _selectedGroup;
        private string _nodeName;
        private string _componentName;
        private void OnEnable()
        {
            _nodeGroupType = FindNodeGroupType();

            _titleStyle = new GUIStyle("Label");
            _titleStyle.fontSize = 25;
            _titleStyle.alignment = TextAnchor.MiddleCenter;
            _titleStyle.fontStyle = FontStyle.BoldAndItalic;

            _btnStyle = new GUIStyle("Button");
            _btnStyle.fontSize = 25;
            _btnStyle.alignment = TextAnchor.MiddleCenter;
            _btnStyle.fontStyle = FontStyle.BoldAndItalic;
        }
        private void OnDisable()
        {
            _nodeGroupType = null;
            _titleStyle = null;
            _btnStyle = null;
        }
        private void OnGUI()
        {
            GUILayout.Label(new GUIContent("Node代码生成器"), _titleStyle, GUILayout.Height(50));
            DrawNodeGroup();
            DrawNodeName();
            DrawGenerateBtn();
        }

        private Type FindNodeGroupType()
        {
            try
            {
                var assemblies = AppDomain.CurrentDomain.GetAssemblies();
                foreach (var assembly in assemblies)
                {
                    var assemblyName = assembly.GetName().Name;
                    if (assemblyName.StartsWith("Business", StringComparison.OrdinalIgnoreCase) == false ||
                        assemblyName.Contains("Editor", StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }
                    var types = assembly.GetTypes();
                    foreach (var type in types)
                    {
                        if (type.IsEnum && type.GetCustomAttribute<NodeGroupAttribute>() != null)
                        {
                            return type;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }
            return null;

        }
        private void DrawNodeGroup()
        {
            if (_nodeGroupType != null)
            {
                var names = Enum.GetNames(_nodeGroupType);
                if (_selectedGroup == null)
                    _selectedGroup = Enum.Parse(_nodeGroupType, names[0]) as Enum;
                _selectedGroup = EditorGUILayout.EnumPopup("选择分组类型:", _selectedGroup);
            }
            else
            {
                EditorGUILayout.HelpBox("没有找到NodeGroup的枚举，请先定义枚举并使用特性：NodeGroupAttribute", MessageType.Error);
            }
        }
        private void DrawNodeName()
        {
            _nodeName = EditorGUILayout.TextField("请输入Node名字:", _nodeName);
            _componentName = EditorGUILayout.TextField("请输入Component名字:", _componentName);
        }
        private void DrawGenerateBtn()
        {
            GUILayout.FlexibleSpace();
            GUILayout.BeginVertical();

            GUIStyle style = new GUIStyle("Button")
            {
                alignment = TextAnchor.MiddleCenter,
                fontStyle = FontStyle.BoldAndItalic,
                fontSize = 15,
            };

            GUI.color = Color.green;
            if (GUILayout.Button("Generator", style, GUILayout.Height(50)))
            {
                if (_nodeGroupType == null ||  _selectedGroup == null)
                {
                    EditorUtility.DisplayDialog("代码生成错误", "NodeGroup不能为空!", "确认");
                    return;
                }
                var isSuccess = (new NodeCodeGen()).Gen(_nodeName, _componentName, $"{_nodeGroupType.Name}.{_selectedGroup}");
                if (isSuccess)
                {
                    Close();
                }
                //new NodeCodeGen().CodeGen(GetCodeGenType(), inputStr, saveObj, behaviour, mapVO, saveMapVO, objcetName);
            }
            GUI.color = Color.white;
            GUILayout.EndVertical();
        }
    }
}

