using CloudGaming.Code.Cache;
using CloudGaming.DtoModel.Epg;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudGaming.Code.Epg
{
    /// <summary>
    /// 
    /// </summary>
    public class EpgBLL : CloudGamingBase
    {
        public EpgBLL(IServiceProvider serviceProvider) : base(serviceProvider)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public async Task<List<EpgCategoryDto>> GetHomeInfo()
        {
            var listQueryable = GetEpgCategory(EpgEnum.EpgCatIdName.大厅首页).AsQueryable();
            if (IsChecking)
            {
                listQueryable = listQueryable.Where(it => it.ShowStatus == 0 || it.ShowStatus == 2).AsQueryable();
            }
            else
            {
                listQueryable = listQueryable.Where(it => it.ShowStatus == 0 || it.ShowStatus == 1).AsQueryable();
            }
            var list = listQueryable.OrderBy(it => it.OrderId).ToList();

            List<EpgCategoryDto> epgCategoryDtos = new List<EpgCategoryDto>();
            list.ForEach(it =>
            {
                var list = new List<EpgInfo>()
                {
                    //STEAMCLOUD
                };
                var epgList = GetEpgList(it.Id);
                epgList?.ForEach(item =>
                {
                    //如果首页展示数量小于集合数量，则退出
                    if (it.ShowNumIndex < list.Count)
                    {
                        return;
                    }
                    
                    var epgInfo = item.ToEpgInfo(Cache.GameEntityCache,it.DefaultImageStyle);
                    if (epgInfo != null)
                    {
                        list.Add(epgInfo);
                    }
                });

                if (list.Count > 0)
                {
                    EpgCategoryDto epgCategoryDto = new EpgCategoryDto()
                    {
                        CategoryName = it.Name,
                        CategoryType = it.IdName,
                        IsQuickStartPopUp = it.IsQuickStartPopUp ?? false,
                        ShowNum_Index = it.ShowNumIndex ?? 0,
                        EpgList = list
                    };

                    epgCategoryDtos.Add(epgCategoryDto);
                }
            });
            return epgCategoryDtos;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="idName"></param>
        /// <returns></returns>
        public List<T_Epg_CategoryCfg> GetEpgCategory(string idName)
        {
            var epg_CategoryCfg = Cache.EpgCategoryCfg.Where(it => it.IdName == idName).FirstOrDefault();
            if (epg_CategoryCfg != null)
            {
                return Cache.EpgCategoryCfg.Where(it => it.EpgParentCategory == epg_CategoryCfg.Id).ToList();
            }
            return new List<T_Epg_CategoryCfg>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="idName"></param>
        /// <returns></returns>
        public List<T_Epg_Cfg> GetEpgList(int epgParentCategoryId)
        {
            var list = CloudGamingCacheExtend.GetDataEntityCache<T_Epg_Cfg>(this, it => it.IsOnline);
            DateTime currentHour = DateTime.Now.Date.AddHours(DateTime.Now.Hour);
            if (list?.DataList?.Count > 0)
            {
                var dataList = list.DataList.Where(it => it.EpgCategory == epgParentCategoryId &&
               (it.StartEnableTime == null || it.StartEnableTime < currentHour)
                && (it.EndEnableTime == null || it.EndEnableTime > currentHour)).ToList();
                return dataList;
            }
            return null;
        }
    }
}
