using Bogus.DataSets;

using CloudGaming.AppConfigModel;
using CloudGaming.Code.AppExtend;
using CloudGaming.Code.Contract;
using CloudGaming.Code.Filter;
using CloudGaming.Code.Game;
using CloudGaming.Code.JY;
using CloudGaming.DtoModel;
using CloudGaming.GameModel.Db.Db_Ext;

using HuanMeng.DotNetCore.CustomExtension;
using HuanMeng.DotNetCore.SwaggerUtile;

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.OpenApi.Models;

using Newtonsoft.Json.Serialization;

using QRCoder;

using Refit;

using System.Drawing.Imaging;
using System.Reflection;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMemoryCache();
builder.Services.AddHttpClient();
builder.Services.AddHttpContextAccessor(); //添加httpContext注入访问



#region  automap
var mapperDomain = AppDomain.CurrentDomain.GetAssemblies().Where(it => it.FullName.Contains("HuanMeng") || it.FullName.Contains("CloudGaming.")).ToList();
Type type = typeof(T_App_Config);
if (type != null)
{
    Assembly assembly = Assembly.GetAssembly(type);
    if (!mapperDomain.Any(it => it.FullName == assembly.FullName))
    {
        mapperDomain.Add(assembly);
    }
}

builder.Services.AddAutoMapper(mapperDomain);
#endregion

await builder.AddAppConfigClient();

builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();
app.MapGet("/qrcode/{identifier}/{userId}/{gameId}.png", async (string identifier, int userId, string gameId, IServiceProvider serviceProvider, IHttpContextAccessor httpContextAccessor) =>
{
    string qrcodeContent = $"{httpContextAccessor.HttpContext.Request.Scheme}://{httpContextAccessor.HttpContext.Request.Host.ToString()}/handle/{identifier}/{userId}/{gameId}.html";

    if (!string.IsNullOrEmpty(identifier))
    {
        if (identifier.ToLower() == "cloudgaming")
        {
            identifier = "default";
        }

        var app = AppConfigurationExtend.GetAppConfigIdentifier(identifier);
        if (app != null)
        {
            qrcodeContent = $"{app.Site.Web}/handle/{identifier}/{userId}/{gameId}.html";
        }
      
    }
    // 创建一个 QRCodeGenerator 实例
    var qrGenerator = new QRCodeGenerator();

    // 生成二维码数据（您可以在此处自定义二维码的内容）
    var qrCodeData = qrGenerator.CreateQrCode(qrcodeContent, QRCodeGenerator.ECCLevel.Q);

    // 生成二维码图像
    var qrCode = new BitmapByteQRCode(qrCodeData);

    // 获取二维码图像的字节数组
    byte[] qrCodeAsBitmapByteArr = qrCode.GetGraphic(20);

    // 返回二维码图片
    return Results.File(qrCodeAsBitmapByteArr, "image/png");
}).WithName("二维码请求");

app.Run();
