use v6.e.PREVIEW;

use HTML::Functional;

use Render::Util;
use Render::Head;
use Render::Post;
use DB::BlogMeta;
use DB::Post;

unit class Config;

method generate-post(Int:D $id, Post:D $post, $db) {
    my $meta = $db.meta;
    my $content = $post.render-html;
    my $cactus-script = qq「
document.addEventListener('DOMContentLoaded', () => \{
initComments(\{
  node: document.getElementById("comment-section"),
  defaultHomeserverUrl: "https://matrix.cactus.chat",
  serverName: "cactus.chat",
  siteName: "stranger.systems",
  commentSectionId: "post-$id"
\})\})」;
    my $head =
        head [
            # Generate the universal header components
            generate-head($meta, $post.title, $post.description);
            # Open graph tags for embedding
            meta :property<og:title>, :content($post.title);
            meta :property<og:url>, :content(post-link-abs $db.meta, $id, $post);
            meta :property<og:site_name>, :content($db.meta.title);
            optl $post.description ~~ Str:D,
                -> {meta :property<og:description>, :content(^ $post.description)};
            meta :property<og:type>, :content<article>;
            meta :property<article:published_time>, :content($post.posted-at);
            $post.tags.map(-> $tag {
                                  meta :property<article:tag>, :content($tag)
                              });

            # Cactus comments support
            link :rel<stylesheet>,
                :href</resources/cactus.css>,
                :type<text/css>;
            script :src<https://gateway.pinata.cloud/ipfs/QmSiWN27KZZ1XE32jKwifBnS3nWTUcFGNArKzur2nmDgoL/v0.13.0/cactus.js>;
            # Only actually load the script if the post isn't hidden
            optl !$post.hidden, -> {script $cactus-script};
        ];
    my $body =
    body [
       site-header $meta;
       article :class<post>, [
          post-header $id, $post, $db;
          div :class<post-body>, [
              $content;
          ];
       ];
       # Only actually have the comment section if the post isn't hidden
       optl !$post.hidden, -> {div :id<comment-section>, :class<comments>};
    ];
    # TODO: Setup Comments
    # TODO: Setup footer
    # my $footer;

   my $html = html :lang<en>, [
       $head,
       $body
   ];

   show-html $html
}

method generate-index($db) {
    my @most-recent =
    $db.sorted-posts
       .head(10)
       .grep(!*.value.hidden)
       .map(-> $pair {
            generate-blurb $pair.key, $db
        });

    my $head = head [generate-head($db.meta);];
    my $body = body [
        site-header $db.meta;
        div :class<post-blurbs>, [
           h1 "Recent Posts"
        ], @most-recent;
    ];

    my $html =
        html :lang<en>, [
            $head,
            $body
        ];

    show-html $html
}

method generate-archive($db) {
    my @most-recent =
    $db.sorted-posts
       .grep(!*.value.hidden)
       .map(-> $pair {
            generate-blurb $pair.key, $db
        });

    my $head = head [generate-head($db.meta);];
    my $body = body [
        site-header $db.meta;
        div :class<post-blurbs>, [
           h1 "All Posts"
        ], @most-recent;
    ];

    my $html =
        html :lang<en>, [
            $head,
            $body
        ];

    show-html $html
}

method generate-tag-blurb($db, $tag, $limit?) {
    sub post-to-link($id, $post) {
        my $desc = $post.description;
        my $link = post-link $id, $post;
        div :class<tag-blurb-post>, [
            div :class<tag-blurb-post-title>, [
                a :href($link), span [
                    h3 $post.title;
                ];
                post-info $id, $post, $db;
                if $desc ~~ Str:D {
                    div :class<tag-blurb-post-description>, [
                        p $post.description;
                    ];
                } else {
                    []
                }
            ];
        ]
    }
    my @posts = $db.sorted-posts.grep(-> $a { $tag (elem) $a.value.tags });
    if $limit {
        @posts.=head($limit);
    }
    if @posts {
        div :class<tag-blurb>, [
            span :class<tag-blurb-title>, [
                a :href("/tags/$tag.html"), [
                    icon 'hash';
                    $tag;
                ];
            ];
            div :class<tag-blurb-links>,
                @posts.map(-> $a {post-to-link $a.key, $a.value});
        ]
    } else {
        []
    }
}

method generate-tags-page($db, @tags) {
    my $head = head [generate-head($db.meta);];
    my $body = body [
        site-header $db.meta;
        div :class<tags>, [
            h1 "Tags";
        ], @tags.map(-> $tag {self.generate-tag-blurb($db, $tag, 4)});
    ];

    my $html =
        html :lang<en>, [
            $head,
            $body
        ];

    show-html $html
}

method generate-tag-page($db, $tag) {
    my $head = head [generate-head($db.meta);];
    my $body = body [
        site-header $db.meta;
        self.generate-tag-blurb($db, $tag, 4);
    ];

    my $html =
        html :lang<en>, [
            $head,
            $body
        ];

    show-html $html
}