use v6.e.PREVIEW;
unit module Render::Post;

use Render::Util;
use DB::Post;
use DB::Series;

use HTML::Functional;

sub post-date(Post:D $post) is export {
    my $datetime = $post.posted-at;
    my $timestamp = sprintf(
        "%s %02d:%02d%s",
        $datetime.yyyy-mm-dd,
        ($datetime.hour % 12) || 12,
        $datetime.minute,
        $datetime.hour < 12 ?? 'am' !! 'pm'
    );

    div :class<post-time>, :title("Posted At $timestamp"), [
        icon 'calendar';
        '&nbsp;';
        $datetime.Date.Str
    ]
}

sub post-edit(Post:D $post) is export {
    return [] unless $post.edited-at.elems;
    my $datetime = $post.edited-at.max;
    my $timestamp = sprintf(
        "%s %02d:%02d%s",
        $datetime.yyyy-mm-dd,
        ($datetime.hour % 12) || 12,
        $datetime.minute,
        $datetime.hour < 12 ?? 'am' !! 'pm'
    );

    div :class<post-edit>, :title("Last Edited At $timestamp"), [
        icon 'edit';
        '&nbsp;';
        $datetime.Date.Str
    ]
}

sub post-read-time(Post:D $post) is export {
    my ($slow, $average, $fast) = $post.readtimes;
    div :class<post-read-time>, :title<Estimated read time at 140/180/220 WPM>, [
        icon 'timer';
        '&nbsp;';
        mins-to-string $slow;
        '&nbsp;';
        '/';
        '&nbsp;';
        mins-to-string $average;
        '&nbsp;';
        '/';
        '&nbsp;';
        mins-to-string $fast;
    ]
}

sub post-tag(Str:D $tag) is export {
    span :class<post-tag>, [
        a :href("/tags/$tag.html"), [
            icon 'hash';
            span $tag;
        ]
    ]
}

sub post-tags(Post:D $post) is export {
    my @tags = $post.tags.sort;
    if @tags {
       @tags.=map(*.&post-tag);
       div :class<post-tags>, [
           icon 'purchase-tag-alt';
           '&nbsp;';
           intersperse(', ', @tags);
       ]
    } else {
        []
    }
}

sub series-tag(Int:D $post-id, Int:D $series-id, Series:D $series) is export {
    span :class<post-series-tag>, [
       a :href("/series/$series-id.html"), [
          icon 'book';
          '&nbsp;';
          span :class<post-series-tag-inner>, [
              $series.title;
              '&nbsp;';
              '(';
              ($series.post-ids.first($post-id, :k) + 1).Str;
              '/';
              $series.post-ids.elems.Str;
              ')';
          ]
       ]
    ]
}

sub post-series-tags(Int:D $post-id, Post:D $post, $db) is export {
    # Find all the series this post is in
    my @series = $db.series.grep(*.value.contains-post: $post-id);
    if @series {
       div :class<post-series-tags>,
           @series.map(-> $pair {
               series-tag $post-id, $pair.key, $pair.value
           });
    } else {
        []
    }
}

sub post-info(Int:D $id, Post:D $post, $db) is export {
    div :class<post-info>, [
        post-date $post;
        post-edit $post;
        post-read-time $post;
        post-tags $post;
        post-series-tags $id, $post, $db;
    ];
}

sub post-header(Int:D $id, Post:D $post, $db) is export {
    header :class<post-header>, [
        div :class<post-title>, [
            h1 $post.title;
        ];
        post-info $id, $post, $db;
    ]
}