using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using TechHelper.Client;
using MudBlazor;
using BlazorProducts.Client.HttpInterceptor;
using Entities.Configuration;
using Microsoft.AspNetCore.Components.Authorization;
using MudBlazor.Services;
using TechHelper.Client.AuthProviders;
using TechHelper.Client.HttpRepository;
using TechHelper.Features;
using TechHelper.Client.Services;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using TechHelper.Client.AI;
using Microsoft.AspNetCore.Components;
using TechHelper.Context;


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);
});

builder.Services.AddAutoMapper(typeof(AutoMapperProFile).Assembly);


builder.Services.Configure<ApiConfiguration>(builder.Configuration.GetSection("ApiConfiguration"));
builder.Services.AddAuthorizationCore();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddLocalStorageServices();



builder.Services.AddScoped<IAuthenticationClientService, AuthenticationClientService>();
builder.Services.AddScoped<IExamService, ExamService>();
builder.Services.AddScoped<IStudentSubmissionService, StudentSubmissionService>();
builder.Services.AddScoped<IStudentSubmissionDetailService, StudentSubmissionDetailService>();
builder.Services.AddScoped<AuthenticationStateProvider, AuthStateProvider>();
builder.Services.AddScoped<IRefreshTokenService, RefreshTokenService>();
builder.Services.AddScoped<IClassServices, ClasssServices>();
builder.Services.AddScoped<IEmailSender, QEmailSender>();
builder.Services.AddScoped<HttpInterceptorHandlerService>();
builder.Services.AddScoped<IAIService, AiService>();
builder.Services.AddScoped<IUserServices, UserServices>();
builder.Services.AddHttpClient("WebApiClient", client =>
{
	var baseAddress = builder.Configuration.GetSection("ApiConfiguration:BaseAddress").Value;
	client.BaseAddress = new Uri(baseAddress+"/api/");
	client.Timeout = TimeSpan.FromSeconds(30);
	client.DefaultRequestHeaders.Add("Accept", "application/json");
}).AddHttpMessageHandler<HttpInterceptorHandlerService>();
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("WebApiClient"));


builder.Services.AddHttpClient("AuthClient", client =>
{
	var baseAddress = builder.Configuration.GetSection("ApiConfiguration:BaseAddress").Value;
	client.BaseAddress = new Uri(baseAddress);
	client.Timeout = TimeSpan.FromSeconds(30);
	client.DefaultRequestHeaders.Add("Accept", "application/json");
});

//builder.Services.AddHttpClient("Default", (sp, cl) =>
//{
//	var apiConfiguration = sp.GetRequiredService<IOptions<ApiConfiguration>>().Value;
//	cl.BaseAddress = new Uri(apiConfiguration.BaseAddress + "/api/");
//})
//	.AddHttpMessageHandler<HttpInterceptorHandlerService>();


//builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
//builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("Default"));



builder.Services.AddMudServices(config =>
{
	config.SnackbarConfiguration.PositionClass = Defaults.Classes.Position.BottomLeft;

	config.SnackbarConfiguration.PreventDuplicates = false;
	config.SnackbarConfiguration.NewestOnTop = false;
	config.SnackbarConfiguration.ShowCloseIcon = true;
	config.SnackbarConfiguration.VisibleStateDuration = 1000;
	config.SnackbarConfiguration.HideTransitionDuration = 500;
	config.SnackbarConfiguration.ShowTransitionDuration = 500;
	config.SnackbarConfiguration.SnackbarVariant = Variant.Filled;
});
await builder.Build().RunAsync();
