@page "/tags"
@inject VoiceWorksClient Client
@inject IJSRuntime JS
@using JSMR.Application.Common.Search
@using JSMR.Application.Tags.Queries.Search
@using JSMR.Application.Tags.Queries.Search.Contracts
@using JSMR.UI.Blazor.Components
@using JSMR.UI.Blazor.Services

<PageTitle>Tags</PageTitle>

<h1>Tags</h1>

<MudTextField @bind-Value="Keywords" Immediate="true" DebounceInterval="500" Label="Filter" Variant="MudBlazor.Variant.Text" Adornment="@Adornment.Start" AdornmentIcon="@Icons.Material.Outlined.Search" />

@if (searchResults is null)
{
    <p>Loading…</p>
}
else if (searchResults.Items.Length == 0)
{
    <p>No results.</p>
}
else
{
    <MudDataGrid Items="@searchResults.Items" Style="table-layout: fixed">
        <Columns>
            <PropertyColumn Property="x => x.Name" Title="Name" />
            <PropertyColumn Property="x => x.EnglishName" Title="English Name" />
            <PropertyColumn Property="x => x.VoiceWorkCount" Title="Voice Works" HeaderStyle="width: 14em" />
            <TemplateColumn Title="Favorite" HeaderStyle="width: 8em">
                <CellTemplate>
                        <MudIconButton Size="@Size.Small" Icon="@Icons.Material.Filled.Favorite" Color="@(context.Item.Favorite ? Color.Secondary : Color.Dark)" @onclick="@(e => IncrementCount(context.Item))" />
                </CellTemplate>
            </TemplateColumn>
            <TemplateColumn Title="Blacklisted" HeaderStyle="width: 8em">
                <CellTemplate>
                    <MudIconButton Size="@Size.Small" Icon="@Icons.Material.Filled.Block" Color="@(context.Item.Blacklisted? Color.Secondary: Color.Dark)" />
                </CellTemplate>
            </TemplateColumn>
        </Columns>
    </MudDataGrid>

    <JPagination @bind-PageNumber="PageNumber" @bind-PageSize="PageSize" @bind-TotalItems="searchResults.TotalItems" />
}

<style>
    .mud-table-root {
        table-layout: fixed;
    }

    .j-pager {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        justify-content: center;
        z-index: 2;
        background: var(--mud-palette-background);
        padding: .5em 1em;
    }
</style>

@code {
    private string? keywords;

    public string? Keywords
    {
        get { return keywords; }
        set
        {
            keywords = value;
            _ = UpdateDataAsync(true);
        }
    }

    private int pageNumber = 1;

    public int PageNumber
    {
        get { return pageNumber; }
        set
        {
            pageNumber = value;
            _ = UpdateDataAsync(false);
        }
    }

    int pageSize = 100;
    
    public int PageSize
    {
        get { return pageSize; }
        set
        {
            pageSize = value;
            _ = UpdateDataAsync(true);
        }
    }

    SearchResult<TagSearchItem>? searchResults;

    protected override Task OnInitializedAsync()
    {
        _ = LoadTagsAsync();

        return Task.CompletedTask;
    }

    private async Task UpdateDataAsync(bool resetPageNumber)
    {
        if (resetPageNumber)
            pageNumber = 1;

        await LoadTagsAsync();
    }

    private async Task LoadTagsAsync()
    {
        SearchTagsRequest request = new(
            Options: new()
            {
                PageNumber = PageNumber,
                PageSize = pageSize,
                Criteria = new()
                {
                    Name = Keywords
                },
                SortOptions =
                [
                    new(TagSortField.Name, Application.Common.Search.SortDirection.Ascending)
                ]
            }
        );

        await JS.InvokeVoidAsync("pageHelpers.scrollToTop");
        var result = await Client.SearchAsync(request);

        searchResults = result?.Results ?? new();

        await InvokeAsync(StateHasChanged);
    }

    private void IncrementCount(TagSearchItem item)
    {
        //item.Favorite = !item.Favorite;
    }
}