@page "/article/new"
@page "/article/{id:guid}/edit"

@using Wave.Data
@using Microsoft.AspNetCore.Identity
@using System.Security.Claims

@rendermode @(new InteractiveServerRenderMode(false))

@inject UserManager<ApplicationUser> UserManager
@inject IStringLocalizer<ArticleEditor> Localizer

<PageTitle>@(Localizer["EditorTitle"] + TitlePostfix)</PageTitle>

@if (User is null) {
	<h1 class="text-3xl lg:text-5xl font-light mb-6 text-primary">@Localizer["EditorTitle"]</h1>

	<div class="flex place-content-center">
		<span class="loading loading-spinner loading-lg"></span>
	</div>
} else {
	<ErrorBoundary>
		<ChildContent>
			<h1 class="text-3xl lg:text-5xl font-light mb-6 text-primary">@Localizer["EditorTitle"]</h1>
			
			<Wave.Components.Pages.Partials.ArticleEditorPartial Id="@Id" User="@User" ClaimsUser="@ClaimsUser" />
		</ChildContent>
		<ErrorContent>
			<h1 class="text-3xl lg:text-5xl font-light mb-6">Not found</h1>
		</ErrorContent>
	</ErrorBoundary>
}


@code {
	[CascadingParameter(Name = "TitlePostfix")]
	private string TitlePostfix { get; set; } = default!;
	[CascadingParameter]
	private Task<AuthenticationState>? AuthenticationState { get; set; }

	[Parameter]
	public Guid? Id { get; set; }
	private ApplicationUser? User { get; set; }
	private ClaimsPrincipal? ClaimsUser { get; set; }
	
	protected override async Task OnInitializedAsync() {
		if (AuthenticationState is null) throw new ApplicationException("???");
		var state = await AuthenticationState;
		ClaimsUser = state.User;
		var user = await UserManager.GetUserAsync(state.User);
		User = user ?? throw new ApplicationException("???2");
	}
}
