using BPA.KitChen.GroupMeal.Core.Common;
using BPA.KitChen.GroupMeal.Handlers;
using BPA.KitChen.GroupMeal.SqlSugar;
using BPA.MQTTClient;
using Furion;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using Newtonsoft.Json.Linq;
using SqlSugar;
using XExten.Advance.LinqFramework;

var builder = WebApplication.CreateBuilder(args).Inject();

//JWT鉴权
builder.Services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
builder.Services.AddMqttClientHostedService(op =>
{
    op.Port = int.Parse(App.Configuration["dev1_brokerHostSettings:Port"]);
    op.Server = App.Configuration["dev1_brokerHostSettings:Host"];
    op.UserName = App.Configuration["MqttClientSettings:UserName"];
    op.Password = App.Configuration["MqttClientSettings:Password"];
    op.mqttClientConnectedHandlerDelegate = new MqttClientConnectedHandlerDelegate(async e =>
    {
        Console.WriteLine("MQTT连接成功");
    });
    op.mqttClientDisconnectedHandlerDelegate = new MqttClientDisconnectedHandlerDelegate(async e =>
    {
        Console.WriteLine("MQTT断开连接");
        await Task.Delay(TimeSpan.FromSeconds(5));
        try
        {
            //var options = op.Server.GetService<IMqttClientOptions>();
            //await op.Server.GetService<IMqttClient>().ConnectAsync(options);
        }
        catch (global::System.Exception)
        {


        }
    });
    op.MqttApplicationMessageReceivedHandler = new MQTTnet.Client.Receiving.MqttApplicationMessageReceivedHandlerDelegate(async e =>
    {

    });
});


CosConfig.cosInfoOptions = new CosInfoOptions(App.Configuration["cos_config:AppId"],
    App.Configuration["cos_config:Region"], App.Configuration["cos_config:Bucket"],
    App.Configuration["cos_config:SecretId"] , App.Configuration["cos_config:SecretKey"]);

//设置返回样式
builder.Services.AddUnifyResult();

builder.Services.AddControllers().AddInject();
builder.Services.AddSqlsugarSetup();


var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseHttpsRedirection();

app.UseAuthentication();
app.UseAuthorization();

app.UseInject();

app.MapControllers();

app.Run();
