using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SlnMesnac.Config;
using SlnMesnac.Serilog;
using System;
using Autofac;
using Microsoft.Extensions.Configuration;
using SlnMesnac.Ioc;
using SlnMesnac.Extensions;
using SlnMesnac.Generate;
using SlnMesnac.TouchSocket;
using SlnMesnac.Business;
using SlnMesnac.Redis;

namespace SlnMesnac.WPF
{
    /// <summary>
    /// 
    /// </summary>
    public class Startup
    {
        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services"></param>
        [Obsolete]
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddSingleton<RedisHandler>();
            services.AddHostedService<MessageService>();

            services.AddControllers();

            //注册AppConfig
            services.AddSingleton<AppConfig>(provider =>
            {
                var configuration = provider.GetService<IConfiguration>();
                return configuration.GetSection("AppConfig").Get<AppConfig>();

            });


            services.AddSingleton<GenerateCode>();


            //ORM
            services.AddSqlSugarSetup();

            //PLC
            services.AddPlcFactorySetup();

            //RFID
            services.AddRfidFactorySetup();

            
        }

        /// <summary>
        /// AutoFac自动注入
        /// </summary>
        /// <param name="builder"></param>
        public void ConfigureContainer(ContainerBuilder builder)
        {
            DependencyConfigurator.Configure(builder);
        }

        /// <summary>
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //启用Serilog中间件
            app.UseSerilogExtensions();

            //初始化MES PLC 信号下发处理中间件
            app.UseMesPlcSingalHandleExtensions();

            app.UseRouting();

            app.UseAuthorization();

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

        }
    }
}