import * as RSSFeed from 'feed'
import {Awaitable, collect, Collection, Inject, Maybe, Singleton} from '@extollo/lib'
import {AbstractBlog, BlogBackend, BlogPost, BlogPostFrontMatter, isBlogPostFrontMatter} from './AbstractBlog.service'
import {MarkMarkService} from '../MarkMark.service'
import {MarkMarkRenderer} from '../../../markmark/markmark.renderer'

@Singleton()
export class MarkMarkBlog extends AbstractBlog<BlogPostFrontMatter> {
    @Inject()
    protected readonly mark!: MarkMarkService

    protected getBackend(): BlogBackend<BlogPostFrontMatter> {
        return {
            routePrefix: '/links',
            resourcePath: [],
            author: {
                name: 'Garrett Mills',
                email: 'shout@garrettmills.dev',
                link: 'https://garrettmills.dev/#about',
            },
        }
    }

    protected isValidFrontMatter(what: unknown): what is BlogPostFrontMatter {
        return isBlogPostFrontMatter(what)
    }

    async getAllPosts(): Promise<Collection<BlogPost>> {
        const mm = await this.mark.getLinks()

        const posts: Collection<BlogPost> = collect()

        for ( const section of mm.sections ) {
            for ( const link of section.links ) {
                if ( !link.date ) {
                    continue
                }

                posts.push({
                    title: link.title,
                    slug: link.hash,
                    date: link.date,
                    tags: link.tags,
                    file: link.hash,  // not used
                    markdown: (new MarkMarkRenderer()).render({
                        ...mm,
                        sections: [{
                            ...section,
                            links: [link],
                        }],
                    }, false),
                })
            }
        }

        return posts.sort((a, b) => b.date!.getTime() - a.date!.getTime())
    }

    protected createFeed(lastUpdated: Maybe<Date>): Awaitable<RSSFeed.Feed> {
        return new RSSFeed.Feed({
            title: 'Garrett\'s Bookmarks',
            description: 'Links to articles, projects, and sites I found interesting',
            id: this.routing.getNamedPath('links').toRemote,
            link: this.routing.getNamedPath('links').toRemote,
            language: 'en',
            image: this.routing.getAssetPath('favicon', 'apple-touch-icon.png').toRemote,
            favicon: this.routing.getAssetPath('favicon', 'favicon.ico').toRemote,
            copyright: `Copyright (c) ${(new Date).getFullYear()} Garrett Mills. See website for licensing details.`,
            updated: lastUpdated,
            generator: '',
            feedLinks: {
                json: this.routing.getNamedPath('links:json').toRemote,
                atom: this.routing.getNamedPath('links:atom').toRemote,
                rss: this.routing.getNamedPath('links:rss').toRemote,
            },
            author: {
                name: 'Garrett Mills',
                email: 'shout@garrettmills.dev',
                link: 'https://garrettmills.dev/#about',
            },
        })
    }

    getUrl(post: BlogPost): string {
        const appUrl = this.routing.getAppUrl().toRemote
        let prefix = this.getBackend().routePrefix
        if ( !prefix.startsWith('/') ) prefix = `/${prefix}`
        return `${appUrl}${prefix}/#link-${post.slug}`
    }
}
