using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using RealEstateTransactions.Repositories;
using RealEstateTransactions.Repositories.Implementations;
using Serilog;
using Unity;
using Unity.Microsoft.Logging;

namespace RealEstateTransactions
{
    internal static class Program
    {
        /// <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.
            ApplicationConfiguration.Initialize();
            Application.Run(CreateContainer().Resolve<FormGeneral>());
        }

        private static IUnityContainer CreateContainer()
        {
            var container = new UnityContainer();

            container.RegisterType<IApartmentRepository, ApartmentRepository>();
            container.RegisterType<IBuyerRepository, BuyerRepository>();
            container.RegisterType<IDealRepository, DealRepository>();
            container.RegisterType<IPreSalesServicesRepository, PreSalesServicesRepository>();
            container.RegisterType<IServicesRepository, ServicesRepository>();
            container.RegisterType<IConnectionString, ConnectionString>();

            container.AddExtension(new LoggingExtension(CreateLoggerFactory()));

            return container;
        }

        private static LoggerFactory CreateLoggerFactory()
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddSerilog(new LoggerConfiguration()
            .ReadFrom.Configuration(new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build())
            .CreateLogger());
            return loggerFactory;
        }

    }
}