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.Redis;
using System.Threading;

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本：4.0.30319.42000
* 机器名称：LAPTOP-E0N2L34V
* 命名空间：ConsoleApp
* 唯一标识：04133b44-1390-4f6a-aaef-88f4127d88f0
*
* 创建者：WenJY
* 电子邮箱：wenjy@mesnac.com
* 创建时间：2024-04-23 18:06:27
* 版本：V1.0.0
* 描述：
*
*--------------------------------------------------------------------
* 修改人：
* 时间：
* 修改说明：
*
* 版本：V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace PrintBarCode
{
    /// <summary>
    /// 
    /// </summary>
    public class Startup
    {

        private System.Threading.Mutex? mutex = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        public Startup(IConfiguration configuration)
        {
            bool ret;
            mutex = new System.Threading.Mutex(true, System.Diagnostics.Process.GetCurrentProcess().ProcessName, out ret);
            if (!ret)
            {
                MessageBox.Show("应用程序已开启，禁止重复运行");
                Environment.Exit(0);
            }

            Configuration = configuration;
        }

        /// <summary>
        /// 
        /// </summary>
        public IConfiguration Configuration { get; }

        /// <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.AddControllers();

            services.AddSingleton<RedisHandler>();
            services.AddSingleton<PrintBarCode.MessageClient>();

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

            });

            //注册ORM
            services.AddSqlSugarSetup();

            //// 注册 ElectricBusiness
            //services.AddScoped<ElectricBusiness>();

        }

        /// <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, PrintBarCode.MessageClient messageClient)
        {
            messageClient.StartListening();
            //启用Serilog中间件
            app.UseSerilogExtensions();

          


        }
    }
}