@using FastReport.Web.Application.Localizations
@inherits ExportSettingsBase

<label>@localization.PageRange</label>
<div class="fr-webreport-popup-content-export-parameters-row">
    <button type="button" class="fr-webreport-popup-content-export-parameters-button @IsActive(p => p == 0)"
            @onclick="OnAllClick">
        @localization.All
    </button>

    <button type="button" class="fr-webreport-popup-content-export-parameters-button @IsActive(p => p == 1)"
            @onclick="OnFirstClick">
        @localization.First
    </button>

    <input type="text"
           class="fr-webreport-popup-content-export-parameters-input @IsActive(p => p > 1)"
           pattern="[0-9]"
           placeholder="@Localize("PagesPlaceholder")"
           @bind-value="PagesCount">
</div>

@code {

    PageSelectorLocalization localization;

    protected override void LocalizeForm()
    {
        var res = WebReport.Res;
        localization = new PageSelectorLocalization(res);
    }

    public string PagesCount
    {
        get => GetPagesCount();
        set => SetPagesCount(value);
    }

    protected override void OnInitialized()
    {
        base.OnInitialized();
    }

    protected string IsActive(Predicate<int?> isActive)
    {
        int currentPagesCount = 0;
        if (ExportParameters.TryGetValue("PagesCount", out string oldVal))
        {
            if (!int.TryParse(oldVal, out currentPagesCount))
            {
            }
        }

        if (isActive(currentPagesCount))
            return "active";

        return string.Empty;
    }

    void OnAllClick(MouseEventArgs e)
    {
        ExportParameters.Remove("PagesCount");
    }

    void OnFirstClick(MouseEventArgs e)
    {
        ExportParameters["PagesCount"] = "1";
    }

    protected string GetPagesCount()
    {
        if (ExportParameters.TryGetValue("PagesCount", out string value))
        {
            if (!string.IsNullOrWhiteSpace(value))
            {
                return value;
            }
            else
                return null;
        }
        else
            return null;
    }

    protected void SetPagesCount(string newPageNumbers)
    {
        ExportParameters["PagesCount"] = newPageNumbers;
    }
}
