import {Controller, view, Injectable, Collection, Inject, Routing, plaintext} from '@extollo/lib'
import {FeedPost} from '../../models/FeedPost.model'
import * as RSSFeed from 'feed'
import {Home} from './Home.controller'

/**
 * Feed Controller
 * ------------------------------------
 * Backend for routes related to my post feed.
 */
@Injectable()
export class Feed extends Controller {
    @Inject()
    protected readonly routing!: Routing

    public async feed(feedPosts: Collection<FeedPost>) {
        const home = <Home> this.make(Home)
        return view('feed', {
            feedPosts: feedPosts.toArray(),
            ...home.getThemeCSS(),
        })
    }

    public async rss(feedPosts: Collection<FeedPost>) {
        const feed = this.getFeed(feedPosts)
        return plaintext(feed.rss2()).contentType('application/rss+xml; charset=UTF-8')
    }

    public async atom(feedPosts: Collection<FeedPost>) {
        const feed = this.getFeed(feedPosts)
        return plaintext(feed.atom1()).contentType('application/atom+xml; charset=UTF-8')
    }

    public async json(feedPosts: Collection<FeedPost>) {
        const feed = this.getFeed(feedPosts)
        return plaintext(feed.json1()).contentType('application/feed+json; charset=UTF-8')
    }

    protected getFeed(feedPosts: Collection<FeedPost>): RSSFeed.Feed {
        const feed = new RSSFeed.Feed({
            title: 'Garrett Mills',
            description: 'A sporadic collection of my thoughts, blog posts, and project updates',
            id: `${this.routing.getAppUrl()}#about`,
            link: this.routing.getNamedPath('feed').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: feedPosts.whereMax('postedAt').first()?.postedAt,
            generator: '',
            feedLinks: {
                json: this.routing.getNamedPath('feed.json').toRemote,
                atom: this.routing.getNamedPath('feed.atom').toRemote,
                rss: this.routing.getNamedPath('feed.rss').toRemote,
            },
            author: {
                name: 'Garrett Mills',
                email: 'shout@garrettmills.dev',
                link: 'https://garrettmills.dev/#about',
            },
        })

        feed.addCategory('Technology')
        feed.addCategory('Software Development')

        feedPosts.each(post => {
            feed.addItem({
                title: post.tag,  // FIXME better title generation
                date: post.postedAt,
                id: `${this.routing.getNamedPath('feed')}#${post.feedPostId}`,
                link: `${this.routing.getNamedPath('feed')}#${post.feedPostId}`,
                content: post.body,
                author: [{
                    name: 'Garrett Mills',
                    email: 'shout@garrettmills.dev',
                    link: 'https://garrettmills.dev/#about',
                }],
            })
        })

        return feed
    }
}
