using Common.Library;
using CommunityToolkit.Maui;
using GreadyPoang.DataLayer;
using GreadyPoang.DataLayer.Database;
using GreadyPoang.EntityLayer;
using GreadyPoang.Popups;
using GreadyPoang.Services;
using GreadyPoang.ViewModelLayer;
using GreadyPoang.Views;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace GreadyPoang;

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

        builder
            .UseMauiApp<App>()
            .UseMauiCommunityToolkit(options =>
            options.SetShouldEnableSnackbarOnWindows(true))
            .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_GreadyPoang.txt"))) ;
            {
                File.WriteAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MauiDataPath_GreadyPoang.txt"), MauiDataPath);
            }
            var dbPath = Path.Combine(MauiDataPath, "PoangDB.db");
            options.UseSqlite($"Data Source={dbPath}");
        });

        builder.Services.AddScoped<IRepository<Participant>, ParticipantRepository>();
        builder.Services.AddScoped<IMethodSharingService<Participant>, MethodSharingService>();

        builder.Services.AddScoped<ParticipantViewModel>();
        builder.Services.AddScoped<ParticipantListView>();

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

        builder.Services.AddScoped<RoundStartingViewModel>();
        builder.Services.AddScoped<RoundStartingView>();

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

        builder.Services.AddScoped<RoundRunningViewModel>();
        builder.Services.AddScoped<RoundRunningView>();

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

        builder.Services.AddTransientPopup<InfoPopup, InfoPopupViewModel>();
        builder.Services.AddSingleton<IPopupEventHub, PopupEventHub>();


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

        return builder.Build();
    }
}
