<?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="/exemplar:exemplar">
    <xsl:apply-templates select="." mode="abstract" />
  </xsl:template>

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

  <!-- Monograph -->
  <xsl:template match="exemplar:exemplar" mode="monograph">
    <table class="monograph">
      <caption></caption>
      <thead></thead>
      <tbody>
        <xsl:apply-templates select="." mode="monograph-entry-type-status" />
        <xsl:apply-templates select="exemplar:features" mode="monograph-entry" />
        <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" />
      </tbody>
      <tfoot></tfoot>
    </table>
    <xsl:apply-templates select="reference:bibliography" mode="bibliography" />
  </xsl:template>

  <!-- Type status -->
  <xsl:template match="exemplar:exemplar" mode="monograph-entry-type-status">
    <tr>
      <td><div>Type Status</div><div></div></td>
      <td><xsl:apply-templates select="common:nomenclature" mode="type-status" /></td>
    </tr>
  </xsl:template>

  <!-- Type status -->
  <xsl:template match="exemplar:exemplar/common:nomenclature" mode="type-status">
    <xsl:variable name="doi" select="../@doi" />
    <xsl:text>This is </xsl:text>
    <xsl:if test="count(common:name-status-group/common:identifier[@nomenclatural-type-exemplar=$doi]) &gt; 0">
      <xsl:text>the type strain for </xsl:text>
      <xsl:apply-templates select="common:name-status-group[common:identifier/@nomenclatural-type-exemplar=$doi]" mode="type-status" />
      <xsl:if test="count(common:name-status-group/common:identifier[not(@nomenclatural-type-exemplar=$doi)]) &gt; 0">
        <xsl:text> and </xsl:text>
      </xsl:if>
    </xsl:if>
    <xsl:if test="count(common:name-status-group/common:identifier[not(@nomenclatural-type-exemplar=$doi)]) &gt; 0">
      <xsl:text>a non-type strain of </xsl:text>
      <xsl:apply-templates select="common:name-status-group[not(common:identifier/@nomenclatural-type-exemplar=$doi)]" mode="type-status" />
    </xsl:if>
    <xsl:text>.</xsl:text>
  </xsl:template>

  <!-- Type status -->
  <xsl:template match="common:name-status-group" mode="type-status">
    <xsl:choose>
      <xsl:when test="position()!=last() and position()!=1">
        <xsl:text>, </xsl:text>
      </xsl:when>
      <xsl:when test="position()=last() and position()!=1">
        <xsl:text> and </xsl:text>
      </xsl:when>
      <xsl:otherwise></xsl:otherwise>
    </xsl:choose>

    <xsl:apply-templates select="." mode="name-authority-link" />

  </xsl:template>

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

  <!-- Exemplar Citation -->
  <xsl:template match="exemplar:exemplar" 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="exemplar:exemplar" 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="exemplar:exemplar" 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>

  <!-- Strain Identifiers -->
  <xsl:template match="exemplar:identifiers" mode="monograph-entry">
    <xsl:apply-templates select="exemplar:identifier" mode="monograph-entry" />
  </xsl:template>

  <!-- Features -->
  <xsl:template match="exemplar:features" mode="monograph-entry">
    <xsl:apply-templates select="exemplar:feature" mode="monograph-entry" />
  </xsl:template>

  <!-- Feature -->
  <xsl:template match="exemplar:feature" mode="monograph-entry">
    <tr>
      <td><div>Feature</div><div><xsl:value-of select="@description" /></div></td>
      <td><xsl:value-of select="@id" /></td>
    </tr>
  </xsl:template>

  <xsl:template name="feature-row">
    <xsl:param name="code" />
    <xsl:param name="id" />
    <tr>
      <td><div>Feature</div><div><xsl:apply-templates select="$code" /></div></td>
      <td><xsl:value-of select="$id" /></td>
    </tr>
  </xsl:template>

  <xsl:template match="exemplar:feature[@code='10.1601.fr.GCAT']" mode="monograph-entry">
  </xsl:template>

  <xsl:template match="exemplar:feature[@code='10.1601.fr.gold']" mode="monograph-entry">
    <tr>
      <td class="GOLD"><div><xsl:value-of select="@description" /></div></td>
      <td>
<!--
        <canvas>
          <xsl:attribute name="id">
            <xsl:value-of select="@id" />
          </xsl:attribute>
        </canvas>
-->
        <a target="_blank">
          <xsl:attribute name="href"><xsl:text>https://gold.jgi.doe.gov/resolver?id=</xsl:text><xsl:value-of select="@id" /></xsl:attribute>
          <xsl:value-of select="@id" />
        </a>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="exemplar:feature[@code='10.1601.fr.gold']" mode="javascript-links">
<!--
    <script defer="defer">
      <xsl:attribute name="src">
        <xsl:text>../script/GOLD/</xsl:text><xsl:value-of select="@id" /><xsl:text>.js</xsl:text>
      </xsl:attribute>
    </script>
-->
  </xsl:template>

  <!-- Highlight the 16S features together. -->
  <xsl:template match="exemplar:feature[@code='10.1601.fr.16S']" mode="monograph-entry">
    <tr>
      <td><div><xsl:value-of select="@description" /></div></td>
      <td>
        <div class="16S rRNA sequence">
          <a target="_blank">
            <xsl:attribute name="href">
              <xsl:choose>
                <xsl:when test="@url"><xsl:value-of select="@url" /></xsl:when>
                <xsl:otherwise><xsl:text>https://www.ncbi.nlm.nih.gov/nuccore/</xsl:text><xsl:value-of select="@id" /></xsl:otherwise>
              </xsl:choose>
            </xsl:attribute>
            <xsl:value-of select="@id" />
          </a>
        </div>
      </td>
    </tr>
  </xsl:template>

  <!-- Keep all of the genomic data together. -->
  <xsl:template match="exemplar:feature[@code='10.1601.fr.chromosome']|exemplar:feature[@code='10.1601.fr.plasmid']|exemplar:feature[@code='10.1601.fr.phage']" mode="monograph-entry">
    <tr>
      <td><div><xsl:value-of select="@description" /></div></td>
      <td>
        <div class="genome sequence">
          <a target="_blank">
            <xsl:attribute name="href">
              <xsl:choose>
                <xsl:when test="@url"><xsl:value-of select="@url" /></xsl:when>
                <xsl:otherwise><xsl:text>https://www.ncbi.nlm.nih.gov/nuccore/</xsl:text><xsl:value-of select="@id" /></xsl:otherwise>
              </xsl:choose>
            </xsl:attribute>
            <xsl:value-of select="@id" />
          </a>
        </div>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="exemplar:feature[@code='10.1601.fr.assembly']" mode="monograph-entry">
    <tr>
      <td><div><xsl:value-of select="@description" /></div></td>
      <td>
        <div class="genome assembly">
          <a target="_blank">
            <xsl:attribute name="href">
              <xsl:choose>
                <xsl:when test="@url"><xsl:value-of select="@url" /></xsl:when>
                <xsl:otherwise><xsl:text>https://www.ncbi.nlm.nih.gov/assembly/</xsl:text><xsl:value-of select="@id" /></xsl:otherwise>
              </xsl:choose>
            </xsl:attribute>
            <xsl:value-of select="@id" />
          </a>
        </div>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="exemplar:feature[@code='10.1601.fr.project']" mode="monograph-entry">
    <tr>
      <td><div><xsl:value-of select="@description" /></div></td>
      <td>
        <div class="genome project">
          <a target="_blank">
            <xsl:attribute name="href">
              <xsl:choose>
                <xsl:when test="@url"><xsl:value-of select="@url" /></xsl:when>
                <xsl:otherwise><xsl:text>https://www.ncbi.nlm.nih.gov/genomeprj/</xsl:text><xsl:value-of select="@id" /></xsl:otherwise>
              </xsl:choose>
            </xsl:attribute>
            <xsl:value-of select="@id" />
          </a>
        </div>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="exemplar:identifier" mode="format">
    <tr>
      <td>
        <xsl:text>&#8482;</xsl:text>
        <xsl:call-template name="atcc-footnote" />
      </td>
    </tr>
  </xsl:template>

  <xsl:template name="atcc-footnote">
    <p class="footnote">ATCC is a registered trademark of the American Type Culture Collection. ATCC culture collection identifiers are also <a target="_blank" href="http://www.atcc.org/About/WhoWeAre/tabid/139/Default.aspx#Trademarks">trademarks</a> of the American Type Culture Collection.</p>
  </xsl:template>

  <xsl:template match="exemplar:exemplar" 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="exemplar:exemplar" mode="abstract-title">
    <xsl:text>Exemplar Abstract for </xsl:text>
    <xsl:apply-templates select="exemplar:identifiers" mode="strain-equivalencies" />
    <xsl:text>.</xsl:text>
  </xsl:template>

</xsl:stylesheet>
