// 本脚本只在不存在时会生成一次。已存在不会再次生成覆盖

using System.Collections.Generic;
using FairyGUI;
using Fantasy;
using UnityEngine;
using NBC;
using NBF.Fishing2;
using UIPanel = NBC.UIPanel;

namespace NBF
{
    public partial class BagSlotPanel : UIPanel
    {
        private List<ItemInfo> _items = new List<ItemInfo>();


        protected override void OnInit()
        {
            SlotList.onClickItem.Add(OnClickSlotItem);
        }

        protected override void OnShow()
        {
            SetList();
            SetSlotList();
        }

        #region 物品列表

        private void SetList()
        {
            _items.Clear();
            foreach (var roleBagItem in RoleModel.Instance.Items)
            {
                if (CanShow(roleBagItem))
                {
                    _items.Add(roleBagItem);
                }
            }

            List<object> items = RoleModel.Instance.GetItemListData();
            List.SetListData(items);
            var children = List.List.GetChildren();
            foreach (var gObject in children)
            {
                gObject.draggable = true;
            }
        }

        // void OnRenderItem(int index, GObject obj)
        // {
        //     var itemData = _items[index];
        //     if (obj is BagItem bagItem)
        //     {
        //         bagItem.SetData(itemData);
        //         bagItem.draggable = true;
        //     }
        // }

        #endregion

        #region 快速选择列表

        private int _lastSelectedItem;

        private void SetSlotList()
        {
            SlotList.RemoveChildrenToPool();
            for (int index = 0; index < 7; index++)
            {
                if (SlotList.AddItemFromPool() is BagSlotItem slotItem)
                {
                    slotItem.SetData(index, RoleModel.Instance.GetSlotItem(index));
                    slotItem.OnChangeSlot = OnChangeSlotItem;
                }
            }
        }


        private void OnChangeSlotItem(int index, long id)
        {
            RoleModel.Instance.SetSlot(index, id).OnCompleted(OnChangeSlotItemDone);
        }

        private void OnChangeSlotItemDone()
        {
            SetSlotList();
            // SlotList.RefreshVirtualList();
        }

        private void OnClickSlotItem(EventContext context)
        {
            if (context.data is BagSlotItem bagSlotItem)
            {
                _lastSelectedItem = bagSlotItem.Index;
                BagSelectPanel.Show(SelectCallback, ItemType.Rod, ItemType.Bobber, ItemType.Reel, ItemType.Bait,
                    ItemType.Hook, ItemType.Line, ItemType.Lure);
            }
        }

        private void SelectCallback(long selectId)
        {
            if (selectId < 1) return;
            RoleModel.Instance.SetSlot(_lastSelectedItem, selectId).OnCompleted(OnChangeSlotItemDone);
        }

        #endregion

        private bool CanShow(ItemInfo itemInfo)
        {
            if (itemInfo.ItemType == ItemType.Rod)
            {
                return true;
            }

            return false;
        }
    }
}