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

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

namespace NBF
{
    public partial class BagSelectPanel : UIPanel
    {
        //context.data
        private static Action<long> _selectCallBack;

        // private static ItemType _itemType;
        private static readonly List<ItemType> _itemTypes = new List<ItemType>();
        private long _selectedId;
        // private List<ItemInfo> _items;

        // public static void Show(ItemType itemType, Action<long> callBack)
        // {
        //     Show(new[] { itemType }, callBack);
        // }

        public static void Show(Action<long> callBack, params ItemType[] itemTypes)
        {
            _itemTypes.Clear();
            _itemTypes.AddRange(itemTypes);
            _selectCallBack = callBack;
            // List
            Show();
        }

        protected override void OnInit()
        {
            IsModal = true;
            this.AutoAddClick(OnClick);
            List.OnDoubleClickItem += OnDoubleClickItem;
            List.OnClickItem += OnClickItem;
        }

        protected override void OnShow()
        {
            _selectedId = 0;
            SetList();
        }

        protected override void OnDestroy()
        {
            List.OnClickItem -= OnClickItem;
            List.OnDoubleClickItem -= OnDoubleClickItem;
        }

        private void OnClick(GComponent btn)
        {
            if (btn == BtnCancel)
            {
                Hide();
            }
            else if (btn == BtnConfirm)
            {
                if (_selectedId > 0)
                {
                    _selectCallBack?.Invoke(_selectedId);
                }

                Hide();
            }
        }

        private void OnClickItem(object item)
        {
            if (item is BagItem bagItem)
            {
                _selectedId = bagItem.ItemInfo.Id;
            }
        }

        private void OnDoubleClickItem(object item)
        {
            if (item is BagItem bagItem)
            {
                _selectedId = bagItem.ItemInfo.Id;
                _selectCallBack?.Invoke(bagItem.ItemInfo.Id);
                Hide();
            }
        }

        private void SetList()
        {
        //     var role = Game.Main.GetComponent<Role>();
        //     var roleBag = role.GetComponent<RoleBag>();
            
        List<object> items = RoleModel.Instance.GetItemListData(_itemTypes.ToArray());
            List.SetListData(items);
        }
    }
}