@using Wave.Data
@using Microsoft.AspNetCore.Identity
@using System.ComponentModel.DataAnnotations
@using Wave.Utilities

@inject UserManager<ApplicationUser> UserManager
@inject IStringLocalizer<Wave.Components.Account.Pages.Manage.Index> Localizer

<EditForm FormName="about-me" Model="@Model" OnValidSubmit="OnValidSubmit" method="post" class="w-full">
    <DataAnnotationsValidator />
    <label class="form-control w-full">
        <div class="label">
            <span class="label-text">
                @Localizer["AboutTheAuthor_Label"]
                <HelpDropdownComponent Body="@Localizer["AboutTheAuthor_Explanation"]"/>
            </span>
        </div>
        <InputTextArea class="textarea textarea-bordered w-full h-24" maxlength="512"
                       @bind-Value="@Model.AboutTheAuthor" placeholder="@Localizer["AboutTheAuthor_Placeholder"]"/>
        <div class="label">
            <span class="label-text-alt text-error"><ValidationMessage For="() => Model.AboutTheAuthor"/></span>
        </div>
    </label>
    <label class="form-control w-full">
        <div class="label">
            <span class="label-text">
                @Localizer["Biography_Label"]
                <span class="badge badge-info badge-sm badge-outline">Markdown</span>
                <HelpDropdownComponent Body="@Localizer["Biography_Explanation"]"/>
            </span>
        </div>
        <InputTextArea class="textarea textarea-bordered w-full h-48" maxlength="4096"
                       @bind-Value="@Model.Biography" placeholder="@Localizer["Biography_Placeholder"]"/>
        <div class="label">
            <span class="label-text-alt text-error"><ValidationMessage For="() => Model.Biography"/></span>
        </div>
    </label>

    <button type="submit" class="btn btn-primary w-full">
        @Localizer["Submit"]
    </button>
</EditForm>
<a class="btn btn-secondary mt-3 w-full" href="/profile/@User.Id">
	<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6">
		<path fill-rule="evenodd" d="M18.685 19.097A9.723 9.723 0 0 0 21.75 12c0-5.385-4.365-9.75-9.75-9.75S2.25 6.615 2.25 12a9.723 9.723 0 0 0 3.065 7.097A9.716 9.716 0 0 0 12 21.75a9.716 9.716 0 0 0 6.685-2.653Zm-12.54-1.285A7.486 7.486 0 0 1 12 15a7.486 7.486 0 0 1 5.855 2.812A8.224 8.224 0 0 1 12 20.25a8.224 8.224 0 0 1-5.855-2.438ZM15.75 9a3.75 3.75 0 1 1-7.5 0 3.75 3.75 0 0 1 7.5 0Z" clip-rule="evenodd" />
	</svg>
	@Localizer["Profile_Link_Label"]
</a>

@code {
	[Parameter]
	public required ApplicationUser User { get; set; }

	[SupplyParameterFromForm(FormName = "about-me")]
	private InputModel Model { get; set; } = new();

	protected override void OnInitialized() {
		Model.AboutTheAuthor ??= User.AboutTheAuthor;
		Model.Biography ??= User.Biography;
	}

	private async Task OnValidSubmit(EditContext obj) {
		if (Model.Biography is not null) {
			User.BiographyHtml = MarkdownUtilities.Parse(Model.Biography);
			User.Biography = Model.Biography;
		}
		if (Model.AboutTheAuthor is not null)
			User.AboutTheAuthor = Model.AboutTheAuthor;

		await UserManager.UpdateAsync(User);
	}

	private sealed record InputModel {
		[MaxLength(512)] 
		public string? AboutTheAuthor { get; set; } 
		[MaxLength(4096)] 
		public string? Biography { get; set; }
	}

}
