using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using MudBlazor.Services;
using RobotNet.WebApp;
using RobotNet.WebApp.Clients;
using RobotNet.WebApp.Helpers;
using System.Globalization;
using System.Security.Claims;

DefaultPersistentStorageConfigurationExtensions.FixStaticConstructorException();
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddOidcAuthentication(options =>
{
    builder.Configuration.Bind("Local", options.ProviderOptions);
    options.UserOptions.RoleClaim = ClaimTypes.Role;
});

var scriptManagerAddress = builder.Configuration["ScriptManager:BaseAddress"] ?? throw new InvalidOperationException("ScriptManager -> BaseAddress not found.");
var robotManagerAddress = builder.Configuration["RobotManager:BaseAddress"] ?? throw new InvalidOperationException("RobotManager -> BaseAddress not found.");
var mapManagerAddress = builder.Configuration["MapManager:BaseAddress"] ?? throw new InvalidOperationException("MapManager -> BaseAddress not found.");

builder.Services.AddAuthorizationHttpClient("ScriptManagerAPI", scriptManagerAddress);
builder.Services.AddTransient(sp => new ConsoleHubClient(sp.GetRequiredService<IAccessTokenProvider>(), new Uri($"{scriptManagerAddress}/hubs/console")));
builder.Services.AddTransient(sp => new ProcessorHubClient(sp.GetRequiredService<IAccessTokenProvider>(), new Uri($"{scriptManagerAddress}/hubs/processor")));
builder.Services.AddTransient(sp => new ScriptTaskHubClient(sp.GetRequiredService<IAccessTokenProvider>(), new Uri($"{scriptManagerAddress}/hubs/scripttask")));
builder.Services.AddTransient(sp => new DashboardHubClient(sp.GetRequiredService<IAccessTokenProvider>(), new Uri($"{scriptManagerAddress}/hubs/dashboard")));
builder.Services.AddTransient(sp => new HMIHubClient(sp.GetRequiredService<IAccessTokenProvider>(), new Uri($"{scriptManagerAddress}/hubs/hmi")));

builder.Services.AddAuthorizationHttpClient("RobotManagerAPI", robotManagerAddress);
builder.Services.AddTransient(sp => new RobotHubClient(sp.GetRequiredService<IAccessTokenProvider>(), new Uri($"{robotManagerAddress}/hubs/robot/online")));
builder.Services.AddTransient(sp => new TrafficHubClient(sp.GetRequiredService<IAccessTokenProvider>(), new Uri($"{robotManagerAddress}/hubs/traffic")));

builder.Services.AddAuthorizationHttpClient("MapManagerAPI", mapManagerAddress);

builder.Services.AddMudServices(config =>
{
    config.SnackbarConfiguration.VisibleStateDuration = 2000;
    config.SnackbarConfiguration.HideTransitionDuration = 500;
    config.SnackbarConfiguration.ShowTransitionDuration = 500;
});

await builder.Build().RunAsync();
