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

using System.Collections.Generic;
using System.Diagnostics;
using FairyGUI;
using Fantasy;
using Fantasy.Async;
using NBC;
using NUnit.Framework;
using UnityEngine;
using Log = NBC.Log;
using UIPanel = NBC.UIPanel;

namespace NBF
{
    public partial class FishingShopPanel : UIPanel
    {
        protected override void OnInit()
        {
            base.OnInit();
        }

        protected override void OnShow()
        {
            InputManager.Instance.On(this);
            ItemList.List.OnClickItem += OnClickItem;

            GetShopData().Coroutine();

            // List<TabItemData> tabItemList = GoodsConfigHelper.TabItemList;

            // ItemList.SetData(tabItemList, true, true);
        }

        private async FTask GetShopData()
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            loading.selectedIndex = 0;
            var response = (Game2C_GetShopItemsResponse)await Net.Call(new C2Game_GetShopItemsRequest());
            //处理数据
            var tabItemList = response.Items.ToTabItemData();
            ItemList.SetData(tabItemList, true, true);
            loading.selectedIndex = 1;
            stopwatch.Stop();
            Log.Info("商店物品获取耗时=" + stopwatch.ElapsedMilliseconds);
        }


        private void OnClickItem(object item)
        {
            if (item is not ShopGearItem gearItem) return;
            // ItemDetailsPanel.s
            // ItemDetailsPanel.Show(bagItem.ItemInfo);
            ShopDetailsPanel.Show(gearItem.Info);
        }

        #region UI事件

        [InputInvoke(InputDef.UI.Prev, UIInputButtonShowMode.MenuLeft)]
        private void OnPrev()
        {
            ItemList.Menu.OnClickBtnPrev();
        }

        [InputInvoke(InputDef.UI.Next, UIInputButtonShowMode.MenuRight)]
        private void OnNext()
        {
            ItemList.Menu.OnClickBtnNext();
        }

        [InputInvoke(InputDef.UI.Left, UIInputButtonShowMode.BottomLeft)]
        private void OnLeft()
        {
            ItemList.List.Selector.Left();
        }

        [InputInvoke(InputDef.UI.Right, UIInputButtonShowMode.BottomLeft)]
        private void OnRight()
        {
            ItemList.List.Selector.Right();
        }

        [InputInvoke(InputDef.UI.Up, UIInputButtonShowMode.BottomLeft)]
        private void OnUp()
        {
            ItemList.List.Selector.Up();
            // ChangeListSelected();
        }

        [InputInvoke(InputDef.UI.Down, UIInputButtonShowMode.BottomLeft)]
        private void OnDown()
        {
            ItemList.List.Selector.Down();
        }

        [InputInvoke(InputDef.UI.Enter, UIInputButtonShowMode.BottomLeft, "查看")]
        private void OnApplySettings()
        {
            var selectedItem = ItemList.List.Selector.SelectedItem;
            if (selectedItem != null)
            {
                ItemList.List.InvokeClickItem(selectedItem);
            }
        }


        [InputInvoke(InputDef.UI.Back, UIInputButtonShowMode.BottomRight)]
        private void OnBack()
        {
            Hide();
        }

        #endregion

        protected override void OnHide()
        {
            ItemList.List.OnClickItem -= OnClickItem;
            InputManager.Instance.Off(this);
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();
        }
    }
}