// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖 

using System;
using UnityEngine;
using FairyGUI;
using Fantasy;
using NBC;
using NBF.Fishing2;
using NBF.Utils;
using Log = NBC.Log;

namespace NBF
{
    public partial class BagSlotItem : GButton
    {
        public int Index { get; private set; }

        public Action<int, long> OnChangeSlot;

        private void OnInited()
        {
            onDrop.Add(OnDrop);
            BtnRemove.onClick.Set(OnRemove);
            onClick.Add(OnClick);
        }

        private void OnClick()
        {
            if (showType.selectedIndex == 0)
            {
                // Notices.Info("拖拽放入");
            }
        }
        
        private void OnRemove(EventContext context)
        {
            context.PreventDefault();
            context.StopPropagation();
            OnChangeSlot?.Invoke(Index, 0);
        }

        private void OnDrop(EventContext context)
        {
            var obj = context.initiator;
            if (obj is BagItem bagItem)
            {
                Log.Info($"放下了：{bagItem.ItemInfo.Id}");
                OnChangeSlot?.Invoke(Index, bagItem.ItemInfo.Id);
            }
        }

        public void SetData(int index, ItemInfo itemInfo = null)
        {
            Index = index;
            TextIndex.text = (index + 1).ToString();
            if (itemInfo != null)
            {
                showType.selectedIndex = 1;
                icon = itemInfo.ConfigId.GetIcon();
                if (itemInfo.Count > 1)
                {
                    TextCount.text = itemInfo.Count.ToString();
                }
                else
                {
                    TextCount.text = string.Empty;
                }

                Quality.SetQuality(itemInfo.Config.Quality);
            }
            else
            {
                showType.selectedIndex = 0;
                TextCount.text = string.Empty;
                icon = string.Empty;
            }
        }
    }
}