@page "/{Slug}"
@using Microsoft.AspNetCore.Components.Sections

@if (Post != null)
{
    <OpenGraph UrlPath="@Post.Slug" Title="@Post.Title" Description="@Post.Description"
               ImagePath="@($"post-assets/{Post.Slug}/{Post.HeaderFilename}")" ImageAlt="@Post.HeaderAlt" LargeImage="true"/>

    <SectionContent SectionName="Header">
        @if (Post.HeaderFilename != null)
        {
            <figure class="header">
                <img src="@Assets[$"post-assets/{Post.Slug}/{Post.HeaderFilename}"]" alt="@Post.HeaderAlt"
                     class="header-img">
                @if (Post.HeaderCaption != null)
                {
                    <figcaption>@Post.HeaderCaption</figcaption>
                }
            </figure>
        }
        <div id="main-content" class="container">
            <h1>@Post.Title</h1>
            <p>@Post.Description</p>
            @if (Post.Edited != null)
            {
                var edited = Post.Edited ?? DateTime.Now; // this null check should never happen
                <small>
                    Posted:
                    <RenderDateTime Datetime="@Post.Published"/>
                    &bull;
                    Latest edit:
                    <RenderDateTime Datetime="@edited"/>
                </small>
            }
            else
            {
                <small>Posted:
                    <RenderDateTime Datetime="@Post.Published"/>
                </small>
            }
            <hr>
        </div>
    </SectionContent>
}
else
{
    <PageTitle>Not found</PageTitle>

    <main id="main-content" class="container">
        <h1>Not found</h1>
        <p>Page not found</p>
        <a href="/">Go home</a>
    </main>

    return;
}

@if (Post?.Content != null)
{
    <article class="container">
        @((MarkupString)Post.Content)
    </article>
}

@if (Post?.Footer != null)
{
    <SectionContent SectionName="Footer">
        @switch (Post.Footer)
        {
            case BlogPosts.PostFooter.CcBy:
                <p>
                    <a href="https://pancakes.guy/@Post.Slug">@Post.Title</a> © @Post.Published.Year by
                    <a href="https://pancakes.gay">pancakes</a> is licensed under
                    <a href="https://creativecommons.org/licenses/by/4.0/" target="_blank">CC BY 4.0</a>
                    <img src="https://mirrors.creativecommons.org/presskit/icons/cc.svg" alt="CC" class="inline-icon">
                    <img src="https://mirrors.creativecommons.org/presskit/icons/by.svg" alt="BY" class="inline-icon">
                </p>
                break;
            default:
                break;
        }
    </SectionContent>
}

@code {
    [Parameter] public required string Slug { get; set; }

    public BlogPosts.Post? Post { get; set; }

    protected override void OnInitialized()
    {
        if (string.IsNullOrWhiteSpace(Slug))
        {
            return;
        }

        Post = BlogPosts.Posts.FirstOrDefault(p => p.Slug == Slug);
    }

}