<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  version="1.0"
  xmlns:name="http://namesforlife.com/ns/name"
  xmlns:taxon="http://namesforlife.com/ns/taxon"
  xmlns:exemplar="http://namesforlife.com/ns/exemplar"
  xmlns:reference="http://namesforlife.com/ns/reference"
  xmlns:common="http://namesforlife.com/ns/common"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  exclude-result-prefixes="name taxon exemplar reference common"
  >

  <xsl:include href="common.xsl" />

  <xsl:output
    method="html"
    standalone="yes"
    encoding="iso-8859-1"
    omit-xml-declaration="yes"
    cdata-section-elements="pre script style"
    indent="yes"
    media-type="text/html"
  />

  <!-- Main Entry Point -->
  <xsl:template match="/taxon:taxon">
    <xsl:apply-templates select="." mode="abstract" />
  </xsl:template>

  <!-- Abstract -->
  <xsl:template match="taxon:taxon" mode="abstract">
    <xsl:apply-templates select="." mode="monograph" />
  </xsl:template>

  <!-- Monograph -->
  <xsl:template match="taxon:taxon" mode="monograph">
    <table class="monograph">
      <caption></caption>
      <thead></thead>
      <tbody>
        <xsl:apply-templates select="." mode="table-row-type-status" />
        <xsl:apply-templates select="." mode="table-row-taxonomy" />
        <xsl:if test="@rank='species' or @rank='subspecies'">
          <xsl:apply-templates select="exemplar:exemplars" mode="table-row-non-type-exemplars" />
        </xsl:if>
        <xsl:call-template name="table-row-empty" />
        <xsl:apply-templates select="." mode="table-row-citation" />
        <xsl:apply-templates select="." mode="table-row-sourcefile" />
      </tbody>
      <tfoot></tfoot>
    </table>
    <table class="footnotes">
      <caption></caption>
      <thead></thead>
      <tbody>
        <xsl:apply-templates select="common:annotation/common:public-note" mode="footnote" />
        <xsl:apply-templates select="common:annotation/common:modified" mode="footnote" />
<!--        <xsl:call-template name="support-footnote" /> -->
      </tbody>
      <tfoot></tfoot>
    </table>
    <xsl:apply-templates select="reference:bibliography" mode="bibliography" />
  </xsl:template>

  <!-- Type Status -->
  <xsl:template match="taxon:taxon" mode="table-row-type-status">
    <tr>
      <td>
        <div>Type Status</div>
      </td>
      <td>
        <xsl:apply-templates select="." mode="type-status" />
      </td>
    </tr>
  </xsl:template>

  <!-- Type Status (content) -->
  <xsl:template match="taxon:taxon" mode="type-status">
<!-- TODO: Add nomenclatural-type-of element to taxon XML.
    <xsl:text>This </xsl:text>
    <xsl:value-of select="@rank" />
    <xsl:text> is</xsl:text>
    <xsl:if test="@is-type='false'"> not</xsl:if>
    <xsl:text> the nomenclatural type for its parent</xsl:text>
    <xsl:if test="taxon:parent">
      <xsl:text> </xsl:text><xsl:value-of select="taxon:parent/@rank" />
    </xsl:if>
    <xsl:text>.</xsl:text>
-->
    <xsl:choose>
      <xsl:when test="@rank='subspecies' or @rank='species' or @rank='subgenus' or @rank='genus' or @rank='subfamily' or @rank='family' or @rank='suborder' or @rank='order' or @rank='subclass' or @rank='class'">
      </xsl:when>
      <xsl:otherwise>
        <xsl:text> Names of taxa above the rank of class or below the rank of subspecies are not covered by the International Code of Nomenclature of Prokaryotes and therefore cannot serve as nomenclatural types.</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Taxonomy -->
  <xsl:template match="taxon:taxon" mode="table-row-taxonomy">
    <xsl:if test="@rank!='domain'">
      <tr>
        <td>
          <div>Taxonomy</div>
        </td>
        <td>
          <div><xsl:text>The taxonomic placement of this </xsl:text><xsl:apply-templates select="@rank" /><xsl:text> within the </xsl:text><xsl:apply-templates select="taxon:ancestor/@rank" /><xsl:text> </xsl:text><xsl:apply-templates select="taxon:ancestor" mode="proper" /><xsl:text>.</xsl:text></div>
          <p>
            <table>
              <thead>
                <tr>
                  <td colspan="2"></td>
                </tr>
              </thead>
              <tbody>
                <xsl:apply-templates select="taxon:ancestor" mode="table-row" />
              </tbody>
              <tfoot>
                <tr>
                  <td colspan="2"></td>
                </tr>
              </tfoot>
            </table>
          </p>
          <div><xsl:text>The taxonomy from the rank of class and below is based upon currently published taxonomic opinion. For a complete taxonomy, refer to The Taxonomic Outline of Bacteria and Archaea, Release 7.7</xsl:text><xsl:apply-templates select="/taxon:taxon/reference:bibliography/reference:reference[@id='rid.11718']" mode="citation" /><xsl:text>.</xsl:text></div>
        </td>
      </tr>
    </xsl:if>
  </xsl:template>

  <!-- Support -->
  <xsl:template match="taxon:taxon" mode="table-row-support">
    <tr>
      <td>
        <div>Support</div>
      </td>
      <td><xsl:call-template name="support" /></td>
    </tr>
  </xsl:template>

  <!-- Citation -->
  <xsl:template match="taxon:taxon" mode="table-row-citation">
    <tr>
      <td>
        <div>Citation</div>
      </td>
      <td><xsl:apply-templates select="." mode="citation" /></td>
    </tr>
  </xsl:template>

  <!-- Non-Type Exemplars -->
  <xsl:template match="exemplar:exemplars" mode="table-row-non-type-exemplars">
    <xsl:if test="exemplar:exemplar[not(@doi=../../common:nomenclatural-type/@nomenclatural-type-exemplar)]">
      <tr>
        <td>
          <div>Non-Type Exemplars</div>
        </td>
        <td>
          <div>
            <table>
              <thead>
                <tr style="border-bottom: 1px solid darkslategrey">
                  <td style="text-align: center">Digital Object Identifier</td>
                  <td style="text-align: center">Strain Equivalence</td>
                </tr>
              </thead>
              <tbody>
                <xsl:apply-templates select="exemplar:exemplar[not(@doi=../../common:nomenclatural-type/@nomenclatural-type-exemplar)]" mode="table-row" />
              </tbody>
              <tfoot>
                <tr>
                  <td colspan="2"></td>
                </tr>
              </tfoot>
            </table>
          </div>
        </td>
      </tr>
    </xsl:if>
  </xsl:template>

  <!-- Microbial Earth -->
  <xsl:template match="taxon:taxon" mode="microbial-earth">
    <canvas id="microbialEarth"></canvas>
    <script>
      <xsl:attribute name="src">
        <xsl:text>https://api.namesforlife.com/ME/</xsl:text>
        <xsl:apply-templates select="taxon:ancestor" mode="microbial-earth-taxon" />
      </xsl:attribute>
    </script>
  </xsl:template>

  <!-- Taxon Citation -->
  <xsl:template match="taxon:taxon" mode="citation">
    <div>When referring to this Abstract, please use its Digital Object Identifier.</div>
    <div class="citation"><xsl:apply-templates select="." mode="abstract-citation" /></div>
  </xsl:template>

  <!-- Source File -->
  <xsl:template match="taxon:taxon" mode="table-row-sourcefile">
    <tr>
      <td><div>Source File</div></td>
      <td><xsl:apply-templates select="." mode="sourcefile" /></td>
    </tr>
  </xsl:template>

  <!-- Source File -->
  <xsl:template match="taxon:taxon" mode="sourcefile">
    <div>
      <a>
        <xsl:attribute name="href">
          <xsl:value-of select="substring(@doi, 9)" />
          <xsl:text>.xml</xsl:text>
        </xsl:attribute>
        <xsl:value-of select="@doi" />
        <xsl:text>.xml</xsl:text>
      </a>
    </div>
  </xsl:template>

  <!-- Generates rows for the taxonomy table. -->
  <xsl:template match="taxon:ancestor" mode="table-row">
    <tr>
      <td>
        <xsl:attribute name="class"><xsl:text>rank </xsl:text><xsl:value-of select="@rank" /></xsl:attribute>
        <xsl:apply-templates select="." mode="taxon-rank-link" />
        <text>&#160;</text>
        <xsl:apply-templates select="." mode="taxon-name-link" />
      </td>
    </tr>
    <xsl:apply-templates select="taxon:ancestor" mode="table-row" />
  </xsl:template>

  <!-- Generates rows for the non-type Exemplar table. -->
  <xsl:template match="exemplar:exemplar" mode="table-row">
    <tr style="border-bottom: 1px solid darkslategrey">
      <td>
        <xsl:apply-templates select="." mode="exemplar-doi-link" />
      </td>
      <td>
        <xsl:apply-templates select="exemplar:identifiers" mode="strain-equivalencies" />
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="common:annotation" mode="monograph-entry-updates">
    <tr>
      <td>
        <div>Updates</div>
      </td>
      <td>
        <p><xsl:apply-templates select="common:created" mode="full" /></p>
        <p><xsl:apply-templates select="common:modified" mode="full" /></p>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="common:annotation" mode="monograph-entry-notes">
    <tr>
      <td>
        <div>Notes</div>
      </td>
      <td>
        <p><xsl:value-of select="common:public-note" /></p>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="taxon:taxon" mode="abstract-citation">
    <xsl:apply-templates select="." mode="abstract-title" />
    <xsl:text> Retrieved </xsl:text>
    <time itemprop="dateModified">
      <xsl:attribute name="datetime">
        <xsl:value-of select="common:annotation/common:snapshot/@date" /><xsl:text>T</xsl:text><xsl:value-of select="common:annotation/common:snapshot/@time" /><xsl:text>Z</xsl:text>
      </xsl:attribute>
      <xsl:value-of select="common:annotation/common:snapshot/@date-formatted" />
    </time>
    <xsl:text>. </xsl:text>
    <a>
      <xsl:attribute name="href">
        <xsl:text>https://doi.org/</xsl:text>
        <xsl:value-of select="@doi" />
      </xsl:attribute>
      <xsl:text>https://doi.org/</xsl:text>
      <xsl:value-of select="@doi" />
    </a>
    <xsl:text>.</xsl:text>
  </xsl:template>

  <!-- Abstract Title -->
  <xsl:template match="taxon:taxon" mode="abstract-title">
    <xsl:text>Taxon Abstract for the </xsl:text>
    <xsl:value-of select="@rank" />
    <xsl:text> </xsl:text>
    <xsl:apply-templates select="common:nomenclature" mode="names-and-authorities" />
    <xsl:text>.</xsl:text>
  </xsl:template>

</xsl:stylesheet>
