using System;
using System.Collections.Generic;
using System.Linq;
using GameMessage;
using MrWu.Debug;

namespace Server.Config
{
    public partial class EntityExchangeMallCategory
    {
        public EntityMallProductsData EntityMallProductsData
        {
            get;
            private set;
        }

        /// <summary>
        /// 服务器当天兑换数据（商品ID -> 兑换数量）
        /// </summary>
        public Dictionary<int, int> ServerExchangeCount
        {
            get;
            private set;
        }

        partial void PostInit()
        {
            EntityMallProductsData = new EntityMallProductsData();
            EntityMallProductsData.Products = new List<EntityMallProductData>();
            EntityMallProductsData.Ver = DateTimeHelper.DateTime2TimeStamp(DateTime.Now);
            foreach (var config in _dataList)
            {
                if (config.NotShelved)
                {
                    continue;
                }

                EntityMallProductData data = new EntityMallProductData();
                data.Id = config.Id;
                data.Name = config.Name;
                data.RedMoney = config.RedMoney;
                data.ProductType = config.ProductType;
                data.MallType = config.MallType;
                data.DayLimit = config.DayLimit;
                data.UserDayLimit = config.UserDayLimit;
                data.Sort = config.Sort;
                data.Desc = config.Desc;

                int index = -1;
                for (int i = 0; i < EntityMallProductsData.Products.Count; i++)
                {
                    if (data.Sort < EntityMallProductsData.Products[i].Sort)
                    {
                        index = i;
                        break;
                    }else if (data.Sort == EntityMallProductsData.Products[i].Sort)
                    {
                        index = i + 1;
                    }
                    else
                    {
                        if (index > 0)
                        {
                            break;
                        }
                    }
                }

                //Debug.Info($"{data.Name} {index} {data.Sort} {EntityMallProductsData.Products.Count}");
                
                if (index > 0)
                {
                    EntityMallProductsData.Products.Insert(index,data);
                }
                else
                {
                    EntityMallProductsData.Products.Add(data);
                }
            }
        }
    }
}