@page "/settings/general"
@inherits MainBase

<LayoutPageTitle>General settings</LayoutPageTitle>

<PageHeader
    BreadcrumbItems="@BreadcrumbItems"
    Title="General settings"
    Description="Configure general AliasVault settings.">
</PageHeader>

<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-4 text-lg font-medium text-gray-900 dark:text-white">Email Settings</h3>

    <div class="mb-4">
        <label for="defaultEmailDomain" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Default Email Domain</label>
        <select @bind="DefaultEmailDomain" @bind:after="UpdateDefaultEmailDomain" id="defaultEmailDomain" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
            @if (ShowPrivateDomains)
            {
                <optgroup label="Private Domains">
                    @foreach (var domain in PrivateDomains)
                    {
                        <option value="@domain">@domain</option>
                    }
                </optgroup>
            }
            <optgroup label="Public Domains">
                @foreach (var domain in PublicDomains)
                {
                    <option value="@domain">@domain</option>
                }
            </optgroup>
        </select>
        <span class="block text-sm font-normal text-gray-500 truncate dark:text-gray-400">
            Set the default email domain that will be used when creating new credentials.
        </span>
    </div>

    <div class="flex items-center mb-4">
        <input @bind="AutoEmailRefresh"  @bind:after="UpdateAutoEmailRefresh" id="autoEmailRefresh" type="checkbox" class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600">
        <label for="autoEmailRefresh" class="ml-2 text-sm font-medium text-gray-900 dark:text-gray-300">Auto email refresh on credential page</label>
    </div>
</div>

<div class="p-4  mx-4 mb-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-4 text-lg font-medium text-gray-900 dark:text-white">Alias Settings</h3>

    <div class="mb-4">
        <label for="defaultEmailDomain" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Alias generation language</label>
        <select @bind="DefaultIdentityLanguage" @bind:after="UpdateDefaultIdentityLanguage" id="defaultIdentityLanguage" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
            <option value="en">English</option>
            <option value="nl">Dutch</option>
        </select>
        <span class="block text-sm font-normal text-gray-500 truncate dark:text-gray-400">
            Set the default language that will be used when generating new identities.
        </span>
    </div>
</div>

@code {
    private List<string> PrivateDomains => Config.PrivateEmailDomains;
    private List<string> PublicDomains => Config.PublicEmailDomains;
    private bool ShowPrivateDomains => PrivateDomains.Count > 0 && !(PrivateDomains.Count == 1 && PrivateDomains[0] == "DISABLED.TLD");

    private string DefaultEmailDomain { get; set; } = string.Empty;
    private bool AutoEmailRefresh { get; set; }
    private string DefaultIdentityLanguage { get; set; } = string.Empty;

    /// <inheritdoc />
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        BreadcrumbItems.Add(new BreadcrumbItem { DisplayName = "General settings" });

        DefaultEmailDomain = DbService.Settings.DefaultEmailDomain;
        if (DefaultEmailDomain == string.Empty)
        {
            if (PrivateDomains.Count > 0)
            {
                DefaultEmailDomain = PrivateDomains[0];
            }
            else if (PublicDomains.Count > 0)
            {
                DefaultEmailDomain = PublicDomains[0];
            }
        }
        AutoEmailRefresh = DbService.Settings.AutoEmailRefresh;
        DefaultIdentityLanguage = DbService.Settings.DefaultIdentityLanguage;
    }

    /// <summary>
    /// Updates the default email domain.
    /// </summary>
    private async Task UpdateDefaultEmailDomain()
    {
        await DbService.Settings.SetDefaultEmailDomain(DefaultEmailDomain);
        StateHasChanged();
    }

    /// <summary>
    /// Updates the auto email refresh setting.
    /// </summary>
    private async Task UpdateAutoEmailRefresh()
    {
        await DbService.Settings.SetAutoEmailRefresh(AutoEmailRefresh);
        StateHasChanged();
    }

    /// <summary>
    /// Updates the auto email refresh setting.
    /// </summary>
    private async Task UpdateDefaultIdentityLanguage()
    {
        await DbService.Settings.SetDefaultIdentityLanguage(DefaultIdentityLanguage);
        StateHasChanged();
    }
}
