text: 'use strict'
  const Output = use('output')
  
  class OutputHTML extends Output {
    from (fic) {
      const filenameize = use('filenameize')
      return super.from(fic).to(filenameize(this.fic.title) + '.html')
    }
  
    chapterExt () {
      return '.html'
    }
  
    async write () {
      const mkdirp = use('mkdirp')
      const fun = require('funstream')
      await mkdirp(this.outname)
      await fun(this.fic).pipe(this.transform())
      await this.writeTitle()
      await this.writeIndex()
      return this.outname
    }
  
    transformChapter (chapter) {
      const chaptername = this.chapterFilename(chapter)
      const path = require('path')
      const filename = chaptername && path.join(this.outname, this.chapterFilename(chapter))
      const fs = use('fs-promises')
      if (chapter.outputType === 'image') {
        return fs.writeFile(filename, chapter.content)
      } else if (chapter.outputType === 'cover') {
        const stream = require('stream')
        if (chapter.content instanceof stream.Stream) {
          const tmpname = path.join(this.outname, 'cover-tmp')
          return new Promise((resolve, reject) => {
            const identifyStream = require('buffer-signature').identifyStream
            const WriteStreamAtomic = require('fs-write-stream-atomic')
            chapter.content.pipe(identifyStream(info => {
              const ext = info.extensions.length ? '.' + info.extensions[0] : ''
              this.coverName = 'cover' + ext
            })).pipe(new WriteStreamAtomic(tmpname)).on('error', reject).on('finish', () => {
              resolve(fs.rename(tmpname, path.join(this.outname, this.coverName)))
            })
          })
        } else {
          const identifyBuffer = require('buffer-signature').identify
          const info = identifyBuffer(chapter.content)
          const ext = info.extensions.length ? '.' + info.extensions[0] : ''
          this.coverName = 'cover' + ext
          return fs.writeFile(path.join(this.outname, this.coverName), chapter.content)
        }
      } else {
        const content = this.prepareHtml(chapter.content)
        return fs.writeFile(filename, content)
      }
    }
  
    writeTitle () {
      const path = require('path')
      const fs = use('fs-promises')
      return fs.writeFile(path.join(this.outname, 'title.html'), this.titlePageHTML())
    }
  
    writeIndex () {
      const path = require('path')
      const fs = use('fs-promises')
      return fs.writeFile(path.join(this.outname, 'index.html'), this.tableOfContentsHTML())
    }
  
    htmlCoverImage () {
      if (!this.coverName) return ''
      return `<p><img style="display: block; margin-left: auto; margin-right: auto;" src="${this.coverName}"></p>\n`
    }
  
    tableOfContentsContent () {
      return this.htmlTitle() +
        this.htmlByline() +
        this.htmlCoverImage() +
        this.htmlDescription() +
        this.htmlSummaryTable(this.htmlSummaryContent()) +
        this.htmlChapterList(this.htmlChapters())
    }
  }
  
  OutputHTML.aliases = ['HTML', 'html']
  module.exports = OutputHTML
  