using System.Collections.Generic;

using UnityEditor;
using UnityEngine;
using Utility;

namespace Mono.UI.UIComs
{
    [CustomEditor(typeof(CustomStateController))]
    public class CustomStateControllerEditor : UnityEditor.Editor
    {
        private CustomStateController uIStateController;


        private void OnEnable()
        {
            uIStateController = (CustomStateController)target;
        }

        public int SelectIndex
        {
            get { return _selectIndex; }
            set
            {
                if (value != _selectIndex && value >= 0)
                {
                    _selectIndex = value;
                    uIStateController.CurrIndex = _selectIndex;
                    uIStateController.ChangeState(_selectIndex);

                    Debug.Log("切换状态+" + uIStateController.data[_selectIndex].StateInfos[0].UIStateV4Datas.Count);
                }
            }
        }

        private int _selectIndex;
        private Object ObjectPrefab;


        bool isInited = false;

        public override void OnInspectorGUI()
        {
            SerializedProperty AllComsProperty = serializedObject.FindProperty("AllComs");
            SerializedProperty AllComsStateTypesProperty = serializedObject.FindProperty("AllComsStateTypes");


            if (uIStateController.AllComsStateTypes.Count < uIStateController.AllComs.Count)
            {
                uIStateController.AllComsStateTypes.Add(CustomStateController.UIStateType.Null);
            }


            GUILayout.BeginHorizontal(GUI.skin.box);

            _selectIndex = uIStateController.CurrIndex;
            for (var i = 0; i < uIStateController.data.Count; i++)
            {
                if (GUILayout.Toggle(SelectIndex == i, i.ToString()))
                {
                    SelectIndex = i;
                }
            }

            EditorGUILayout.EndHorizontal();

            EditorGUILayout.ObjectField(ObjectPrefab, typeof(Object), false);


            SerializedProperty property;
            EditorGUILayout.LabelField("需要保存状态数据的组件列表：");
            var delList = new List<int>();
            for (var i = 0; i < uIStateController.AllComs.Count; i++)
            {
                GUILayout.BeginHorizontal();
                property = AllComsProperty.GetArrayElementAtIndex(i);

                property.objectReferenceValue = EditorGUILayout.ObjectField(property.objectReferenceValue, typeof(Object), true);

                uIStateController.AllComsStateTypes[i] = (CustomStateController.UIStateType)EditorGUILayout.EnumFlagsField(uIStateController.AllComsStateTypes[i]);


                if (GUILayout.Button("X"))
                {
                    //将元素添加进删除list
                    delList.Add(i);
                }

                GUILayout.EndHorizontal();
            }


            //遍历删除list，将其删除掉
            foreach (var i in delList)
            {
                AllComsProperty.DeleteArrayElementAtIndex(i);
                AllComsStateTypesProperty.DeleteArrayElementAtIndex(i);
            }


            // EditorGUILayout.LabelField("当前保存的状态数据列表：");
            // for (var i = 0; i < uIStateController.data.Count; i++)
            // {
            //     property = AllComDatasProperty.GetArrayElementAtIndex(i);
            //     EditorGUILayout.PropertyField(property);
            // }

            EditorGUILayout.BeginHorizontal();

            if (GUILayout.Button("添加当前状态数据"))
            {
                uIStateController.AddData();
                SelectIndex = uIStateController.data.Count - 1;
                Debug.Log("添加当前状态数据");
                EditorUtility.SetDirty(uIStateController.gameObject);
            }

            if (GUILayout.Button("保存当前状态数据"))
            {
                uIStateController.SaveData();


                EditorUtility.SetDirty(uIStateController.gameObject);
                Debug.Log("保存当前状态数据");
            }

            if (GUILayout.Button("删除当前状态数据"))
            {
                uIStateController.RemoveData(SelectIndex);
                SelectIndex = uIStateController.data.Count - 1;
                EditorUtility.SetDirty(uIStateController.gameObject);
                Debug.Log("删除当前状态数据");
            }

            EditorGUILayout.EndHorizontal();


            var eventType = Event.current.type;
            //在Inspector 窗口上创建区域，向区域拖拽资源对象，获取到拖拽到区域的对象
            if (eventType == EventType.DragUpdated || eventType == EventType.DragPerform)
            {
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;

                if (eventType == EventType.DragPerform)
                {
                    DragAndDrop.AcceptDrag();
                    foreach (var o in DragAndDrop.objectReferences)
                    {
                        AddObject(AllComsProperty, o);
                    }
                }

                Event.current.Use();
            }


            serializedObject.ApplyModifiedProperties();
            serializedObject.UpdateIfRequiredOrScript();
        }


        private void AddObject(SerializedProperty dataProperty, Object obj)
        {
            int index = dataProperty.arraySize;
            dataProperty.InsertArrayElementAtIndex(index);
            var element = dataProperty.GetArrayElementAtIndex(index);
            element.objectReferenceValue = obj;
        }

        private void AddStateData(CustomStateController.UIStateData data)
        {
            uIStateController.data.Add(data);
        }
    }
}