@namespace OpenArchival.Blazor.ArtifactGroupingDisplay
@using Microsoft.JSInterop
@using OpenArchival.Blazor.FileViewer
@using OpenArchival.Blazor.CustomComponents
@using MudBlazor
@using OpenArchival.DataAccess

<MudPaper Class="pa-4 ma-2 rounded" Elevation="3">
    <MudGrid Spacing="2">
        <MudItem lg="8" xs="12" Class="d-flex align-center justify-center flex-column">
            <MudText Class="d-flex" Typo=Typo.h6>@ArtifactEntry.Title</MudText>
            <FileViewerCarousel FilePathListings="ArtifactEntry.Files" MaxHeight="350" ShowUnsupportedFiles=true></FileViewerCarousel>
        </MudItem>

        <MudItem lg="4" xs="12">
            <MudText Typo="Typo.h6">Artifact Identifier</MudText>
            <MudDivider></MudDivider>
            <MudText Typo="Typo.caption">@ArtifactEntry.ArtifactIdentifier</MudText>

            <MudText Typo="Typo.h6">Primary Artifact Type</MudText>
            <MudDivider></MudDivider>
            <MudText Typo="Typo.caption">@ArtifactEntry.Type.Name</MudText>
            
            @if (!string.IsNullOrEmpty(ArtifactEntry.StorageLocation.Location))
            {
                <MudText Typo="Typo.h6">Storage Location</MudText>
                <MudDivider></MudDivider>
                <MudText Typo="Typo.caption">@ArtifactEntry.StorageLocation.Location</MudText>
            }

            @if (ArtifactEntry.Tags.Count > 0)
            {
                <MudText Typo="Typo.h6">Tags</MudText>
                <MudDivider></MudDivider>
                <ChipContainer DeleteEnabled=false @bind-Items="ArtifactEntry.Tags"></ChipContainer>
            }
            
            @if (ArtifactEntry.ListedNames.Count > 0)
            {
                <MudText Typo="Typo.h6">Listed Names</MudText>
                <MudDivider></MudDivider>
                <ChipContainer DeleteEnabled=false @bind-Items="ArtifactEntry.ListedNames"></ChipContainer>
            }
            
            @if (ArtifactEntry.AssociatedDates.Count > 0)
            {
                <MudText Typo="Typo.h6">Associated Dates</MudText>
                <MudDivider></MudDivider>
                <ChipContainer DeleteEnabled=false @bind-Items="ArtifactEntry.AssociatedDates" DisplayFunc="@(date => date.ToString("d"))"></ChipContainer>
            }
        </MudItem>
    </MudGrid>

    <MudText Class="mt-4" Typo="Typo.h6">Description</MudText>
    <MudDivider></MudDivider>
    <MudText Typo="Typo.body1">@ArtifactEntry.Description</MudText>
    
    <MudExpansionPanel Text="Downloads">
        <MudList T="string">
            @foreach (FilePathListing file in ArtifactEntry.Files)
            {
                <MudListItem Icon="@Icons.Material.Filled.Download" IconColor="Color.Primary" OnClick="() => OnFileDownloadClicked(file)">@file.OriginalName</MudListItem>
            }
        </MudList>
    </MudExpansionPanel>
</MudPaper>

@inject IJSRuntime JSRuntime
@inject ISnackbar Snackbar
@code {
    [Parameter]
    public required ArtifactEntry ArtifactEntry { get; set; }

    private async Task OnFileDownloadClicked(FilePathListing file)
    {
        try
        {
            byte[] fileBytes = await File.ReadAllBytesAsync(file.Path);

            string mimeType = "";

            await JSRuntime.InvokeVoidAsync("downloadFileFromBytes", file.OriginalName, mimeType, Convert.ToBase64String(fileBytes));
        } 
        catch (Exception ex)
        {
            Snackbar.Add($"Failed to download file {file.OriginalName}", Severity.Error);
            throw ex;
        }
    }
}
