using bundleboiler.ui;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using qdr.app.bundleboiler.business;
using qdr.app.bundleboiler.business.Services.Interfaces;
using qdr.app.bundleboiler.data;
using qdr.app.bundleboiler.data.Model;
using qdr.app.bundleboiler.ui.Configuration;
using Quadarax.Foundation.Core.Business.Injection;
using Quadarax.Foundation.Core.Logging;

namespace qdr.app.bundleboiler.ui
{
    internal static class Program
    {

        public static IServiceProvider? ServiceProvider { get; private set; }

        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // To customize application configuration such as set high DPI settings or default font,
            // see https://aka.ms/applicationconfiguration.


            ServiceProvider = CreateHostBuilder().Build().Services;

            ApplicationConfiguration.Initialize();
            Application.Run(new MainForm(ServiceProvider.GetService<IDatabaseService>(),ServiceProvider.GetService<ILogger>()));
        }


        private static IHostBuilder CreateHostBuilder()
        {
            var optionsCfg = new ApplicationOptions();
            

            return Host.CreateDefaultBuilder()
            .ConfigureServices((context, services) => {
                // configuration
                services.AddSingleton<IOptions, ApplicationOptions>();
                // db context
                services.AddDbContext<BoilerDbContext>(options =>
                    options.UseSqlServer(optionsCfg.ConnectionString ?? 
                    context.Configuration.GetConnectionString("DefaultConnection")));
                // mapper
                services.AddSingleton<DataMapper>();
                // logging
                services.AddSingleton<ILogger, Quadarax.Foundation.Core.NLog.LoggerFactory>();
                // business services
                DiManager.RegisterAssemblyFromThisAppdomain(["bundleboiler.business", "bundleboiler.data"], Array.Empty<string>());
                DiManager.ScanAndRegisterDependencyInjections(services);
            });
        }
    }
}