@page "/Games/{id:guid}/Saves"
@using LANCommander.Models;
@using System.IO.Compression;
@using Microsoft.EntityFrameworkCore;
@attribute [Authorize(Roles = "Administrator")]
@inject GameService GameService
@inject CompanyService CompanyService
@inject GenreService GenreService
@inject TagService TagService
@inject ArchiveService ArchiveService
@inject ScriptService ScriptService
@inject IMessageService MessageService
@inject NavigationManager NavigationManager
@inject ModalService ModalService
@inject GameSaveService GameSaveService

<Table TItem="GameSave" DataSource="@GameSaves" Loading="@Loading" Responsive>
    <PropertyColumn Property="g => g.User" Sortable>
        @context.User?.UserName
    </PropertyColumn>
    <PropertyColumn Property="g => g.CreatedOn" Format="MM/dd/yyyy hh:mm tt" Sortable />
    <PropertyColumn Property="g => g.CreatedBy" Sortable>
        @context.CreatedBy?.UserName
    </PropertyColumn>
    <ActionColumn Title="">
        <Space Direction="DirectionVHType.Horizontal">
            <SpaceItem>
                <Popconfirm OnConfirm="() => Delete(context)" Title="Are you sure you want to delete this game save?">
                    <Button Icon="@IconType.Outline.Close" Type="@ButtonType.Text" Danger />
                </Popconfirm>
            </SpaceItem>
        </Space>
    </ActionColumn>
</Table>

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

    ICollection<GameSave> GameSaves { get; set; } = new List<GameSave>();

    bool Loading = true;

    protected override async Task OnInitializedAsync()
    {
        GameSaves = await GameSaveService.Get(gs => gs.GameId == Id).OrderBy(gs => gs.User.UserName).ThenByDescending(gs => gs.CreatedOn).ToListAsync();

        Loading = false;
    }

    private async Task Delete(GameSave gameSave)
    {
        GameSaves = new List<GameSave>();

        Loading = true;

        await GameSaveService.Delete(gameSave);

        GameSaves = await GameSaveService.Get(gs => gs.GameId == Id).OrderBy(gs => gs.User.UserName).ThenByDescending(gs => gs.CreatedOn).ToListAsync();

        Loading = false;
    }
}
