using TFramework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System;

[CustomEditor(typeof(Main))]
public class MianInspector:TEditor
{
    private SerializedProperty _doNotDestroy;
    private SerializedProperty _ManagerList;
    private SerializedProperty _IsEnableDebug;
    private ReorderableList _managerReorderable;

    private GUIContent dndGC = new GUIContent("跳场景不销毁");
    private GUIContent mListGC = new GUIContent("管理器注册表");
    private GUIContent dbGC = new GUIContent("启用Debug工具");
    List<BaseManager> monos = new List<BaseManager>();
    List<Type> magTypes;

    private void OnEnable()
    {
        _doNotDestroy = serializedObject.FindProperty("doNotDestroy");
        _ManagerList = serializedObject.FindProperty("m_ManagerList");
        _IsEnableDebug = serializedObject.FindProperty("IsEnableDebug");
        _managerReorderable = new ReorderableList(serializedObject, _ManagerList);

        magTypes = GlobalTool.GetTypesInRunTimeAssemblies(type =>
        {
            return (type.IsSubclassOf(typeof(BaseManager)) ||
            (type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(BaseManager)));
        }); 
        monos.AddRange(FindObjectsOfType<BaseManager>());
        GenericMenu menu = new GenericMenu();
        
        _managerReorderable.onAddCallback = (list) =>
        {
            magTypes.ForEach(p =>
            {
                menu.AddItem(new GUIContent(p.Name), (target as Main).m_ManagerList.Find(m => m.GetType() == p) != null, () =>
                {
                    if ((target as Main).m_ManagerList.Find(m => m.GetType() == p) != null) return;
                    serializedObject.Update();
                    BaseManager magr = monos.Find(m => m.GetType() == p);
                    if (magr == null)
                    {
                        magr = FindObjectsOfType<BaseManager>().Find(b => b.GetType() == p);
                        if (magr == null)
                        {
                            magr = (target as Main).gameObject.AddComponent(p) as BaseManager;
                        }
                    }
                    list.serializedProperty.arraySize++;
                    list.index = list.serializedProperty.arraySize - 1;
                    SerializedProperty marg = list.serializedProperty.GetArrayElementAtIndex(list.index);
                    marg.objectReferenceValue = magr;
                    serializedObject.ApplyModifiedProperties();
                });
            });
            menu.ShowAsContext();
        };

        _managerReorderable.drawHeaderCallback = rect =>
        {
            GUI.Label(rect, mListGC);
        };

        _managerReorderable.drawElementCallback = (rect, index, isActive, isFocused) =>
        {
            UnityEngine.Object magr = EditorGUI.ObjectField(rect, _ManagerList.GetArrayElementAtIndex(index).objectReferenceValue, typeof(BaseManager), true);
            if(magr!= _ManagerList.GetArrayElementAtIndex(index).objectReferenceValue)
            {
                if ((target as Main).m_ManagerList.Contains(magr as BaseManager)) return;
                _ManagerList.GetArrayElementAtIndex(index).objectReferenceValue = magr;
            }
            //EditorGUI.PropertyField(rect, _ManagerList.GetArrayElementAtIndex(index));
        };
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        EditorGUILayout.PropertyField(_doNotDestroy,dndGC);
        _managerReorderable.DoLayoutList();
        EditorGUILayout.PropertyField(_IsEnableDebug,dbGC);
        serializedObject.ApplyModifiedProperties();
    }
}
