using Common.Library;
using Gready_Poang.CommandClasses;
using Gready_Poang.Core;
using Gready_Poang.DataLayer;
using Gready_Poang.DataLayer.Database;
using Gready_Poang.EntityLayer;
using Gready_Poang.InterFaces;
using Gready_Poang.LocalServices;
using Gready_Poang.Services;
using Gready_Poang.ViewModelLayer;
using Gready_Poang.Views;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace Gready_Poang;

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

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



        builder.Services.AddDbContext<DataContext>(options =>
        {
            var MauiDataPath = FileSystem.Current.AppDataDirectory;
            if (!File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MauiDataPath_Gready_Poang.txt"))) ;
            {
                File.WriteAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MauiDataPath_Gready_Poang.txt"), MauiDataPath);
            }
            var dbPath = Path.Combine(MauiDataPath, "PoangDB.db");
            options.UseSqlite($"Data Source={dbPath}");
        });

        //        builder.Services.AddScoped<IPageFactory, PageFactory>();
        builder.Services.AddSingleton<INavigationService, NavigationService>();
        builder.Services.AddSingleton<IServiceLocator, ServiceLocator>();

        builder.Services.AddScoped<IRepository<Participant>, ParticipantRepository>();
        builder.Services.AddScoped<ParticipantViewModelCommands>();
        builder.Services.AddScoped<ParticipantListView>();

        builder.Services.AddScoped<IRepository<GameRound>, GameRoundRepository>();
        builder.Services.AddScoped<RoundStartingViewModelCommands>();
        builder.Services.AddScoped<RoundStartingView>();

        builder.Services.AddScoped<IRepository<GamePoint>, GamePointRepository>();
        builder.Services.AddScoped<RoundRunningViewModelCommands>();
        builder.Services.AddScoped<RoundRunningView>();

        builder.Services.AddScoped<IMethodSharingService<Participant>, MethodSharingService>();
        builder.Services.AddScoped<ICombinedRepository, CombinedRepository>();

        builder.Services.AddSingleton<IObjectMessageService, ObjectMessageService>();

        builder.Services.AddSingleton<AppShellViewModel>();
        builder.Services.AddSingleton<AppShell>();

        builder.Services.AddSingleton<SplashViewModelCommands>();
        builder.Services.AddSingleton<ISplashService, SplashService>();

        builder.Services.AddSingleton<MainPageViewModelCommands>();


#if DEBUG
        builder.Logging.AddDebug();
#endif
        //var app = builder.Build();
        //ServiceLocator.Services = app.Services;
        //return app;
        return builder.Build();
    }
}
