using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SewerStammGen.Shared.Contracts;
using SewerStammGen.WPF.Interface.Navigator;
using SewerStammGen.WPF.ViewModel;
using SewerStammGen.WPF.ViewModel.State.Navigation;
using Shared.Contracts;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace SewerStammGen.HostBuilders
{
    internal static class AddServicesHostBuilderExtensions
    {
        public static IHostBuilder AddServices(this IHostBuilder host)
        {
            host.ConfigureServices((context,services) =>
            {
                services.AddSingleton<IMainWindowNavigator, MainWindowNavigator>();
                services.AddSingleton<ViewModelDelegateRenavigator<ProjektEditViewModel>>();

                string? databaseToUse = context.Configuration.GetConnectionString("databaseToUse");
                if(databaseToUse != null)
                {
                    if(databaseToUse.Equals("postgresql"))
                    {
                        string? connectionstring = context.Configuration.GetConnectionString("postgresql");
                        if(connectionstring == null) throw new ArgumentNullException(nameof(connectionstring));
                        services.AddSingleton<IProjektDataService>(_=> new DAL.Services.PostgresqlData.ProjektDataService(connectionstring));
                        services.AddSingleton<IAuftraggeberDataService>(_ => new DAL.Services.PostgresqlData.AuftraggeberDataService(connectionstring));
                        services.AddSingleton<ISchachtDataService>(_ => new DAL.Services.PostgresqlData.SchachtDataService(connectionstring));
                        services.AddSingleton<IHaltungDataService>(_ => new DAL.Services.PostgresqlData.HaltungDataService(connectionstring));
                    }
                }
                
               
                
            });
            return host;
        }
    }
}
