using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEditor;
using UnityEngine;
using WS;

///<summary>流程编辑器</summary>
public class ProcessEditor : EditorWindow
{
    ///<summary>数据</summary>
    private List<Module> Config = new List<Module>();

    ///<summary>编辑器</summary>
    private List<ModuleEditorData> EditorData = new List<ModuleEditorData>();

    ///<summary>组件类型</summary>
    private List<Type> ComponentTypes = new List<Type>();

    ///<summary>编辑器类型</summary>
    private Dictionary<string, EditorTypeData> EditorTypes =
        new Dictionary<string, EditorTypeData>();

    ///<summary>编辑器类型名称</summary>
    private string[] Components;

    ///<summary>路径</summary>
    private string path;

    ///<summary>滑杆</summary>
    private Vector2 pos;

    ///<summary>模块</summary>
    private string[] Modules
    {
        get { return EditorData.Select(m => m.Name).ToArray(); }
    }

    ///<summary>模块索引</summary>
    private int ModuleIndex;

    ///<summary>所有任务</summary>
    private string[] OwnTasks
    {
        get
        {
            List<string> tasks = new List<string>();
            for (int i = 0; i < EditorData.Count; i++)
            {
                tasks.AddRange(EditorData[i].VTasks.Select(t => $"{t.Id} {t.Name}"));
            }
            return tasks.ToArray();
        }
    }

    private void OnGUI()
    {
        pos = EditorGUILayout.BeginScrollView(pos);
        DrawModule();
        EditorGUILayout.EndScrollView();
        DrawSetting();
    }

    private void OnDestroy()
    {
        for (int i = 0; i < EditorData.Count; i++)
        {
            for (int j = 0; j < EditorData[i].VTasks.Count; j++)
            {
                for (int k = 0; k < EditorData[i].VTasks[j].Operates.Count; k++)
                {
                    EditorData[i].VTasks[j].Operates[k].Editor.Dispose();
                }
                EditorData[i].VTasks[j].Operates.Clear();
            }
            EditorData[i].VTasks.Clear();
        }
        EditorData.Clear();
    }

    ///<summary>绘制模块</summary>
    private void DrawModule()
    {
        for (int i = 0; i < EditorData.Count; i++)
        {
            EditorGUILayout.BeginHorizontal();
            EditorData[i].Foldout = EditorGUILayout.Foldout(
                EditorData[i].Foldout,
                EditorData[i].Name
            );
            GUILayout.Label("");
            if (EditorData[i].VTasks.Count > 0)
            {
                GUILayout.Label(GetVTaskIndex(EditorData[i].VTasks), GUILayout.Width(100));
            }
            GUILayout.Label("任务无顺序:", GUILayout.Width(70));
            EditorData[i].NoOrder = EditorGUILayout.Toggle(
                EditorData[i].NoOrder,
                GUILayout.Width(20)
            );
            GUILayout.Label("忽略任务栏:", GUILayout.Width(70));
            EditorGUI.BeginChangeCheck();
            EditorData[i].NoTaskbar = EditorGUILayout.Toggle(
                EditorData[i].NoTaskbar,
                GUILayout.Width(20)
            );
            if (EditorGUI.EndChangeCheck())
            {
                for (int x = 0; x < EditorData[i].VTasks.Count; x++)
                {
                    EditorData[i].VTasks[x].NoTaskbar = EditorData[i].NoTaskbar;
                }
            }

            if (GUILayout.Button("展开", GUILayout.Width(35)))
            {
                EditorData[i].SetFoldout(true);
            }
            if (GUILayout.Button("收起", GUILayout.Width(35)))
            {
                EditorData[i].SetFoldout(false);
            }
            if (GUILayout.Button("删除", GUILayout.Width(35)))
            {
                RemoveModule(EditorData[i]);
                EditorData.RemoveAt(i);
                SortId();
            }
            if (GUILayout.Button("插入", GUILayout.Width(35)))
            {
                EditorData.Insert(i + 1, new ModuleEditorData());
                SortId();
            }
            if (GUILayout.Button("上移", GUILayout.Width(35)))
            {
                if (i > 0)
                {
                    ModuleEditorData data = EditorData[i];
                    EditorData.RemoveAt(i);
                    EditorData.Insert(i - 1, data);
                    SortId();
                }
            }
            if (GUILayout.Button("下移", GUILayout.Width(35)))
            {
                if (i < EditorData.Count - 1)
                {
                    ModuleEditorData data = EditorData[i];
                    EditorData.RemoveAt(i);
                    EditorData.Insert(i + 1, data);
                    SortId();
                }
            }
            EditorGUILayout.EndHorizontal();
            if (EditorData[i].Foldout)
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("", GUILayout.Width(5));
                GUILayout.Label("模块名称:", GUILayout.Width(55));
                EditorData[i].Name = EditorGUILayout.TextField(EditorData[i].Name);
                EditorGUILayout.EndHorizontal();
                DrawTask(EditorData[i]);
            }
        }
    }

    ///<summary>绘制设置</summary>
    private void DrawSetting()
    {
        float score = 0;
        for (int i = 0; i < EditorData.Count; i++)
        {
            for (int j = 0; j < EditorData[i].VTasks.Count; j++)
            {
                score += EditorData[i].VTasks[j].Score;
            }
        }
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("");
        GUILayout.Label($"总分:{score}", GUILayout.Width(80));
        if (Modules.Length > 0)
        {
            ModuleIndex = EditorGUILayout.Popup(ModuleIndex, Modules, GUILayout.Width(100));
            if (GUILayout.Button("拷贝模块", GUILayout.Width(70)))
            {
                Module module = ToModule(EditorData[ModuleIndex]);
                ModuleEditorData editordata = LoadModule(module);
                EditorData.Add(editordata);
                SortId();
            }
        }
        if (GUILayout.Button("添加模块", GUILayout.Width(70)))
        {
            EditorData.Add(new ModuleEditorData());
            SortId();
        }
        if (GUILayout.Button("保存", GUILayout.Width(70)))
        {
            Save();
        }
        EditorGUILayout.EndHorizontal();
    }

    ///<summary>绘制任务</summary>
    private void DrawTask(ModuleEditorData module)
    {
        for (int i = 0; i < module.VTasks.Count; i++)
        {
            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("", GUILayout.Width(10));
            module.VTasks[i].Foldout = EditorGUILayout.Foldout(
                module.VTasks[i].Foldout,
                $"任务:{module.VTasks[i].Id}"
            );
            GUILayout.Label("");
            GUILayout.Label(module.VTasks[i].Name);
            GUILayout.Label("");
            GUILayout.Label($"分数:{module.VTasks[i].Score}", GUILayout.Width(55));
            GUILayout.Label("关联获取:", GUILayout.Width(60));
            module.VTasks[i].Get = EditorGUILayout.Toggle(
                module.VTasks[i].Get,
                GUILayout.Width(20)
            );
            if (module.VTasks[i].Get)
                module.VTasks[i].GetId = EditorGUILayout.Popup(
                    module.VTasks[i].GetId,
                    OwnTasks,
                    GUILayout.Width(80)
                );
            if (module.NoOrder)
            {
                GUILayout.Label("关联执行:", GUILayout.Width(60));
                module.VTasks[i].Perform = EditorGUILayout.Toggle(
                    module.VTasks[i].Perform,
                    GUILayout.Width(20)
                );
                if (module.VTasks[i].Perform)
                    module.VTasks[i].PerformId = EditorGUILayout.Popup(
                        module.VTasks[i].PerformId,
                        module.VTasksName,
                        GUILayout.Width(80)
                    );
            }
            GUILayout.Label("忽略任务栏:", GUILayout.Width(70));
            module.VTasks[i].NoTaskbar = EditorGUILayout.Toggle(
                module.VTasks[i].NoTaskbar,
                GUILayout.Width(20)
            );
            if (GUILayout.Button("展开", GUILayout.Width(35)))
            {
                module.VTasks[i].SetFoldout(true);
            }
            if (GUILayout.Button("收起", GUILayout.Width(35)))
            {
                module.VTasks[i].SetFoldout(false);
            }
            if (GUILayout.Button("删除", GUILayout.Width(35)))
            {
                RemoveTask(module.VTasks[i]);
                module.VTasks.RemoveAt(i);
                SortId();
                return;
            }
            if (GUILayout.Button("插入", GUILayout.Width(35)))
            {
                VTaskEditorData vTask = new VTaskEditorData();
                vTask.ModuleData = module;
                module.VTasks.Insert(i + 1, vTask);
                SortId();
            }
            if (GUILayout.Button("上移", GUILayout.Width(35)))
            {
                if (i > 0)
                {
                    VTaskEditorData data = module.VTasks[i];
                    module.VTasks.RemoveAt(i);
                    module.VTasks.Insert(i - 1, data);
                    SortId();
                }
            }
            if (GUILayout.Button("下移", GUILayout.Width(35)))
            {
                if (i < EditorData.Count - 1)
                {
                    VTaskEditorData data = module.VTasks[i];
                    module.VTasks.RemoveAt(i);
                    module.VTasks.Insert(i + 1, data);
                    SortId();
                }
            }
            EditorGUILayout.EndHorizontal();
            if (module.VTasks[i].Foldout)
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("", GUILayout.Width(22));
                GUILayout.Label("任务名称:", GUILayout.Width(55));
                module.VTasks[i].Name = EditorGUILayout.TextField(module.VTasks[i].Name);
                if (!module.VTasks[i].NoTaskbar)
                {
                    GUILayout.Label("分数:", GUILayout.Width(30));
                    module.VTasks[i].Score = EditorGUILayout.FloatField(module.VTasks[i].Score);
                }
                EditorGUILayout.EndHorizontal();
                DrawOperate(module.VTasks[i].Operates);
            }
        }
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("", GUILayout.Width(10));
        if (module.VTasks.Count > 0)
        {
            module.Index = EditorGUILayout.Popup(module.Index, OwnTasks, GUILayout.Width(100));
            if (GUILayout.Button("拷贝任务", GUILayout.Width(70)))
            {
                WSTask task = ToVTask(GetEditorData(module.Index));
                VTaskEditorData editordata = LoadVTask(task);
                editordata.ModuleData = module;
                module.VTasks.Add(editordata);
                SortId();
            }
        }
        if (GUILayout.Button("添加任务", GUILayout.Width(70)))
        {
            VTaskEditorData vTask = new VTaskEditorData();
            vTask.ModuleData = module;
            module.VTasks.Add(vTask);
            SortId();
        }
        EditorGUILayout.EndHorizontal();
    }

    ///<summary>绘制操作</summary>
    private void DrawOperate(List<OperateEditorData> operates)
    {
        Color originalBackgroundColor = GUI.backgroundColor;
        for (int i = 0; i < operates.Count; i++)
        {
            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("", GUILayout.Width(30));
            operates[i].Foldout = EditorGUILayout.Foldout(operates[i].Foldout, $"操作:{i + 1}");
            GUILayout.Label("");
            GUILayout.Label($"{operates[i].Describe}");
            GUILayout.Label("");
            GUILayout.Label("组件:", GUILayout.Width(30));
            operates[i].Index = EditorGUILayout.Popup(
                operates[i].Index,
                Components,
                GUILayout.Width(200)
            );

            if (GUILayout.Button("删除", GUILayout.Width(35)))
            {
                OperateEditorData data = operates[i];
                data.Editor.Dispose();
                operates.RemoveAt(i);
                return;
            }
            if (GUILayout.Button("插入", GUILayout.Width(35)))
            {
                operates.Insert(i + 1, CreateOperateEditor());
            }
            if (GUILayout.Button("上移", GUILayout.Width(35)))
            {
                if (i > 0)
                {
                    OperateEditorData data = operates[i];
                    operates.RemoveAt(i);
                    operates.Insert(i - 1, data);
                }
            }
            if (GUILayout.Button("下移", GUILayout.Width(35)))
            {
                if (i < EditorData.Count - 1)
                {
                    OperateEditorData data = operates[i];
                    operates.RemoveAt(i);
                    operates.Insert(i + 1, data);
                }
            }
            EditorGUILayout.EndHorizontal();
            if (operates[i].Foldout)
            {
                if (operates[i].Editor.Name != Components[operates[i].Index])
                {
                    operates[i].Editor.Dispose();
                    operates[i].Editor = CreateOperateEditor(operates[i].Index);
                }
                SetBackGroundColor(operates[i].Index, originalBackgroundColor);
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("", GUILayout.Width(42));
                EditorGUILayout.BeginVertical();
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("描述:", GUILayout.Width(30));
                operates[i].Describe = EditorGUILayout.TextField(operates[i].Describe);
                EditorGUILayout.EndHorizontal();
                operates[i].Editor.OnGUI();
                EditorGUILayout.EndVertical();
                EditorGUILayout.EndHorizontal();
                GUI.backgroundColor = originalBackgroundColor;
            }
        }
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("", GUILayout.Width(42));
        if (GUILayout.Button("添加操作"))
        {
            operates.Add(CreateOperateEditor());
        }
        EditorGUILayout.EndHorizontal();
    }

    ///<summary>初始化</summary>
    public void Init(string filename)
    {
        LoadEditorType();
        path = $"{Application.dataPath}/Resources/Config/Process/{filename}.txt";
        if (ConfigHelper.ConfigExists(path))
        {
            Config = ConfigHelper.FileLoadConfig<List<Module>>(path);
        }
        if (Config.Count > 0)
        {
            for (int i = 0; i < Config.Count; i++)
            {
                EditorData.Add(LoadModule(Config[i]));
            }
        }
        EditorHelper.NameToScript(
            "Assets/Scripts/Framework/Common/Process",
            typeof(ProcessManager),
            ComponentTypes
        );
        AssetDatabase.Refresh();
    }

    private void SetBackGroundColor(int index, Color orginalColor)
    {
        Color color = orginalColor;
        switch (index)
        {
            case 0:
                ColorUtility.TryParseHtmlString("#F08784", out color);
                break;
            case 1:
                ColorUtility.TryParseHtmlString("#FFFE91", out color);
                break;
            case 2:
                ColorUtility.TryParseHtmlString("#A1FB8E", out color);
                break;
            case 3:
                ColorUtility.TryParseHtmlString("#EB3324", out color);
                break;
            case 4:
                ColorUtility.TryParseHtmlString("#F09B59", out color);
                break;
            case 5:
                ColorUtility.TryParseHtmlString("#9FFCFD", out color);
                break;
            case 6:
                ColorUtility.TryParseHtmlString("#3B93FD", out color);
                break;
            case 7:
                ColorUtility.TryParseHtmlString("#7E84F7", out color);
                break;
            case 8:
                ColorUtility.TryParseHtmlString("#EA3680", out color);
                break;
            case 9:
                ColorUtility.TryParseHtmlString("#377E47", out color);
                break;
        }
        GUI.backgroundColor = color;
    }

    ///<summary>保存</summary>
    private void Save()
    {
        Config.Clear();
        for (int i = 0; i < EditorData.Count; i++)
        {
            Config.Add(ToModule(EditorData[i]));
        }
        ConfigHelper.SaveConfig(Config, path);
        AssetDatabase.Refresh();
    }

    ///<summary>编辑器模块转换成模块数据</summary>
    private Module ToModule(ModuleEditorData module)
    {
        Module data = new Module();
        data.Id = module.Id;
        data.Name = module.Name;
        data.NoTaskbar = module.NoTaskbar;
        data.NoOrder = module.NoOrder;
        for (int i = 0; i < module.VTasks.Count; i++)
        {
            WSTask task = ToVTask(module.VTasks[i]);
            data.WSTasks.Add(task);
        }
        return data;
    }

    ///<summary>加载模块数据</summary>
    private ModuleEditorData LoadModule(Module module)
    {
        ModuleEditorData data = new ModuleEditorData();
        data.Id = module.Id;
        data.Name = module.Name;
        data.NoOrder = module.NoOrder;
        data.NoTaskbar = module.NoTaskbar;
        for (int i = 0; i < module.WSTasks.Count; i++)
        {
            VTaskEditorData task = LoadVTask(module.WSTasks[i]);
            task.ModuleData = data;
            data.VTasks.Add(task);
        }
        for (int i = 0; i < data.VTasks.Count; i++)
        {
            if (data.VTasks[i].Perform)
                data.VTasks[i].PerformId = data.SetVTaskIndex(data.VTasks[i].PerformId);
        }
        return data;
    }

    ///<summary>编辑器任务转换成任务数据</summary>
    private WSTask ToVTask(VTaskEditorData task)
    {
        WSTask data = new WSTask();
        data.Id = task.Id;
        data.Name = task.Name;
        data.NoTaskbar = task.NoTaskbar;
        data.Score = task.Score;
        data.Get = task.Get;
        data.GetId = task.Get ? task.GetId + 1 : 0;
        data.Perform = task.ModuleData.NoOrder ? task.Perform : false;
        data.PerformId = task.Perform ? task.ModuleData.GetVTaskId(task.PerformId) : 0;
        for (int i = 0; i < task.Operates.Count; i++)
        {
            Operate operate = new Operate();
            operate.Id = i + 1;
            operate.Component = EditorTypes[Components[task.Operates[i].Index]].Component;
            operate.Describe = task.Operates[i].Describe;
            if (task.Operates[i].IsLoad)
                operate.Json = task.Operates[i].Editor.ToData();
            else
                operate.Json = task.Operates[i].Json;
            data.Operates.Add(operate);
        }
        return data;
    }

    ///<summary>加载任务数据</summary>
    private VTaskEditorData LoadVTask(WSTask task)
    {
        VTaskEditorData data = new VTaskEditorData();
        data.Id = task.Id;
        data.Name = task.Name;
        data.Score = task.Score;
        data.Get = task.Get;
        data.GetId = task.Get ? task.GetId - 1 : 0;
        data.Perform = task.Perform;
        data.PerformId = task.PerformId;
        for (int i = 0; i < task.Operates.Count; i++)
        {
            OperateEditorData operate = new OperateEditorData();
            operate.Index = IntiOperateIndex(task.Operates[i].Component);
            operate.Describe = task.Operates[i].Describe;
            operate.Json = task.Operates[i].Json;
            operate.Editor = CreateOperateEditor(operate.Index);
            data.Operates.Add(operate);
        }
        return data;
    }

    ///<summary>初始化组件索引</summary>
    private int IntiOperateIndex(string component)
    {
        string key = "";
        int index = -1;
        foreach (var item in EditorTypes)
        {
            if (item.Value.Component == component)
            {
                key = item.Key;
                break;
            }
        }
        for (int i = 0; i < Components.Length; i++)
        {
            if (Components[i] == key)
            {
                index = i;
                break;
            }
        }
        return index;
    }

    ///<summary>移除模块</summary>
    private void RemoveModule(ModuleEditorData module)
    {
        for (int i = 0; i < module.VTasks.Count; i++)
        {
            RemoveTask(module.VTasks[i]);
        }
        module.VTasks.Clear();
    }

    ///<summary>移除任务</summary>
    private void RemoveTask(VTaskEditorData task)
    {
        for (int i = 0; i < task.Operates.Count; i++)
        {
            task.Operates[i].Editor.Dispose();
        }
        task.Operates.Clear();
    }

    ///<summary>获取任务索引</summary>
    private string GetVTaskIndex(List<VTaskEditorData> tasks)
    {
        return $"第{tasks[0].Id}～{tasks[tasks.Count - 1].Id}步";
    }

    ///<summary>获取任务实体</summary>
    private VTaskEditorData GetEditorData(int index)
    {
        index = index + 1;
        for (int i = 0; i < EditorData.Count; i++)
        {
            VTaskEditorData vtask = EditorData[i].VTasks.Find(t => t.Id == index);
            if (vtask != null)
                return vtask;
        }
        return null;
    }

    ///<summary>排序Id</summary>
    private void SortId()
    {
        int id = 0;
        for (int i = 0; i < EditorData.Count; i++)
        {
            EditorData[i].Id = i + 1;
            for (int j = 0; j < EditorData[i].VTasks.Count; j++)
            {
                id += 1;
                EditorData[i].VTasks[j].Id = id;
            }
        }
    }

    ///<summary>初始化编辑器类型</summary>
    private void LoadEditorType()
    {
        List<string> components = new List<string>();
        Type type = typeof(IOperateEditor);
        Type[] types = type.Assembly.GetTypes();
        for (int i = 0; i < types.Length; i++)
        {
            if (types[i].IsInterface)
            {
                continue;
            }
            Type[] interfaces = types[i].GetInterfaces();
            if (interfaces.Length == 0)
            {
                continue;
            }
            if (interfaces[0] == type)
            {
                object entity = Activator.CreateInstance(types[i]);
                string key = (string)types[i].GetProperty("Name").GetValue(entity);
                if (!EditorTypes.ContainsKey(key))
                {
                    Type component = (Type)types[i].GetProperty("Component").GetValue(entity);
                    ComponentTypes.Add(component);
                    EditorTypes[key] = new EditorTypeData
                    {
                        EditorType = types[i],
                        Component = component.Name
                    };
                    components.Add(key);
                }
                else
                {
                    ShowNotification(new GUIContent($"类型:{types[i].Name} 的名称:{key} 已存在"), 2);
                }
            }
        }
        Components = components.ToArray();
    }

    ///<summary>新建OperateEditor</summary>
    private OperateEditorData CreateOperateEditor()
    {
        OperateEditorData data = new OperateEditorData();
        data.Index = 0;
        data.Editor = CreateOperateEditor(0);
        data.Foldout = true;
        return data;
    }

    ///<summary>新建IOperateEditor</summary>
    private IOperateEditor CreateOperateEditor(int index)
    {
        Type type = EditorTypes[Components[index]].EditorType;
        IOperateEditor processeditor = (IOperateEditor)Activator.CreateInstance(type);
        processeditor.Init(null);
        return processeditor;
    }

    ///<summary>克隆对象 不支持Unity对象</summary>
    private T CloneObject<T>(T realobject)
    {
        using (Stream objStream = new MemoryStream())
        {
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(objStream, realobject);
            objStream.Seek(0, SeekOrigin.Begin);
            return (T)formatter.Deserialize(objStream);
        }
    }
}

///<summary>模块数据</summary>
[Serializable]
public class ModuleEditorData
{
    ///<summary>Id</summary>
    public int Id;

    ///<summary>名称</summary>
    public string Name;

    ///<summary>忽略任务栏</summary>
    public bool NoTaskbar;

    ///<summary>无顺序</summary>
    public bool NoOrder;

    ///<summary>折叠开关</summary>
    public bool Foldout;

    ///<summary>任务数据</summary>
    public List<VTaskEditorData> VTasks = new List<VTaskEditorData>();

    ///<summary>索引</summary>
    public int Index;

    ///<summary>任务名称</summary>
    public string[] VTasksName
    {
        get { return VTasks.Select(t => $"{t.Id} {t.Name}").ToArray(); }
    }

    /// <summary>设置收缩框</summary>
    /// <param name="foldout">打开关闭</param>
    public void SetFoldout(bool foldout)
    {
        Foldout = foldout;
        for (int i = 0; i < VTasks.Count; i++)
        {
            VTasks[i].SetFoldout(foldout);
        }
    }

    ///<summary>获取选择关联的任务Id</summary>
    public int GetVTaskId(int index)
    {
        return VTasks[index].Id;
    }

    ///<summary>设置选择关联的任务Id</summary>
    public int SetVTaskIndex(int id)
    {
        return VTasks.FindIndex(t => t.Id == id);
    }
}

///<summary>任务数据</summary>
[Serializable]
public class VTaskEditorData
{
    ///<summary>Module</summary>
    public ModuleEditorData ModuleData;

    ///<summary>Id</summary>
    public int Id;

    ///<summary>名称</summary>
    public string Name;

    ///<summary>忽略任务栏</summary>
    public bool NoTaskbar;

    ///<summary>分数</summary>
    public float Score;

    ///<summary>获取</summary>
    public bool Get;

    ///<summary>关联Id</summary>
    public int GetId;

    ///<summary>执行</summary>
    public bool Perform;

    ///<summary>关联Id</summary>
    public int PerformId;

    ///<summary>折叠开关</summary>
    public bool Foldout;

    ///<summary>操作数据</summary>
    public List<OperateEditorData> Operates = new List<OperateEditorData>();

    /// <summary>设置收缩框</summary>
    /// <param name="foldout">打开关闭</param>
    public void SetFoldout(bool foldout)
    {
        Foldout = foldout;
        for (int i = 0; i < Operates.Count; i++)
        {
            Operates[i].Foldout = foldout;
        }
    }
}

///<summary>操作数据</summary>
[Serializable]
public class OperateEditorData
{
    ///<summary>组件索引</summary>
    public int Index;

    ///<summary>编辑器</summary>
    public IOperateEditor Editor;

    ///<summary>描述/名称</summary>
    public string Describe;

    ///<summary>数据</summary>
    public string Json;

    //折叠开关
    private bool foldout;

    ///<summary>折叠开关</summary>
    public bool Foldout
    {
        get { return foldout; }
        set
        {
            foldout = value;
            if (!IsLoad)
                Editor.Init(Json);
            IsLoad = true;
        }
    }

    //是否加载了数据
    public bool IsLoad;
}

///<summary>编辑器类型数据</summary>
public class EditorTypeData
{
    ///<summary>编辑器类型</summary>
    public Type EditorType;

    ///<summary>组件名称</summary>
    public string Component;
}
