using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Common.SecurityItems;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Web;

namespace SSOBackendApi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                var host = CustomWebHostBuilder();

                using (var scope = host.Services.CreateScope())
                {
                    var services = scope.ServiceProvider;
                }

                host.Run();
            }
            catch (Exception ex)
            {
                //NLog: catch setup errors
                ex.Message.ToString();
                throw;
            }
            finally
            {
                // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
                NLog.LogManager.Shutdown();
            }
        }

        public static IWebHost CustomWebHostBuilder()
        {
            var webHost = new WebHostBuilder()
                .UseKestrel()
                .ConfigureKestrel(options =>
                {
                    options.AddServerHeader = false;
                })
                .UseContentRoot(Directory.GetCurrentDirectory())
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    var env = hostingContext.HostingEnvironment;
                    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                    config.AddJsonFile("appsettings.Local.json", optional: true, reloadOnChange: true);
                    //.AddJsonFile($"appsettings.{env.EnvironmentName}.json",
                    //    optional: true, reloadOnChange: true)
                    ;
                    config.AddEnvironmentVariables();
                })
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.SetMinimumLevel(LogLevel.Trace);
                })
                .UseNLog()  // NLog: setup NLog for Dependency injection
                .UseStartup<Startup>()
                .Build();
            return webHost;
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.AddConsole();
                    logging.AddDebug();
                    logging.AddEventSourceLogger();
                })
                .Build();
    }
}
