using Azure;

using CloudGaming.AppConfigModel;
using CloudGaming.Code.AppExtend;
using CloudGaming.Code.Cache;
using CloudGaming.DtoModel;

using StackExchange.Redis;

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

using static SKIT.FlurlHttpClient.Wechat.TenpayV3.Models.CreateNewTaxControlFapiaoApplicationRequest.Types.Fapiao.Types;

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

        /// <summary>
        /// 获取app启动配置
        /// </summary>
        /// <returns></returns>
        public async Task<AppConfigDto> GetAppConfig()
        {
            var x = AppRequestInfo.VersionNum;
            AppConfigDto appConfigDto = new AppConfigDto()
            {
                IsAuthRealName = true
            };
            appConfigDto.IsChecking = IsChecking;
            appConfigDto.SignKey = AppConfig.TenantId.ToString("N");
            appConfigDto.NightCardTips = $"22点30分至次日7点畅玩无消耗";
            appConfigDto.ChannelId = AppConfig.GameConfig.ChannelId;
            appConfigDto.BsUrl = AppConfig.GameConfig.BsUrl.TrimEnd('/') + "/";
            appConfigDto.PrivacyAgreement = AppConfig.PrivacyAgreement;
            appConfigDto.UserAgreement = AppConfig.UserAgreement;
            return appConfigDto;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool GetAppIsChecking()
        {
            var list = Cache.AppConfigList.ToList();
            return list.GetAppIsChecking(AppRequestInfo);
        }

        /// <summary>
        /// 清除缓存
        /// </summary>
        /// <returns></returns>
        public bool ClearCacheData()
        {
            this.ClearLocalDataCache();
            return true;
        }
        /// <summary>
        /// 加载redis缓存
        /// </summary>
        /// <returns></returns>
        public bool ReloadLocalData()
        {
            this.ReloadLocalDataCache();
            return true;
        }

        /// <summary>
        /// 清除缓存
        /// </summary>
        /// <returns></returns>
        /// <summary>
        /// 清除缓存
        /// </summary>
        /// <returns></returns>
        public async IAsyncEnumerable<string> ClearAllCacheData()
        {
            // 开始总耗时计时
            var swAll = Stopwatch.StartNew();

            // 删除应用配置缓存
            var delAppConfig = Stopwatch.StartNew();
            var keysDeleted = (int)await RedisCache.KeysDeleteds("App:Config:*");
            delAppConfig.Stop();
            yield return $"删除app配置：{keysDeleted}, 耗时==》{delAppConfig.ElapsedMilliseconds.ToString("0.##")}毫秒";

            // 删除API接口缓存
            var apiCache = Stopwatch.StartNew();
            var keysDeleted1 = (int)await RedisCache.KeysDeleteds("cache:api:*");
            apiCache.Stop();
            yield return $"删除api接口缓存：{keysDeleted1}, 耗时==》{apiCache.ElapsedMilliseconds.ToString("0.##")}毫秒";

            // 删除图片缓存
            var imageCache = Stopwatch.StartNew();
            var imageDel = (int)await RedisCache.KeysDeleteds("cache:Image:*");
            imageCache.Stop();
            yield return $"删除图片缓存：{imageDel}, 耗时==》{imageCache.ElapsedMilliseconds.ToString("0.##")}毫秒";

            // 重新加载缓存
            var reloadCache = Stopwatch.StartNew();
            Cache.AppImageCache.LoadRedisDatabase();
            yield return $"重新加载图片缓存";
            Cache.GameEntityCache.ReloadData();
            yield return $"重新加载游戏缓存";
            Cache.AppConfigCache.ReloadData();
            yield return $"重新加载系统配置缓存";
            Cache.ProductCacheEntityCache.ReloadData();
            yield return $"重新加载产品缓存";
            Cache.RedemptionCodeEntityCache.ReloadData();
            yield return $"重新加载兑换码缓存";
            Cache.SevenDayEntityCache.ReloadData();
            yield return $"重新加载七天签到缓存";
            reloadCache.Stop();
            yield return $"全部重新加载缓存耗时==》{reloadCache.ElapsedMilliseconds.ToString("0.##")}毫秒";

            // 清除本地缓存
            var clearLocalCache = Stopwatch.StartNew();
            ClearCacheData();
            clearLocalCache.Stop();
            yield return $"清除本地缓存, 耗时==》{clearLocalCache.ElapsedMilliseconds.ToString("0.##")}毫秒";

            // 清除网络缓存
            var clearNetworkCache = Stopwatch.StartNew();
            var urls = AppConfig.CacheRequestUrls ?? new List<string>();
            foreach (var url in urls)
            {
                string resp = string.Empty;
                try
                {
                    var client = HttpClientFactory.CreateClient();
                    var data = await client.GetAsync(url);
                    resp = $"清除网络缓存{url}===》{data.StatusCode}==>{data.Content.ReadAsStringAsync()}";
                }
                catch (Exception ex)
                {
                    resp = $"清除网络缓存{url}失败===》{ex.Message}";
                }
                yield return resp;
            }
            clearNetworkCache.Stop();
            yield return $"清除网络缓存耗时==》{clearNetworkCache.ElapsedMilliseconds.ToString("0.##")}毫秒";

            // 停止总耗时计时
            swAll.Stop();
            yield return $"清除结束, 总耗时==》{swAll.ElapsedMilliseconds.ToString("0.##")}毫秒";
        }


    }
}
