using System;
using System.Threading.Tasks;
using BPA.AGV;
using BPA.ApiClient;
using BPA.MES.Base.Application;
using BPA.MES.Base.Application.Subscriber;
using BPA.MES.Base.Core;
using BPA.MQTTClient;
using Furion;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MQTTnet.Client;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Options;
using Newtonsoft.Json.Linq;
using Yitter.IdGenerator;

namespace BPA.MES.Base.Web.Core;

public class Startup : AppStartup
{
    public void ConfigureServices(IServiceCollection services)
    {

        services.AddConsoleFormatter();
        services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
        services.AddCorsAccessor();
        services.AddSqlsugarSetup(App.Configuration);
        // 配置雪花Id算法机器码
        YitIdHelper.SetIdGenerator(new IdGeneratorOptions
        {
            WorkerId = 5
        });
        // 注册 EventBus 服务
        services.AddEventBus(builder =>
        {
            // 通过类型注册
            builder.AddSubscriber(typeof(ToDoEventSubscriber));
        });
        services.AddRemoteRequest();

        services.AddWebApiClient();
        services.AddAGV(op =>
        {
            op.Header = new KCOption()
            {
                AppKey = "43",
                AppSecret = "12",
                RequestId = "43",
                Timestamp = "2234234324",
                Version = "2.9"
            };
            op.Sign = "123456";
        });

        //services.AddMqttClientHostedService(op =>
        //{
        //    op.Port = 1883;
        //    op.Server = "10.2.1.21";
        //    op.UserName = "emqx_u_block";
        //    op.Password = "emqx_p_admin8765490789";
        //    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 = builder.Build().Services.GetService<IMqttClientOptions>();
        //            //await builder.Build().Services.GetService<IMqttClient>().ConnectAsync(options);
        //        }
        //        catch (global::System.Exception)
        //        {
        //        }
        //    });
        //    op.MqttApplicationMessageReceivedHandler = new MQTTnet.Client.Receiving.MqttApplicationMessageReceivedHandlerDelegate(async e =>
        //      {

        //      });
        //});

        services.AddControllers()
                .AddInjectWithUnifyResult();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseCorsAccessor();

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

        app.UseInject(string.Empty);

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}
