using BookReader.Services;
using BookReader.ViewModels;
using BookReader.Views;
using CommunityToolkit.Maui;
using Microsoft.Extensions.Logging;

namespace BookReader;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();

        builder
            .UseMauiApp<App>()
            .UseMauiCommunityToolkit()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });

#if DEBUG
        builder.Services.AddHybridWebViewDeveloperTools();
        builder.Logging.AddDebug();
#endif

        // Register Services (Singleton for shared state)
        builder.Services.AddSingleton<IDatabaseService, DatabaseService>();
        builder.Services.AddSingleton<IBookParserService, BookParserService>();
        builder.Services.AddSingleton<ISettingsService, SettingsService>();
        builder.Services.AddSingleton<INavigationService, NavigationService>();
        builder.Services.AddSingleton<ICoverCacheService, LruCoverCacheService>();
        builder.Services.AddSingleton<ICachedImageLoadingService, CachedImageLoadingService>();

        // HTTP Client for Calibre
        builder.Services.AddHttpClient<ICalibreWebService, CalibreWebService>();

        // Register ViewModels
        builder.Services.AddTransient<BookshelfViewModel>();
        builder.Services.AddTransient<ReaderViewModel>();
        builder.Services.AddTransient<SettingsViewModel>();
        builder.Services.AddTransient<CalibreLibraryViewModel>();

        // Register Pages
        builder.Services.AddTransient<BookshelfPage>();
        builder.Services.AddTransient<ReaderPage>();
        builder.Services.AddTransient<SettingsPage>();
        builder.Services.AddTransient<CalibreLibraryPage>();

        return builder.Build();
    }
}