using CommunityToolkit.Mvvm.Messaging;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using SteUp.Data.LocalDb;
using SteUp.Data.LocalDb.EntityServices;
using SteUp.Maui.Core.Logger;
using SteUp.Maui.Core.Services;
using SteUp.Maui.Core.System;
using SteUp.Maui.Core.System.Network;
using SteUp.Shared.Core.BarcodeReader;
using SteUp.Shared.Core.BarcodeReader.Contracts;
using SteUp.Shared.Core.Data;
using SteUp.Shared.Core.Data.Contracts;
using SteUp.Shared.Core.Interface;
using SteUp.Shared.Core.Interface.IntegryApi;
using SteUp.Shared.Core.Interface.LocalDb;
using SteUp.Shared.Core.Interface.System;
using SteUp.Shared.Core.Interface.System.Network;
using SteUp.Shared.Core.Messages.Ispezione;
using SteUp.Shared.Core.Messages.Scanner;
using SteUp.Shared.Core.Messages.Scheda;
using SteUp.Shared.Core.Services;

namespace SteUp.Maui.Core;

public static class CoreModule
{
    extension(MauiAppBuilder builder)
    {
        public void RegisterAppServices()
        {
            builder.Services.AddSingleton<IFormFactor, FormFactor>();
            builder.Services.AddSingleton<IGenericSystemService, GenericSystemService>();

            builder.Services.AddScoped<ISteupDataService, SteupDataService>();
            builder.Services.AddSingleton<IBarcodeManager, BarcodeManager>();
        }

        public void RegisterIntegryServices()
        {
            builder.Services.AddScoped<IIntegryApiService, IntegryApiService>();
            builder.Services.AddScoped<IIntegrySteupService, IntegrySteupService>();
        }

        public void RegisterSystemService()
        {
            builder.Services.AddSingleton<INetworkService, NetworkService>();
            builder.Services.AddSingleton<IAttachedService, AttachedService>();
            builder.Services.AddSingleton<IFileManager, FileManager>();
            builder.Services.AddSingleton<IBarcodeReaderService, HoneywellScannerService>();
        }

        public void AddAuthorizationCore()
        {
            builder.Services.AddAuthorizationCore();
            builder.Services.AddScoped<AppAuthenticationStateProvider>();
            builder.Services.AddScoped<AuthenticationStateProvider>(provider =>
                provider.GetRequiredService<AppAuthenticationStateProvider>());
        }

        public void RegisterMessageServices()
        {
            builder.Services.AddSingleton<IMessenger, WeakReferenceMessenger>();
            builder.Services.AddSingleton<NewSchedaService>();
            builder.Services.AddSingleton<OnScannerService>();
            builder.Services.AddSingleton<CompleteInspectionService>();
        }

        public void RegisterDbServices()
        {
            builder.Services.AddSingleton<IDbPathProvider, DbPathProvider>();
            builder.Services.AddDbContext<AppDbContext>((sp, options) =>
            {
                var dbPath = sp.GetRequiredService<IDbPathProvider>().GetDbPath();
                options.UseSqlite($"Filename={dbPath}");
            });
            builder.Services.AddSingleton<IDbInitializer, DbInitializer>();
            builder.Services.AddSingleton<IIspezioniService, IspezioniService>();
        }

        public void RegisterLoggerServices()
        {
            var logPath = Path.Combine(FileSystem.AppDataDirectory, "logs");
            const string logFilePrefix = "SteUp-log";

            builder.Services.AddLogging(loggingBuilder =>
            {
                loggingBuilder.AddProvider(new FileLoggerProvider(logPath, logFilePrefix));
                loggingBuilder.SetMinimumLevel(LogLevel.Information);
            });
        }
    }
}