import {appPath, Controller, Inject, Injectable, plaintext, view} from '@extollo/lib'
import {Home} from './Home.controller'
import * as marked from 'marked'
import {MarkMarkService} from '../../services/MarkMark.service'

@Injectable()
export class MarkMark extends Controller {
    @Inject()
    protected readonly markmark!: MarkMarkService

    public welcome() {
        const home = <Home> this.make(Home)
        return view('markmark:welcome', {
            ...home.getThemeCSS(),
            title: 'MarkMark (v1.0)',
        })
    }

    private cachedStandard?: string

    public async standard() {
        if ( !this.cachedStandard ) {
            const path = appPath('resources', 'markmark', 'standard.md')
            const content = await path.read()
            this.cachedStandard = marked.marked(content)
        }

        const home = <Home> this.make(Home)
        return view('markmark:standard', {
            ...home.getThemeCSS(),
            title: 'Spec: MarkMark (v1.0)',
            contentHtml: this.cachedStandard,
        })
    }

    public async linksHtml() {
        const home = <Home> this.make(Home)
        return view('links', {
            ...home.getThemeCSS(),
            title: 'Bookmarks',
            contentHtml: await this.markmark.getLinksHTML(),
        })
    }

    public async linksMarkMark() {
        return plaintext(await this.markmark.getLinksMM())
            .contentType('text/markdown;variant=markmark')
    }

}
