using CrazyStudio.Core.Common;
using CrazyStudio.Core.Common.AliyunExt.Oss.Tools;
using CrazyStudio.Core.Common.Converters;
using CrazyStudio.Core.Common.Eitities;
using CrazyStudio.Core.Common.Tools.Extensions;
using StackExchange.Redis;
using System.Configuration;
using System.Net;
using XPrint.Image;
using XPrintServer.Api.Hubs;
using XPrintServer.Business.Extensions;
using XPrintServer.Business.Services;
using XPrintServer.Business.Workflows.Configure;
using XPrintServer.DataModel;

var builder = WebApplication.CreateBuilder(args);
//添加配置文件信息
builder.Configuration.AddConfigureFiles();

var sm2Infos = builder.Configuration.GetSection("SM2Infos").Get<SM2Infos>()!;
builder.Services.AddSingleton(sm2Infos);

//new Aspose.Svg.License().SetLicense("assets/Aspose.Total.NET.lic");
//注入HttpContextAccessor
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
//添加 DistributedMemoryCache 服务
builder.Services.AddDistributedMemoryCache();
//添加 Session 服务
builder.Services.AddSession();

var redisOptions = builder.Configuration.GetSection("RedisOptions").Get<RedisOptions>();
var volInfos = builder.Configuration.GetSection("VolInfos").Get<VolInfos>()!;
var aliyunInfos = builder.Configuration.GetSection("AliyunInfos").Get<AliyunInfos>()!;
var comfyUIInfos = builder.Configuration.GetSection("ComfyUIInfos").Get<ComfyUIInfos>()!;
//var kelinInfos = builder.Configuration.GetSection("KelinInfos").Get<KelinInfos>()!;

var ossTool = new OssTool(aliyunInfos.AccessKeyId, aliyunInfos.AccessKeySecret, aliyunInfos.Oss.Region + ".aliyuncs.com");
//注入阿里云OSS工具
builder.Services.AddSingleton(ossTool);

//注入火山引擎相关配置
builder.Services.AddSingleton(volInfos);
//注入阿里云相关配置
builder.Services.AddSingleton(aliyunInfos);
//注入ComfyUI相关配置
builder.Services.AddSingleton(comfyUIInfos);
builder.Services.AddSingleton(new ComfyUIService(comfyUIInfos));
////注入可灵AI相关配置
//builder.Services.AddSingleton(kelinInfos);


string potracePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"SvgLibs/potrace.exe");
string pngquantPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"SvgLibs/pngquant.exe");
// 创建位图转SVG转换器实例
var converter = new ColorVectorConverter(potracePath, pngquantPath, false);
//注入SVG转换器实例
builder.Services.AddSingleton(converter);


//SqlSugar ORM 配置
builder.Services.AddSqlSugar(builder.Configuration);
//注入仓储
builder.Services.AddScoped(typeof(SqlSugarRepository<>));
//注入各Service
builder.Services.AddServices(builder.Configuration);

//设置SnowIdInfo
//builder.Services.SetSnowIdInfo(builder.Configuration);
//获取数据库配置
builder.Services.GetSqlSugarConfig(builder.Configuration);


// 跨域配置
builder.Services.AddCors(builder.Configuration);


builder.Services.AddSignalR((hubOptions) =>
{
    //hubOptions.SupportedProtocols!.Remove("json");
    //hubOptions.ClientTimeoutInterval = TimeSpan.FromSeconds(2);
    //hubOptions.KeepAliveInterval = TimeSpan.FromSeconds(1.5);
}).AddMessagePackProtocol();

//暂时不开启 SignalR Redis 集群,需要的时候配置好放开
//.AddStackExchangeRedis((options) =>
//{
//    options.Configuration = new ConfigurationOptions
//    {
//        EndPoints = { { IPAddress.Parse(redisOptions!.Host!), int.Parse(redisOptions!.Port!) } },
//        Password = redisOptions!.Password
//        //AsyncTimeout = 5000,
//        //SyncTimeout = 5000
//    };
//});


builder.Services.AddControllers().AddJsonOptions((options) =>
{
    //添加字符串到Guid的序列化处理，用来修复提交接收类型有Guid的情况下的错误
    options.JsonSerializerOptions.Converters.Add(new StringToGuidConverter());
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

//builder.Services.AddSingleton(new TextToImageModel());

var app = builder.Build();

app.MapHub<MsgHub>("/MsgHub");

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseSession();

app.UseCors("Policy");

//app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

#if DEBUG
app.Urls.Add("http://*:5020");
#endif

app.Run();
