@using Sidekick.Common.Enums
@using Sidekick.Common.Settings
@using Sidekick.Modules.Settings.Localization

<FormSelect Value="@PreferredWiki"
            ValueChanged="(string? v) => PreferredChanged(v)"
            Label="@Resources.Wiki_Preferred"
            Options="Options"/>

@inject SettingsResources Resources
@inject ISettingsService SettingsService

@code {

    private string? PreferredWiki { get; set; }

    private List<SelectOption> Options { get; } = new()
    {
        new SelectOption()
        {
            Value = WikiSetting.PoeWiki.GetValueAttribute(),
            Label = "poewiki.net",
        },
        new SelectOption()
        {
            Value = WikiSetting.PoeDb.GetValueAttribute(),
            Label = "poedb.tw",
        },
    };

    protected override async Task OnInitializedAsync()
    {
        PreferredWiki = await SettingsService.GetString(SettingKeys.PreferredWiki);
        await base.OnInitializedAsync();
    }

    private async Task PreferredChanged(string? value)
    {
        PreferredWiki = value;
        await SettingsService.Set(SettingKeys.PreferredWiki, Enum.TryParse<WikiSetting>(value, out var enumValue) ? enumValue : WikiSetting.PoeWiki);
    }

}
