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

using System;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
using Fantasy;
using Log = NBC.Log;

namespace NBF
{
    public class ClassifyListTitleData
    {
        public string Title;
    }

    public partial class ClassifyList : GComponent
    {
        private readonly List<object> _listData = new List<object>();

        public event Action<object> OnClickItem;
        public event Action<object> OnDoubleClickItem;

        private int _columnsCount;

        public ListSelector Selector;

        private void OnInited()
        {
            List.onClickItem.Add(OnClickListItem);
            Selector = new ListSelector(List, typeof(ListTitleItem));
        }

        public void InvokeClickItem(object data)
        {
            OnClickItem?.Invoke(data);
        }
        

        public void SetListData(List<object> listData,
            ListSelectionMode selectionMode = ListSelectionMode.Single)
        {
            List.selectedIndex = -1;
            _listData.Clear();

            foreach (var obj in listData)
            {
                _listData.Add(obj);
            }

            List.RemoveChildrenToPool();
            List.defaultItem = ListTitleItem.URL;
            List.itemProvider = GetListItemResource;
            List.itemRenderer = OnRenderItem;
            List.selectionMode = selectionMode;
            try
            {
                List.numItems = _listData.Count;
                List.ScrollToView(0);
            }
            catch (Exception e)
            {
                Log.Error(e);
            }

            _columnsCount = 6;
            Selector.Refresh();
        }

        void OnClickListItem(EventContext context)
        {
            OnClickItem?.Invoke(context.data);
            if (context.inputEvent.isDoubleClick)
            {
                OnDoubleClickItem?.Invoke(context.data);
            }
        }

        void OnRenderItem(int index, GObject obj)
        {
            var itemData = _listData[index];
            if (obj is ListItemBase item)
            {
                item.SetData(itemData);
            }
            else if (obj is BagItem bagItem)
            {
                bagItem.SetData(itemData as ItemInfo);
            }
            else if (obj is ListTitleItem titleItem)
            {
                titleItem.SetData(itemData);
                titleItem.SetSize(List.width, 32);
            }
            else if (obj is ShopGearItem shopGearItem)
            {
                shopGearItem.SetData(itemData as ShopItemInfo);
            }
        }


        //根据索引的不同，返回不同的资源URL
        string GetListItemResource(int index)
        {
            var itemData = _listData[index];
            

            if (itemData is ItemInfo itemInfo)
            {
                return BagItem.URL;
            }

            if (itemData is ShopItemInfo shopItemInfo)
            {
                return ShopGearItem.URL;
            }

            if (itemData is ClassifyListTitleData titleData)
            {
                return ListTitleItem.URL;
            }

            return List.defaultItem;
        }
    }
}