using CloudGaming.Code.Game;

using HuanMeng.DotNetCore.Redis;

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

namespace CloudGaming.Code.AppExtend;


public class AppService(IServiceProvider scopeFactory) : IHostedService
{

    public Task StartAsync(CancellationToken cancellationToken)
    {
        var appConfigs = AppConfigurationExtend.AppConfigs.Where(it => it.Key != "default")
            .Select(it => it.Value)
            .ToList();

        // 为每个项目启动一个独立的任务
        foreach (var appConfig in appConfigs)
        {
            using var scope = scopeFactory.CreateScope();
            var scopedProvider = scope.ServiceProvider;
            var app = scopedProvider.GetRequiredService<AppConfig>();
            appConfig.ToAppConfig(app);
            CloudGamingBase cloudGamingBase = new CloudGamingBase(scopedProvider);
            //加载图片缓存
            cloudGamingBase.Cache.AppImageCache.ReloadData();
            //cloudGamingBase.RedisCache.StringGet("languages");
            //appConfig.InitAppLanguage();

        }
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {

        //var appConfigs = AppConfigurationExtend.AppConfigs.Where(it => it.Key != "default")
        //  .Select(it => it.Value)
        //  .ToList();

        //// 为每个项目启动一个独立的任务
        //foreach (var appConfig in appConfigs)
        //{
        //    using var scope = scopeFactory.CreateScope();
        //    var scopedProvider = scope.ServiceProvider;
        //    var app = scopedProvider.GetRequiredService<AppConfig>();
        //    appConfig.ToAppConfig(app);
        //    var languages = appConfig.GetLanguages();
        //    CloudGamingBase cloudGamingBase = new CloudGamingBase(scopedProvider);
        //    ////加载图片缓存
        //    //cloudGamingBase.Cache.AppImageCache.ReloadData();

        //    foreach (var language in languages)
        //    {
        //        var json = AppLanguage.LanguageToJson(appConfig, language);
        //        cloudGamingBase.RedisCache.StringSet($"languages:{language}", json);
        //    }
        //}
        return Task.CompletedTask;
    }
}
