using XGame.Editor.Asset.Tools;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace XGame.Editor.Build.AssetBundles
{
    public class ABPackerReorderableList<TItem, TData> : IReorderableHandler, System.IDisposable where TItem : IReorderableItem, IReorderableItemData<TData>
    {
        private KReorderableList _reorderableList;
        private bool _inited;
        public bool IsFoldout { set; get; }
        private List<TData> _datas;
        private string _headerName;
        private string _headerTooltip;
        private bool _draggable;

        public float TotalHeight => _reorderableList?.TotalHeight ?? 0;

        public ABPackerReorderableList(List<TData> datas, string headerName, string headerTooltip, bool draggable = true)
        {
            _datas = datas;
            _headerName = headerName;
            _headerTooltip = headerTooltip;
            _draggable = draggable;
        }
        private void Init()
        {
            if (_inited) return;
            _inited = true;
            _reorderableList = new KReorderableList(this)
            {
                Draggable = _draggable
            };
        }

        public void OnGUI()
        {
            IsFoldout = EditorGUILayout.Foldout(IsFoldout, _headerName);
            if (IsFoldout)
            {
                Init();
                //开始水平布局
                EditorGUILayout.BeginHorizontal();
                //缩进14像素
                GUILayout.Space(14);
                //开始垂直布局
                EditorGUILayout.BeginVertical();

                _reorderableList.DoLayoutList();
                //结束垂直布局
                EditorGUILayout.EndVertical();
                //结束水平布局
                EditorGUILayout.EndHorizontal();
            }
        }
        public void OnGUI(Rect rect)
        {
            //IsFoldout = EditorGUI.Foldout(rect, IsFoldout, _headerName);
            //if (IsFoldout)
            {
                //rect.y += EditorGUIUtility.singleLineHeight;
                Init();
                //开始水平布局
                EditorGUILayout.BeginHorizontal();
                //缩进14像素
                GUILayout.Space(14);
                //开始垂直布局
                EditorGUILayout.BeginVertical();

                _reorderableList.DoList(rect);
                //结束垂直布局
                EditorGUILayout.EndVertical();
                //结束水平布局
                EditorGUILayout.EndHorizontal();
            }
        }
        public TData[] GetItemDatas()
        {
            return _reorderableList.GetItemDatas<TData>();
        }
        #region IReorderableHandler 接口实现
        string IReorderableHandler.HeaderName => _headerName;

        string IReorderableHandler.HeaderTooltip => _headerTooltip;

        int IReorderableHandler.DataCount => _datas.Count;

        IReorderableItem IReorderableHandler.CreateItem(bool isInit, int index)
        {
            //Debug.Log($"ABPackerReorderableList CreateItem isInit:{isInit} index:{index} InfoCounts:{_abPackerInfos.Count}");
            var count = _datas.Count;
            if (index >= count)
            {
                for (var i = count; i <= index; i++)
                {
                    var dataType = typeof(TData);
                    if (dataType == typeof(string))
                    {
                        _datas.Add(default(TData));
                    }
                    else
                    {
                        _datas.Add(System.Activator.CreateInstance<TData>());
                    }
                }
            }
            var itemType = typeof(TItem);
            return System.Activator.CreateInstance(itemType, _datas[index]) as IReorderableItem;
        }
        void IReorderableHandler.RemoveItem(int index)
        {
            _datas.RemoveAt(index);
        }
        void IReorderableHandler.ItemChangedIndex(int oldIndex, int newIndex)
        {
            var lastItem = _datas[oldIndex];
            var nextItem = _datas[newIndex];
            _datas[oldIndex] = nextItem;
            _datas[newIndex] = lastItem;
        }
        bool IReorderableHandler.IsCanAdd(int count, int index)
        {
            //Debug.Log($"ABPackerReorderableList IsCanAdd Count:{count} index:{index} InfoCounts:{_abPackerInfos.Count}");
            return true;
        }

        bool IReorderableHandler.IsCanRemove(int count, int index)
        {
            return index >= 0;
        }
        #endregion

        void System.IDisposable.Dispose()
        {
            _datas = null;
            _reorderableList?.Dispose();
            _reorderableList = null;
        }
    }
}
