@page "/Settings/Tools/MissingArchives"
@using LANCommander.Models;
@using LANCommander.Pages.Games.Components;
@layout SettingsLayout
@inject IMessageService MessageService
@inject ArchiveService ArchiveService
@attribute [Authorize(Roles = "Administrator")]

<PageHeader Title="Missing Archives" />

<div style="padding: 0 24px;">
    <p>
        These archives are missing their backing file in the "Upload" directory of your server. This may result in broken downloads for clients. To fix, either upload a new file or delete the offending archive.
    </p>

    <Table TItem="Archive" DataSource="@Archives" Loading="@Loading" Responsive>
        <PropertyColumn Property="a => a.Version" Sortable />
        <PropertyColumn Property="a => a.Game.Title" />
        <PropertyColumn Property="a => a.CreatedOn" Format="MM/dd/yyyy hh:mm tt" Sortable />
        <PropertyColumn Property="a => a.CreatedBy" Sortable>
            @context.CreatedBy?.UserName
        </PropertyColumn>
        <ActionColumn Title="" Style="text-align: right">
            <Space Direction="DirectionVHType.Horizontal">
                <SpaceItem>
                    <Button OnClick="() => Upload(context)" Icon="@IconType.Outline.Upload" Type="@ButtonType.Text" />
                </SpaceItem>
                <SpaceItem>
                    <Popconfirm OnConfirm="() => Delete(context)" Title="Are you sure you want to delete this archive?">
                        <Button Icon="@IconType.Outline.Close" Type="@ButtonType.Text" Danger />
                    </Popconfirm>
                </SpaceItem>
            </Space>
        </ActionColumn>
    </Table>

    <ArchiveUploader @ref="Uploader" OnArchiveUploaded="LoadData" />
</div>


 @code {
    ICollection<Archive> Archives;
    bool Loading = true;

    ArchiveUploader Uploader;

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

    async Task LoadData()
    {
        Loading = true;

        Archives = new List<Archive>();

        foreach (var archive in await ArchiveService.Get())
        {
            var archivePath = ArchiveService.GetArchiveFileLocation(archive);
            var exists = await ArchiveService.Exists(archive.Id);

            if (!exists)
                Archives.Add(archive);
            else if (new FileInfo(archivePath).Length == 0)
                Archives.Add(archive);
        }

        Loading = false;
    }

    async Task Upload(Archive archive)
    {
        var archiveFilePath = ArchiveService.GetArchiveFileLocation(archive);

        if (await ArchiveService.Exists(archive.Id))
            File.Delete(archiveFilePath);

        System.IO.File.Create(archiveFilePath).Close();

        await Uploader.Open(archive.Id);
    }

    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.");
        }
    }
}
