@namespace OpenArchival.Blazor.ArchiveDisplay
@using Microsoft.AspNetCore.Components.Web
@using MudBlazor

<MudExpansionPanel Text="Filter...">
    <MudText Typo="Typo.caption">Choose which data the serach bar will search on:</MudText>
    <MudDivider></MudDivider>

    <MudRadioGroup 
        T="ArchiveSearchFilterType" 
        Value="SelectedFilter"
        ValueChanged="OnSelectedFilterChanged"
    > 
        <MudRadio Option="ArchiveSearchFilterType.All" T="ArchiveSearchFilterType">All</MudRadio>
        <MudRadio Option="ArchiveSearchFilterType.Tags" T="ArchiveSearchFilterType">Tags</MudRadio>
        <MudRadio Option="ArchiveSearchFilterType.Defects" T="ArchiveSearchFilterType">Defects</MudRadio>
        <MudRadio Option="ArchiveSearchFilterType.ListedNames" T="ArchiveSearchFilterType">Listed Names</MudRadio>
        <MudRadio Option="ArchiveSearchFilterType.Title" T="ArchiveSearchFilterType">Title</MudRadio>
        <MudRadio Option="ArchiveSearchFilterType.Description" T="ArchiveSearchFilterType">Description</MudRadio>
        <MudRadio Option="ArchiveSearchFilterType.Filenames" T="ArchiveSearchFilterType">Filenames</MudRadio>
        <MudRadio Option="ArchiveSearchFilterType.ArtifactTranscriptions" T="ArchiveSearchFilterType">Artifact Transcriptions</MudRadio>

    </MudRadioGroup>
    
    @if (SelectedTitlePrefixEnabled)
    {
        <MudText Typo="Typo.caption">Filter artifacts by the first letter of their title</MudText>
        <MudDivider/>
        <MudSelect 
        Text="Title First Letter Filter"
        T="string" 
        Value="SelectedTitlePrefix"
        ValueChanged="OnSelectedTitlePrefixChanged"
        Variant="Variant.Outlined">
            @foreach (var value in TitlePrefixOptions)
            {
                <MudSelectItem Value="@value">@value</MudSelectItem> 
            }
        </MudSelect>
        <MudButton OnClick="ApplyFiltersClicked">Apply Filters</MudButton>
    }
    </MudExpansionPanel>

@code {
    // Search parameters
    [Parameter]
    public ArchiveSearchFilterType SelectedFilter { get; set; } = ArchiveSearchFilterType.All;
    [Parameter]
    public EventCallback<ArchiveSearchFilterType> SelectedFilterChanged { get; set; }

    // Filter parameters
    [Parameter]
    public bool SelectedTitlePrefixEnabled { get; set; } = true;
    [Parameter]
    public string SelectedTitlePrefix { get; set; } = "";
    [Parameter]
    public EventCallback<string> SelectedTitlePrefixChanged { get; set; }

    [Parameter]
    public EventCallback<MouseEventArgs> ApplyFiltersClicked { get; set; }

    [Parameter]
    public IEnumerable<string> TitlePrefixOptions { get; set; } = 
        new List<string> {
            "",
            "a", 
            "b", 
            "c", 
            "d", 
            "e", 
            "f", 
            "g", 
            "h", 
            "i", 
            "j", 
            "k", 
            "l", 
            "m", 
            "n", 
            "o", 
            "p", 
            "q", 
            "r", 
            "s", 
            "t", 
            "u", 
            "v", 
            "w", 
            "x", 
            "y", 
            "z"  
        };

    private async Task OnSelectedTitlePrefixChanged(string value)
    {
        SelectedTitlePrefix = value;
        if (SelectedTitlePrefixChanged.HasDelegate)
        {
            await SelectedTitlePrefixChanged.InvokeAsync(value);
        }
    }

    private async Task OnSelectedFilterChanged(ArchiveSearchFilterType filter)
    {
        SelectedFilter = filter;
        if (SelectedFilterChanged.HasDelegate)
        {
            await SelectedFilterChanged.InvokeAsync(filter);
        }
    }    
}
