@page "/Games"
@inject GameService GameService

<MudTable Items="@Games.Where(g => String.IsNullOrEmpty(Search) || g.Title.ToLower().Contains(Search.ToLower().Trim()))" RowsPerPage="25" Hover="true">
    <ToolBarContent>
        <MudText Typo="Typo.h6">Games</MudText>
        <MudSpacer />
        <MudTextField @bind-Value="Search" Placeholder="Search" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" Class="mt-0"></MudTextField>
    </ToolBarContent>

    <HeaderContent>
        <MudTh></MudTh>
        <MudTh>Title</MudTh>
        <MudTh>Sort Title</MudTh>
        <MudTh>Released</MudTh>
        <MudTh>Created</MudTh>
        <MudTh>Created By</MudTh>
        <MudTh>Updated</MudTh>
        <MudTh>Updated By</MudTh>
    </HeaderContent>

    <RowTemplate>
        <MudTd>
            <MudImage Src="@GetIcon(context)" Height="32" Width="32" />
        </MudTd>
        <MudTd DataLabel="Title">@context.Title</MudTd>
        <MudTd DataLabel="Sort Title">@context.SortTitle</MudTd>
        <MudTd DataLabel="Released">@context.ReleasedOn?.ToString("MM/dd/yyyy")</MudTd>
        <MudTd DataLabel="Created">@context.CreatedOn</MudTd>
        <MudTd DataLabel="Created By">@context.CreatedBy?.UserName</MudTd>
        <MudTd DataLabel="Updated">@context.UpdatedOn</MudTd>
        <MudTd DataLabel="Updated By">@context.UpdatedBy?.UserName</MudTd>
        <MudTd Class="d-flex justify-end">
            <MudButton Href="@($"/Games/{context.Id}/Edit")">Edit</MudButton>
        </MudTd>
    </RowTemplate>

    <PagerContent>
        <MudTablePager />
    </PagerContent>
</MudTable>

@code {
    private ICollection<Game> Games { get; set; }
    private string Search { get; set; }

    protected override async Task OnInitializedAsync()
    {
        Games = GameService.Get().OrderBy(g => String.IsNullOrWhiteSpace(g.SortTitle) ? g.Title : g.SortTitle).ToList();
    }

    private string GetIcon(Game game)
    {
        return $"/api/Games/{game.Id}/Icon.png";
    }
}
