@page "/Games/{id:guid}/Keys"
@inject GameService GameService
@inject KeyService KeyService

<MudTable Items="@Keys" Hover="true" Elevation="0">
    <ToolBarContent>
        <MudText Typo="Typo.h6">Keys</MudText>
        <MudSpacer />
        <MudButton Color="Color.Primary" Variant="Variant.Filled" Href="@($"/Games/{Id}/Keys/Edit")">Edit</MudButton>
    </ToolBarContent>

    <HeaderContent>
        <MudTh>Key</MudTh>
        <MudTh>Allocation Method</MudTh>
        <MudTh>Claimed By</MudTh>
        <MudTh>Claimed On</MudTh>
        <MudTh></MudTh>
    </HeaderContent>

    <RowTemplate>
        <MudTd>@context.Value</MudTd>
        <MudTd>@context.AllocationMethod</MudTd>
        <MudTd>
            @switch (context.AllocationMethod)
            {
                case KeyAllocationMethod.MacAddress:
                    <text>@context.ClaimedByMacAddress</text>
                    break;

                case KeyAllocationMethod.UserAccount:
                    <text>@context.ClaimedByUser?.UserName</text>
                    break;
            }
        </MudTd>
        <MudTd>@context.ClaimedOn</MudTd>
        <MudTd>
            @if ((context.AllocationMethod == KeyAllocationMethod.MacAddress && !String.IsNullOrWhiteSpace(context.ClaimedByMacAddress)) || (context.AllocationMethod == KeyAllocationMethod.UserAccount && context.ClaimedByUser != null))
            {
                <MudButton OnClick="() => Release(context)">Release</MudButton>
            }
        </MudTd>
    </RowTemplate>
</MudTable>

@code {
    [Parameter] public Guid Id { get; set; }

    private ICollection<Key> Keys { get; set; }

    protected override async Task OnInitializedAsync()
    {
        var game = await GameService.Get(Id);

        Keys = game.Keys;
    }

    private async Task Release(Key key)
    {
        key = await KeyService.Release(key);
    }
}
