@page "/Account/Manage/SetPassword"

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

@inject UserManager<ApplicationUser> UserManager
@inject SignInManager<ApplicationUser> SignInManager
@inject IdentityUserAccessor UserAccessor
@inject IdentityRedirectManager RedirectManager
@inject IStringLocalizer<SetPassword> Localizer

<PageTitle>@Localizer["Title"]</PageTitle>

<StatusMessage Message="@Message"/>
<BoardComponent>
	<BoardCardComponent Heading="@Localizer["Title"]">
		<Alert CanRemove="false" Type="Alert.MessageType.Information">
			<p>@Localizer["Message"]</p>
		</Alert>
		<EditForm Model="Input" FormName="set-password" OnValidSubmit="OnValidSubmitAsync" method="post">
			<DataAnnotationsValidator/>
			<InputLabelComponent LabelText="@Localizer["NewPassword_Label"]" For="() => Input.NewPassword">
				<InputText type="password" @bind-Value="Input.NewPassword" class="input input-bordered w-full" autocomplete="new-password"
				           required aria-required="true" placeholder="@Localizer["NewPassword_Placeholder"]"/>
			</InputLabelComponent>
			<InputLabelComponent LabelText="@Localizer["ConfirmPassword_Label"]" For="() => Input.ConfirmPassword">
				<InputText type="password" @bind-Value="Input.ConfirmPassword" class="input input-bordered w-full" autocomplete="new-password"
				           required aria-required="true" placeholder="@Localizer["ConfirmPassword_Placeholder"]"/>
			</InputLabelComponent>
			<button type="submit" class="btn btn-primary w-full">
				@Localizer["ChangePassword_Submit"]
			</button>
		</EditForm>
	</BoardCardComponent>
</BoardComponent>

@code {
	private string? Message { get; set; }
	private ApplicationUser User { get; set; } = default!;

	[CascadingParameter] private HttpContext HttpContext { get; set; } = default!;

	[SupplyParameterFromForm] private InputModel Input { get; set; } = new();

	protected override async Task OnInitializedAsync() {
		User = await UserAccessor.GetRequiredUserAsync(HttpContext);

		bool hasPassword = await UserManager.HasPasswordAsync(User);
		if (hasPassword) {
			RedirectManager.RedirectTo("Account/Manage/ChangePassword");
		}
	}

	private async Task OnValidSubmitAsync() {
		var addPasswordResult = await UserManager.AddPasswordAsync(User, Input.NewPassword!);
		if (!addPasswordResult.Succeeded) {
			Message = $"Error: {string.Join(",", addPasswordResult.Errors.Select(error => error.Description))}";
			return;
		}

		await SignInManager.RefreshSignInAsync(User);
		RedirectManager.RedirectToCurrentPageWithStatus("Your password has been set.", HttpContext);
	}

	private sealed class InputModel {
		[Required]
		[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
		[DataType(DataType.Password)]
		[Display(Name = "New password")]
		public string? NewPassword { get; set; }

		[DataType(DataType.Password)]
		[Display(Name = "Confirm new password")]
		[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
		public string? ConfirmPassword { get; set; }
	}

}