'use strict'
const Output = require('./output.js')
const Streampub = require('streampub')
const chapterFilename = require('./chapter-filename.js')
const fs = require('fs')
const html = require('./html-template-tag.js')
const promisify = require('./promisify')
const pump = promisify(require('pump'))
const filenameize = require('./filenameize.js')

class OutputEpub extends Output {
  from (fic) {
    return super.from(fic).to(filenameize(this.fic.title) + '.epub')
  }
  write () {
    const epub = new Streampub({
      id: this.fic.id,
      title: this.fic.title,
      author: this.fic.author,
      authorUrl: this.fic.authorUrl,
      description: this.fic.description,
      source: this.fic.link,
      subject: this.fic.tags && this.fic.tags.length && this.fic.tags.join(','),
      publisher: this.fic.publisher,
      published: this.fic.started || this.fic.created,
      modified: this.fic.modified,
      numberTOC: this.fic.numberTOC
    })

    epub.write(Streampub.newChapter('Title Page', this.titlePageHTML(), 0, 'top.xhtml'))
    if (this.fic.includeTOC) {
      epub.write(Streampub.newChapter('Table of Contents', this.tableOfContentsHTML(), 1, 'toc.xhtml'))
    }
    const output = fs.createWriteStream(this.outname)
    return pump(
      this.fic,
      this.transform(),
      epub,
      output).then(() => this.outname)
  }

  transformChapter (chapter) {
    if (chapter.image) {
      return Streampub.newFile(chapter.filename, chapter.content)
    }
    if (chapter.cover) {
      return Streampub.newCoverImage(chapter.content)
    }
    const index = chapter.order != null && (1 + chapter.order)
    const name = chapter.name
    const filename = chapterFilename(chapter)
    const toSanitize = '<html xmlns:epub="http://www.idpf.org/2007/ops">' +
      (name ? html`<title>${name}</title></head>` : '') +
      '<section epub:type="chapter">' + chapter.content + '</section>' +
      '</html>'
    return Streampub.newChapter(name, this.sanitizeHtml(toSanitize), 100 + index, filename)
  }

  html (content) {
    return `<html xmlns:epub="http://www.idpf.org/2007/ops">${content}</html>`
  }

  htmlBody (body) {
    return `<body epub:type="cover titlepage">${body}</body>`
  }

  htmlTitle () {
    return html`<section epub:type="title"><h1 style="text-align: center;">${this.fic.title}</h1></section>`
  }

  htmlByline () {
    if (!this.fic.author) return ''
    return `<h3 style="text-align: center;">by <span epub:type="credits">${this.htmlAuthor(this.fic.author, this.fic.authorUrl)}</span></h3>`
  }

  htmlCoverImage () {
    if (!this.coverName) return ''
    return `<p><img style="display: block; margin-left: auto; margin-right: auto;" src="images/cover.jpg"></p>`
  }

  htmlSummaryRow (key, value) {
    if (key === 'Tags') {
      return super.htmlSummaryRow(key, `<section epub:type="keywords">${value}`)
    } else {
      return super.htmlSummaryRow(key, value)
    }
  }

  htmlDescription () {
    if (!this.fic.description) return ''
    return `<section epub:type="abstract">${super.htmlDescription()}</section>`
  }
}

OutputEpub.aliases = []
module.exports = OutputEpub

@@ -12,22 +12,18 @@
 '%0Aconst 
-Output
+fs
  = requi
@@ -30,18 +30,9 @@
 re('
-./output.j
+f
 s')%0A
@@ -70,16 +70,90 @@
 ampub')%0A
+const TOML = require('@iarna/toml')%0Aconst Output = require('./output.js')%0A
 const ch
@@ -205,33 +205,8 @@
 s')%0A
-const fs = require('fs')%0A
 cons
@@ -293,48 +293,8 @@
 y')%0A
-const pump = promisify(require('pump'))%0A
 cons
@@ -338,16 +338,57 @@
 e.js')%0A%0A
+const pump = promisify(require('pump'))%0A%0A
 class Ou
@@ -989,16 +989,170 @@
     %7D)%0A%0A
+    epub.write(%7B%0A      id: 'fic',%0A      content: Buffer.from(TOML.stringify(this.fic)),%0A      fileName: 'meta.fic.toml',%0A      mime: 'text/x-toml'%0A    %7D)%0A
     epub

Include fic metadata in epubs