using System.Collections.Generic;
using System.Linq;
using NBF.Utils;

namespace NBF
{
    public static class GoodsConfigHelper
    {
        public static readonly List<TabItemData> TabItemList = new List<TabItemData>();

        /// <summary>
        /// 组合和id映射关系
        /// </summary>
        private static readonly Dictionary<int, List<int>> _group2Id = new Dictionary<int, List<int>>();

        private static readonly Dictionary<int, cfg.Goods> _googs = new Dictionary<int, cfg.Goods>();

        // private static readonly Dictionary<>


        public static void Init()
        {
            var listGoods = Game.Tables.TbGoods.DataList; //GoodsConfig.GetList();
            TabItemList.Clear();


            foreach (var goodsConfig in listGoods)
            {
                var group = goodsConfig.Group;
                if (group < 1)
                {
                    // goodsConfig.Group = goodsConfig.Id;
                    group = goodsConfig.Id;
                }

                if (!_group2Id.TryGetValue(group, out List<int> ids))
                {
                    ids = new List<int>();
                    _group2Id.Add(group, ids);
                }

                ids.Add(goodsConfig.Id);
                _googs[goodsConfig.Id] = goodsConfig;
            }

            Dictionary<ItemType, List<cfg.Goods>> tabDic = new Dictionary<ItemType, List<cfg.Goods>>();
            foreach (var goodsId in _group2Id.Keys)
            {
                var good = _googs[goodsId];
                var awards = good.Awards;
                var type = awards.First().Id.GetItemType();
                if (!tabDic.ContainsKey(type))
                {
                    tabDic.Add(type, new List<cfg.Goods>());
                }

                tabDic[type].Add(good);
            }

            foreach (var (key, list) in tabDic)
            {
                list.Sort((x, y) => (int)(y.Price1 - x.Price1));
            }

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