@inject DbService DbService
@inject IStringLocalizerFactory LocalizerFactory
@using Microsoft.Extensions.Localization

<div class="mb-4">
    <label for="password-generator-settings-modal" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">@Localizer["PasswordGeneratorSettingsLabel"]</label>
    <button type="button" id="password-generator-settings-modal" class="px-4 py-2 bg-primary-600 text-white rounded-md hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-primary-500 dark:bg-primary-700 dark:hover:bg-primary-600" @onclick="OpenSettings">
        @Localizer["ConfigureButton"]
    </button>
    <span class="block text-sm font-normal text-gray-500 truncate dark:text-gray-400 mt-2">
        @Localizer["PasswordGeneratorSettingsDescription"]
    </span>
</div>

@if (IsSettingsVisible)
{
    <PasswordSettingsPopup
        PasswordSettings="@PasswordSettings"
        IsTemporary="false"
        OnSaveSettings="@HandlePasswordSettingsSaved"
        OnClose="@CloseSettings" />
}

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

    private PasswordSettings PasswordSettings { get; set; } = new();
    private bool IsSettingsVisible { get; set; }

    /// <inheritdoc />
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        PasswordSettings = DbService.Settings.PasswordSettings;
    }

    /// <summary>
    /// Opens the password settings popup.
    /// </summary>
    private void OpenSettings()
    {
        IsSettingsVisible = true;
    }

    /// <summary>
    /// Closes the password settings popup.
    /// </summary>
    private void CloseSettings()
    {
        IsSettingsVisible = false;
    }

    /// <summary>
    /// Handles the password settings saved event.
    /// </summary>
    private void HandlePasswordSettingsSaved((PasswordSettings settings, string generatedPassword) args)
    {
        // The settings are already saved in the PasswordSettingsPopup component
        // We just need to update our local state
        PasswordSettings = args.settings;
    }
}
