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

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

using static CloudGaming.DtoModel.Epg.EpgEnum;

namespace CloudGaming.Code.Epg
{
    public static class EpgExtend
    {

        /// <summary>
        /// 菜单转化游戏
        /// </summary>
        /// <param name="epgCfg"></param>
        /// <param name="gameEntityCache"></param>
        /// <returns></returns>
        public static EpgInfo ToEpgInfo(this T_Epg_Cfg epgCfg, GameEntityCache gameEntityCache, int defaultImageStyle)
        {
            if (epgCfg.ResType == (int)EpgEnum.EpgResType.游戏 && string.IsNullOrEmpty(epgCfg.ResId))
            {
                return null;
            }

            var epgInfo = new EpgInfo
            {
                CornerIcon = epgCfg.CornerIconId,
                EpgId = epgCfg.Id,
                ResId = epgCfg.ResId ?? "",
                ResType = epgCfg.ResType,
                IdName = epgCfg.IdName ?? "",
                ImageUrl = epgCfg.ImageId,
                Title = epgCfg.Title ?? "",
                SubTitle = epgCfg.Title2 ?? ""
            };

            if (epgCfg.ResType == (int)EpgEnum.EpgResType.游戏)
            {
                var gameInfo = gameEntityCache[epgCfg.ResId];
                if (gameInfo == null)
                {
                    return null;
                }

                if (string.IsNullOrEmpty(epgInfo.Title))
                {
                    epgInfo.Title = gameInfo.GameName ?? "";
                }
                if (string.IsNullOrEmpty(epgInfo.SubTitle))
                {
                    epgInfo.SubTitle = gameInfo.Title2 ?? "";
                }
                if (epgCfg.ImageResStyle == 0)
                {
                    epgCfg.ImageResStyle = defaultImageStyle;
                }
                epgInfo.ImageResStyle = epgCfg.ImageResStyle;
                //defaultImageStyle
                if (epgInfo.ImageUrl == 0 && epgCfg.ImageResStyle != 0)
                {
                    epgInfo.ImageUrl = ((ImageResStyle)epgCfg.ImageResStyle).GetImageResStyle(gameInfo);
                }

                epgInfo.Score = gameInfo.Score ?? string.Empty;
            }

            return epgInfo;
        }


    }
}
