@namespace OpenArchival.Blazor.ArtifactGroupingDisplay
@page "/archive/{GroupingIdString}"
@using Microsoft.EntityFrameworkCore
@using OpenArchival.DataAccess;
@using OpenArchival.Blazor.FileViewer
@using MudBlazor


@if (_artifactGrouping is not null)
{
    <MudContainer MaxWidth="MaxWidth.Large">
        <MudPaper Class="pa-4 ma-2 rounded d-flex justify-center" Elevation="3">
            <MudText Typo="Typo.h3">@_artifactGrouping.Title</MudText>
        </MudPaper>

        <MudPaper Class="pa-4 ma-2 rounded d-flex justify-center" Elevation="3">
            @*<MudImage Class="d-flex justify-center" Src="https://dummyimage.com/600x400/000/fff"></MudImage>*@
            <FileViewerCarousel FilePathListings="_artifactGrouping.ChildFilePathListings" MaxHeight="500" ShowUnsupportedFiles=false></FileViewerCarousel>
        </MudPaper>
        
        <MudGrid Spacing="2">
            <MudItem xs="8">
                <MudPaper Style="height: 100%;" Class="pa-4 ma-2 rounded" Elevation="3">
                    <MudText Typo="Typo.h6">Description</MudText>
                    <MudDivider></MudDivider>
                    <MudText Typo="Typo.body2">@_artifactGrouping.Description</MudText>
                </MudPaper>
            </MudItem>
            <MudItem xs="4">
                <MudPaper Style="height: 100%;" Class="pa-4 ma-2 rounded" Elevation="3">
                    <MudText Typo="Typo.h6">Artifact Identifier</MudText>
                    <MudDivider></MudDivider>
                    <MudText Typo="Typo.caption">@_artifactGrouping.ArtifactGroupingIdentifier</MudText>

                    <MudText Typo="Typo.h6">Primary Artifact Category</MudText>
                    <MudDivider></MudDivider>
                    <MudText Typo="Typo.caption">@_artifactGrouping.Category.Name</MudText>

                    <MudText Typo="Typo.h6">Primary Artifact Type</MudText>
                    <MudDivider></MudDivider>
                    <MudText Typo="Typo.caption">@_artifactGrouping.Type.Name</MudText>

                </MudPaper>
            </MudItem>
        </MudGrid>

        <MudPaper Class="pa-4 mt-8 ma-2 rounded" Elevation="3">
            @foreach (ArtifactEntry child in _artifactGrouping.ChildArtifactEntries) {
                <ArchiveEntryDisplay ArtifactEntry="child"></ArchiveEntryDisplay>
            }
        </MudPaper>

    </MudContainer>
}
@inject IArtifactGroupingProvider GroupingProvider;
@inject NavigationManager NavigationManager;
@inject IDbContextFactory<ApplicationDbContext> ContextFactory; 
@code {
    [Parameter]
    public string GroupingIdString { get; set; }

    /// <summary>
    /// The converted grouping id from the URL
    /// </summary>
    private int _groupingId { get; set; }

    private ArtifactGrouping _artifactGrouping { get; set; } = default!;

    protected override async Task OnParametersSetAsync()
    {
        _groupingId = int.Parse(GroupingIdString);
        await using var context = await ContextFactory.CreateDbContextAsync();
        var grouping = await context.ArtifactGroupings
            .Include(g => g.Category)
            .Include(g => g.IdentifierFields)
            .Include(g => g.Type)
            .Include(g => g.ChildArtifactEntries)
                .ThenInclude(e => e.StorageLocation)
            .Include(g => g.ChildArtifactEntries)
                .ThenInclude(e => e.Type)
            .Include(g => g.ChildArtifactEntries)
                .ThenInclude(e => e.Files)
            .Include(g => g.ChildArtifactEntries)
                .ThenInclude(e => e.Tags)
            .Include(g => g.ChildArtifactEntries)
                .ThenInclude(e => e.ListedNames)
            .Include(g => g.ChildArtifactEntries)
                .ThenInclude(e => e.Defects)
            .Include(g => g.ViewCount)
        .FirstOrDefaultAsync(g => g.Id == _groupingId); 

        if (grouping is null)
        {
            NavigationManager.NavigateTo("/grouping-not-found"); 
        }

        _artifactGrouping = grouping!;

        if (_artifactGrouping.ViewCount is null)
        {
            var viewCount = new ArtifactGroupingViewCount() {Grouping=grouping, Views=1};
            _artifactGrouping.ViewCount = viewCount;
            context.Add(viewCount);
        } else
        {
            _artifactGrouping.ViewCount.Views++;
        }
        await context.SaveChangesAsync();

        StateHasChanged();

        await base.OnParametersSetAsync();
    }

}
