@inherits MainBase
@inject ILogger<QuickVaultUnlockSection> Logger
@using Microsoft.Extensions.Localization

<div class="p-4 mb-4 mx-4 bg-white border border-gray-200 rounded-lg shadow-sm dark:border-gray-700 sm:p-6 dark:bg-gray-800">
    <h3 class="mb-2 text-lg font-medium text-gray-900 dark:text-white">@Localizer["Title"]</h3>

    @if (IsLoading)
    {
        <LoadingIndicator />
    }
    else
    {
        if (IsWebAuthnEnabled)
        {
            <div class="mb-3 text-sm text-gray-600 dark:text-gray-400">@Localizer["EnabledDescription"]</div>
            <Button Color="danger" OnClick="DisableWebAuthn">@Localizer["DisableButton"]</Button>
        }
        else
        {
            <div class="mb-3 text-sm text-gray-600 dark:text-gray-400">@Localizer["DisabledDescription"]</div>

            <div class="mb-3 text-sm text-gray-600 dark:text-gray-400">@Localizer["ExperimentalWarning"]</div>

            <Button Color="success" OnClick="EnableWebAuthn">@Localizer["EnableButton"]</Button>
        }
    }
</div>

@code {
    private IStringLocalizer Localizer => LocalizerFactory.Create("Components.Main.Settings.Security.QuickVaultUnlockSection", "AliasVault.Client");

    /// <summary>
    /// Event callback that is invoked when the Two-Factor Authentication status changes.
    /// </summary>
    [Parameter]
    public EventCallback OnStatusChanged { get; set; }

    private bool IsLoading { get; set; } = true;

    private bool IsWebAuthnEnabled { get; set; }

    /// <summary>
    /// Loads the Two-Factor Authentication status from the server.
    /// </summary>
    /// <returns>A task representing the asynchronous operation.</returns>
    public async Task LoadData()
    {
        IsLoading = true;
        StateHasChanged();

        IsWebAuthnEnabled = await AuthService.IsWebAuthnEnabledAsync();

        IsLoading = false;
        StateHasChanged();
    }

    /// <summary>
    /// Enables WebAuthn.
    /// </summary>
    public async Task EnableWebAuthn()
    {
        try
        {
            var webauthCredentialDerivedKey = await JsInteropService.CreateWebAuthnCredentialDerivedKey(await GetUsernameAsync());
            await AuthService.SetWebAuthnEnabledAsync(true, webauthCredentialDerivedKey.CredentialId, webauthCredentialDerivedKey.Salt, webauthCredentialDerivedKey.DerivedKey);
            GlobalNotificationService.AddSuccessMessage(Localizer["SuccessEnabledMessage"], true);
        }
        catch (NotSupportedException)
        {
            GlobalNotificationService.AddErrorMessage(Localizer["WebAuthnNotSupportedError"], true);
            return;
        }
        catch (Exception ex)
        {
            GlobalNotificationService.AddErrorMessage(Localizer["EnableErrorMessage"], true);
            Logger.LogInformation(ex, "An error occurred while trying to enable WebAuthn.");
            return;
        }

        await LoadData();
    }

    /// <summary>
    /// Disables WebAuthn.
    /// </summary>
    public async Task DisableWebAuthn()
    {
        await AuthService.SetWebAuthnEnabledAsync(false);
        GlobalNotificationService.AddSuccessMessage(Localizer["SuccessDisabledMessage"], true);
        await LoadData();
    }
}
