using CloudGaming.Code.Game;

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();
        }
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {

        return Task.CompletedTask;
    }
}
