using CloudGaming.DtoModel;

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

namespace CloudGaming.Code.Config;

/// <summary>
/// AppConfig扩展
/// </summary>
public static class AppConfigBLLExtend
{

    /// <summary>
    /// 获取app是否在审核中
    /// </summary>
    /// <param name="app_Configs"></param>
    /// <param name="appRequestInfo"></param>
    /// <returns></returns>
    public static bool GetAppIsChecking(this List<AppConfigCache> app_Configs, AppRequestConfig appRequestInfo)
    {
        var config = GetAppConfig(app_Configs, (int)AppConfigType.审核配置, 1, appRequestInfo);
        if (config == null)
        {
            return false;
        }
        return config.IsEnabled;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="app_Configs"></param>
    /// <param name="cfgType"></param>
    /// <param name="cfgId"></param>
    /// <param name="appRequestInfo"></param>
    /// <returns></returns>
    public static AppConfigCache GetAppConfig(this List<AppConfigCache> app_Configs, int cfgType, int cfgId, AppRequestConfig? appRequestInfo)
    {
        var list = GetAppCfgList(app_Configs, cfgType, cfgId, appRequestInfo?.Channel, appRequestInfo?.Platform, appRequestInfo.VersionNum, appRequestInfo?.Continent, appRequestInfo?.CountryName);
        return list?.FirstOrDefault();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="app_Configs"></param>
    /// <param name="cfgType"></param>
    /// <param name="cfgId"></param>
    /// <param name="appRequestInfo"></param>
    /// <returns></returns>
    public static AppConfigCache GetAppConfig(this List<AppConfigCache> app_Configs, AppConfigType cfgType, int cfgId, AppRequestConfig? appRequestInfo)
    {
        var list = GetAppCfgList(app_Configs, (int)cfgType, cfgId, appRequestInfo?.Channel, appRequestInfo?.Platform, appRequestInfo.VersionNum, appRequestInfo?.Continent, appRequestInfo?.CountryName);
        return list?.FirstOrDefault();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="app_Configs"></param>
    /// <param name="cfgType"></param>
    /// <param name="cfgId"></param>
    /// <param name="appRequestInfo"></param>
    /// <returns></returns>
    public static List<AppConfigCache> GetAppCfgList(this List<AppConfigCache> app_Configs, int cfgType, int cfgId, AppRequestConfig appRequestInfo)
    {
        return GetAppCfgList(app_Configs, cfgType, cfgId, appRequestInfo.Channel, appRequestInfo.Platform, appRequestInfo.VersionNum, appRequestInfo.Continent, appRequestInfo.CountryName);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="app_Configs"></param>
    /// <param name="cfgType"></param>
    /// <param name="cfgId"></param>
    /// <param name="bossId"></param>
    /// <param name="plat"></param>
    /// <param name="appVersion"></param>
    /// <param name="continent"></param>
    /// <param name="countryName"></param>
    /// <returns></returns>
    public static List<AppConfigCache> GetAppCfgList(this List<AppConfigCache> app_Configs, int cfgType, int cfgId, string bossId, string plat = "", int appVersion = 0, string continent = "", string countryName = "")
    {
        //有很多老配置，没有appid的参与
        var lst = app_Configs.Where(it => it.ConfigType == cfgType && it.ConfigId == cfgId).ToList();

        // 计算地区、国家
        lst = lst.Where(it => it.IsContinentMatch(continent)).ToList();
        lst = lst.Where(it => it.IsCountryNameMatch(countryName)).ToList();

        // 计算平台
        lst = lst.Where(it => it.IsPlatformMatch(plat)).ToList();

        // 计算版本号
        lst = lst.Where(it => it.IsAppVersion(appVersion)).ToList();

        // 计算渠道号
        lst = lst.Where(it => it.IsChannelIdMatch(bossId)).ToList();
        return lst;
    }

}
