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

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

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

        protected override void OnInit()
        {
            this.AutoAddClick(OnBtnClick);
        }


        protected override void OnShow()
        {
            ItemList.List.OnClickItem += OnClickItem;
            InputManager.OnUICanceled += OnUICanceled;

            List<TabItemData> tabItemList = new List<TabItemData>();

            var dic = GetItemsByType();
            foreach (var (type, list) in dic)
            {
                TabItemData tabItem = new TabItemData
                {
                    Key = type.ToString()
                };
                tabItem.Items.AddRange(list);
                tabItemList.Add(tabItem);
            }

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

        private void OnBtnClick(GComponent btn)
        {
            if (btn == BtnGenAllIcon)
            {
                AutoGenIcon();
            }
        }


        private void OnUICanceled(string action)
        {
            if (!IsTop) return;
            if (action == InputDef.UI.SubPrev)
            {
            }
            else if (action == InputDef.UI.SubNext)
            {
            }
            else if (action == InputDef.UI.Up)
            {
            }
            else if (action == InputDef.UI.Down)
            {
            }
        }

        private void OnClickItem(object item)
        {
            if (item is not BagItem bagItem) return;
            PreviewDetailsPanel.Show(bagItem.ItemInfo);
        }


        #region 数据处理

        public Dictionary<ItemType, List<ItemInfo>> GetItemsByType()
        {
            // List<ItemInfo> Items = new List<ItemInfo>();
            _itemInfos.Clear();

            var configs = Game.Tables.TbItem.DataList.ToList();
            foreach (var itemConfig in configs)
            {
                ItemInfo itemInfo = new ItemInfo
                {
                    ConfigId = itemConfig.Id,
                    Count = 1
                };
                _itemInfos.Add(itemInfo);
            }

            var dic = new Dictionary<ItemType, List<ItemInfo>>();
            foreach (var item in _itemInfos)
            {
                var type = item.ConfigId.GetItemType();
                if (!dic.ContainsKey(type))
                {
                    dic.Add(type, new List<ItemInfo>());
                }

                dic[type].Add(item);
            }

            foreach (var (key, list) in dic)
            {
                list.Sort((x, y) => (int)(y.Config.Quality - x.Config.Quality));
            }

            return dic;
        }

        #endregion

        #region 自动生成所有icon

        private int _index = -1;

        private void AutoGenIcon()
        {
            // Timer.ClearAll(this);
            // Timer.Loop(2f, this, NextIcon);
            Game.Instance.StartCoroutine(NextIcon());
        }

        private IEnumerator NextIcon()
        {
            foreach (var item in _itemInfos)
            {
                GenModel.visible = true;
                // GenModel.SetData(item.Config);
                GenModel.SetData(new ItemInfo()
                {
                    Id = item.Config.Id,
                    ConfigId = item.Config.Id,
                    Count = 1,
                });
                yield return new WaitForSeconds(1f);
                GenModel.SaveRenderTextureToPNG(item.Config.Id);
                yield return new WaitForSeconds(1f);
            }

#if UNITY_EDITOR
            GenModel.visible = false;
            UnityEditor.AssetDatabase.Refresh();
            Timer.ClearAll(this);
            Log.Info("全部完成===");
            Notices.Success("全部图标生成完成");
#endif
        }

        #endregion

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

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