using CommunityToolkit.Maui;
using CommunityToolkit.Mvvm.Messaging;
using IntegryApiClient.MAUI;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.Logging;
using MudBlazor.Services;
using MudExtensions.Services;
using salesbook.Maui.Core.RestClient.IntegryApi;
using salesbook.Maui.Core.Services;
using salesbook.Maui.Core.System.Network;
using salesbook.Maui.Core.System.Notification;
using salesbook.Maui.Core.System.Notification.Push;
using salesbook.Shared;
using salesbook.Shared.Core.Dto;
using salesbook.Shared.Core.Dto.PageState;
using salesbook.Shared.Core.Helpers;
using salesbook.Shared.Core.Interface;
using salesbook.Shared.Core.Interface.IntegryApi;
using salesbook.Shared.Core.Interface.System.Network;
using salesbook.Shared.Core.Interface.System.Notification;
using salesbook.Shared.Core.Messages.Activity.Copy;
using salesbook.Shared.Core.Messages.Activity.New;
using salesbook.Shared.Core.Messages.Back;
using salesbook.Shared.Core.Messages.Contact;
using salesbook.Shared.Core.Messages.Notification.Loaded;
using salesbook.Shared.Core.Messages.Notification.NewPush;
using salesbook.Shared.Core.Services;
using Shiny;

namespace salesbook.Maui
{
    public static class MauiProgram
    {
        private const string AppToken = "f0484398-1f8b-42f5-ab79-5282c164e1d8";

        public static MauiAppBuilder CreateMauiAppBuilder()
        {
            InteractiveRenderSettings.ConfigureBlazorHybridRenderModes();

            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .UseIntegry(appToken: AppToken, useLoginAzienda: true)
                .UseMauiCommunityToolkit()
                .UseShiny()
                .UseSentry(options =>
                {
                    options.Dsn = "https://453b6b38f94fd67e40e0d5306d6caff8@o4508499810254848.ingest.de.sentry.io/4509605099667536";
#if DEBUG
                    options.Debug = true;
#endif
                    options.TracesSampleRate = 1.0;
                })
                .ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); });

            builder.Services.AddMauiBlazorWebView();
            builder.Services.AddMudServices();
            builder.Services.AddMudExtensions();

            builder.Services.AddAutoMapper(typeof(MappingProfile));

            builder.Services.AddAuthorizationCore();
            builder.Services.AddScoped<AppAuthenticationStateProvider>();
            builder.Services.AddScoped<AuthenticationStateProvider>(provider =>
                provider.GetRequiredService<AppAuthenticationStateProvider>());

            builder.Services.AddScoped<IIntegryApiService, IntegryApiService>();
            builder.Services.AddScoped<ISyncDbService, SyncDbService>();
            builder.Services.AddScoped<IManageDataService, ManageDataService>();
            builder.Services.AddScoped<PreloadService>();

            //SessionData
            builder.Services.AddSingleton<JobSteps>();
            builder.Services.AddSingleton<UserPageState>();
            builder.Services.AddSingleton<UserListState>();
            builder.Services.AddSingleton<NotificationState>();
            builder.Services.AddSingleton<FilterUserDTO>();

            //Message
            builder.Services.AddSingleton<IMessenger, WeakReferenceMessenger>();
            builder.Services.AddSingleton<NewActivityService>();
            builder.Services.AddSingleton<BackNavigationService>();
            builder.Services.AddSingleton<CopyActivityService>();
            builder.Services.AddSingleton<NewContactService>();
            builder.Services.AddSingleton<NotificationsLoadedService>();
            builder.Services.AddSingleton<NewPushNotificationService>();

            //Notification
            builder.Services.AddNotifications();
            builder.Services.AddPush<PushNotificationDelegate>();
            builder.Services.AddSingleton<IIntegryRegisterNotificationRestClient, IntegryRegisterNotificationRestClient>();
            builder.Services.AddSingleton<IIntegryNotificationRestClient, IntegryNotificationRestClient>();
            builder.Services.AddSingleton<IFirebaseNotificationService, FirebaseNotificationService>();
            builder.Services.AddSingleton<IShinyNotificationManager, ShinyNotificationManager>();
            builder.Services.AddSingleton<INotificationService, NotificationService>();

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

            builder.Services.AddSingleton<IFormFactor, FormFactor>();
            builder.Services.AddSingleton<IAttachedService, AttachedService>();
            builder.Services.AddSingleton<INetworkService, NetworkService>();
            builder.Services.AddSingleton<LocalDbService>();

            return builder;
        }
    }
}