using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using XLAB2.Infrastructure;

namespace XLAB2
{
    internal static class AppHost
    {
        public static IHost Create()
        {
            return Host.CreateDefaultBuilder()
                .UseContentRoot(AppContext.BaseDirectory)
                .ConfigureAppConfiguration((context, config) =>
                {
                    config.Sources.Clear();
                    config.SetBasePath(AppContext.BaseDirectory);
                    config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                    config.AddEnvironmentVariables();
                })
                .ConfigureServices((_, services) =>
                {
                    services.AddSingleton<IDatabaseConnectionFactory>(_ => SqlServerConnectionFactory.Current);
                    services.AddTransient<PsvDataService>();
                    services.AddTransient<MainWindow>(provider => new MainWindow(provider.GetRequiredService<PsvDataService>()));
                })
                .UseDefaultServiceProvider((_, options) =>
                {
                    options.ValidateOnBuild = true;
                    options.ValidateScopes = true;
                })
                .Build();
        }
    }
}
