@using System.Net;
@using System.Diagnostics;
@using Hangfire;
@using LANCommander.Jobs.Background;
@using Microsoft.EntityFrameworkCore;
@inject HttpClient HttpClient
@inject NavigationManager Navigator
@inject ArchiveService ArchiveService
@inject IMessageService MessageService
@inject IJSRuntime JS

<Space Direction="DirectionVHType.Vertical" Style="width: 100%">
    <SpaceItem>
        <Table TItem="Archive" DataSource="@Archives" HidePagination="true" Responsive>
            <PropertyColumn Property="a => a.Version" />
            <PropertyColumn Property="a => a.CompressedSize">
                @ByteSizeLib.ByteSize.FromBytes(context.CompressedSize)
            </PropertyColumn>
            <PropertyColumn Property="a => a.CreatedBy">
                @context.CreatedBy?.UserName
            </PropertyColumn>
            <PropertyColumn Property="a => a.CreatedOn" Format="MM/dd/yyyy hh:mm tt" DefaultSortOrder="@SortDirection.Descending" />
            <ActionColumn Title="">
                <Space Style="display: flex; justify-content: end">
                    <SpaceItem>
                        <a href="/Download/Archive/@context.Id" target="_blank" class="ant-btn ant-btn-text ant-btn-icon-only">
                            <Icon Type="@IconType.Outline.Download" />
                        </a>
                    </SpaceItem>
                    <SpaceItem>
                        <Popconfirm Title="Are you sure you want to delete this archive?" OnConfirm="() => Delete(context)">
                            <Button Icon="@IconType.Outline.Close" Type="@ButtonType.Text" Danger />
                        </Popconfirm>
                    </SpaceItem>
                </Space>
            </ActionColumn>
        </Table>
    </SpaceItem>

    <SpaceItem>
        <GridRow Justify="end">
            <GridCol>
                <Button OnClick="UploadArchive" Type="@ButtonType.Primary">Upload Archive</Button>
            </GridCol>
        </GridRow>
    </SpaceItem>
</Space>

<ArchiveUploader @ref="Uploader" GameId="GameId" RedistributableId="RedistributableId" OnArchiveUploaded="LoadData" />

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

    ICollection<Archive> Archives { get; set; }

    ArchiveUploader Uploader;

    protected override async Task OnInitializedAsync()
    {
        await LoadData();

        HttpClient.BaseAddress = new Uri(Navigator.BaseUri);
    }

    private async Task LoadData()
    {
        if (GameId != Guid.Empty)
            Archives = await ArchiveService.Get(a => a.GameId == GameId).ToListAsync();
        else if (RedistributableId != Guid.Empty)
            Archives = await ArchiveService.Get(a => a.RedistributableId == RedistributableId).ToListAsync();
    }

    private async Task Download(Archive archive)
    {
        string url = $"/Download/Game/{archive.Id}";

        await JS.InvokeAsync<object>("open", url, "_blank");
    }

    private async Task UploadArchive()
    {
        await Uploader.Open();
    }

    private async Task Delete(Archive archive)
    {
        try
        {
            await ArchiveService.Delete(archive);

            await LoadData();

            await MessageService.Success("Archive deleted!");
        }
        catch (Exception ex)
        {
            await MessageService.Error("Archive could not be deleted.");
        }
    }
}
