using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using SlnMesnac.Serilog;

namespace SlnMesnac.WCS
{
    internal class Program
    {
        private static SerilogHelper? serilogHelper;

        private static void Main(string[] args)
        {
            try
            {
                Thread.CurrentThread.Name = "Main";
                Console.ForegroundColor = ConsoleColor.Yellow;
                AppDomain.CurrentDomain.UnhandledException += GlobalExceptionHandler; //全局异常捕获
                var host = CreateHostBuilder(args).Build();

                var hostTask = host.RunAsync();

                serilogHelper = host.Services.GetRequiredService<SerilogHelper>();

                MainCentralControl mainCentralControl = new MainCentralControl(host);
                mainCentralControl.Start();

                // serilogHelper.Info("WCS调度系统启动成功");
                while (true)
                {
                    Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                if (serilogHelper != null)
                {
                    serilogHelper.Error("系统启动失败，异常：" + ex.Message);
                }
            }
        }

        private static void GlobalExceptionHandler(object sender, UnhandledExceptionEventArgs e)
        {
            //当前线程名称
            Thread.CurrentThread.Name = "Error";
            if (e.ExceptionObject is Exception exception)
            {
                Console.WriteLine("全局异常捕获:");
                Console.WriteLine(exception.Message);
                Console.WriteLine(exception.StackTrace);
                if (serilogHelper != null)
                {
                    serilogHelper.Error("全局异常捕获:" + exception.Message + "\n" + exception.StackTrace);
                }
            }
            else
            {
                Console.WriteLine("未知异常捕获:");
                Console.WriteLine(e.ExceptionObject);
                if (serilogHelper != null)
                {
                    serilogHelper.Error("未知异常捕获:" + e.ExceptionObject);
                }
            }
        }

        /// <summary>
        /// CreateHostBuilder
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseSerilog((context, config) =>
                {
                })
                .UseServiceProviderFactory(new AutofacServiceProviderFactory())
             .ConfigureServices((hostContext, services) =>
             {
                 //services.AddHostedService<ReadBusiness>();
             })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<SlnMesnac.WCS.Startup>();
                });
    }
}