using CommunityToolkit.Maui;
using CommunityToolkit.Maui.Markup;

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting;

using RBLNews.Maui.Services;
using RBLNews.Shared.Models;
using RBLNews.Shared.Services;
using RBLNews.Shared.Services.Contracts;

namespace RBLNews.Maui
{
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                 .UseMauiCommunityToolkit()
                 .UseMauiCommunityToolkitMarkup()
                .ConfigureFonts(fonts =>
                {
                    //fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("NanumMyeongjo-Regular.ttf", "NanumMyeongjoRegular");
                });

            builder.Services.AddMauiBlazorWebView();
            builder.Services.AddBlazorBootstrap();
            builder.Services.AddSingleton<IConfigService, ConfigService>();
            builder.Services.AddSingleton<IFeedDataService, FeedDataService>();
            builder.Services.AddSingleton<AppLifecycleService>();

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

            MauiApp app = builder.Build();

            AppSettingsModel? appSettings = app.Configuration.GetSection("Logging").Get<AppSettingsModel>();

            if (string.IsNullOrEmpty(appSettings?.BackendApiUrl))
            {
                throw new Exception($"can't instantiate services, due to parameters are null. backendApiUrl: '{appSettings?.BackendApiUrl}'");
            }

            return app;
        }
    }
}
