<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" exclude-result-prefixes="i" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 xmlns:i="http://docs.oasis-open.org/ws-tx/issues/Issues.xsd" 
 xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://www.w3.org/1999/xhtml"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://docs.oasis-open.org/ws-tx/issues/Issues.xsd Issues.xsd"
>
  <xsl:strip-space elements="*"/>
  <xsl:output 
	 method="xml" 
	 encoding="utf-8" 
	 indent="yes" 
	 doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" 
	 doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" 
	 media-type="text/html" 
	 omit-xml-declaration="no"
	 />

  <xsl:template match="i:issues">
    <html>
      <head>
        <title>
          <xsl:value-of select="i:head/i:title"></xsl:value-of>
        </title>
        <style type="text/css">
          body {color: black; background: white; font-family: sans-serif; }
          <xsl:for-each select="i:head/i:states/i:state">
            <xsl:if test="@color">
              .<xsl:value-of select="."/> { background-color: <xsl:value-of select="@color"/>; }
            </xsl:if>
          </xsl:for-each>
          dt { color: #666; font-weight: bold; }
          dd { white-space: normal; }
          .meta { background-color: #eee; }
          .date { font-weight: lighter; font-size: 0.8em; padding-left: 2em; }
          .marker { font-weight: bold; }
          .cvs:first-letter { color: transparent; }
          dl { padding: 0pt 24pt 12pt 0pt; }
          td, th { vertical-align: top; margin: 0px; padding: 3px; }
          th, th a { background-color: #666; color: #fff; text-align: left; }
          td.title { background-color: #fec; border-top: 1px solid black; page-break-after: avoid; }
          .summary td, .stats td { border-color: #eee; border-style: solid; border-width: 0 0 2px 0; }
          .stats td, .stats th { text-align: right; }
          @media print {
          a.ext:after {
          <!--content: " <xsl:text disable-output-escaping="yes">&lt;" attr(href) "&gt; </xsl:text>";-->
          font-size: 75%;
          font-weight: lighter;
          }
          .nav { display: none; }
          }

        </style>
      </head>
      <body>
        <h1>
          <xsl:value-of select="i:head/i:title"></xsl:value-of>
        </h1>
        <p class="cvs">
          <xsl:value-of select="i:head/i:date"></xsl:value-of>
        </p>
        <p class="cvs">
          <xsl:value-of select="i:head/i:revision"></xsl:value-of>
        </p>

        <h2>
          <a name="Stats" accesskey="S" tabindex="0">
            Statistics
          </a>
        </h2>
        <xsl:apply-templates select="/i:issues" mode="stats"/>
        <xsl:for-each select="i:head/i:states/i:state">
          <xsl:call-template name="summary">
            <xsl:with-param name="type">
              <xsl:value-of select="."/>
            </xsl:with-param>
          </xsl:call-template>
        </xsl:for-each>
        <!--<xsl:call-template name="summary">
					<xsl:with-param name="type">Open</xsl:with-param>
				</xsl:call-template>
				<xsl:call-template name="summary">
					<xsl:with-param name="type">Pending</xsl:with-param>
				</xsl:call-template>
				<xsl:call-template name="summary">
					<xsl:with-param name="type">Done</xsl:with-param>
				</xsl:call-template>
				<xsl:call-template name="summary">
					<xsl:with-param name="type">Resolved</xsl:with-param>
				</xsl:call-template>
				<xsl:call-template name="summary">
					<xsl:with-param name="type">Deferred</xsl:with-param>
				</xsl:call-template>
				<xsl:call-template name="summary">
					<xsl:with-param name="type">Closed</xsl:with-param>
				</xsl:call-template>-->
        <h2>
          <a name="Detail" accesskey="D" tabindex="2">
            Detailed Listing
          </a>
        </h2>
        <table cellspacing="0" width="95%">
          <xsl:apply-templates select="i:issue" mode="full">
          </xsl:apply-templates>
        </table>
      </body>
    </html>
  </xsl:template>
  <!-- Stats -->
  <xsl:template match="/i:issues" mode="stats">
    <table cellspacing="0" class="stats">
      <tbody>
        <tr>
          <th>
          </th>
          <xsl:for-each select="//i:head/i:targets/i:target">
            <th>
              <xsl:value-of select="."></xsl:value-of>
            </th>
          </xsl:for-each>
          <th>total</th>
        </tr>
        <xsl:for-each select="//i:head/i:states/i:state">
          <xsl:variable name="state">
            <xsl:value-of select="."/>
          </xsl:variable>
          <tr>
            <th>
              <xsl:value-of select="$state"/>
            </th>
            <xsl:for-each select="//i:head/i:targets/i:target">
              <xsl:variable name="target">
                <xsl:value-of select="."/>
              </xsl:variable>
              <td>
                <xsl:value-of select="count(//i:issue[@status=$state][i:target=$target])"></xsl:value-of>
              </td>
            </xsl:for-each>
            <td>
              <xsl:value-of select="count(//i:issue[@status=$state])"></xsl:value-of>
            </td>
          </tr>
        </xsl:for-each>
        <tr>
          <th>total</th>
          <xsl:for-each select="//i:head/i:targets/i:target">
            <xsl:variable name="target">
              <xsl:value-of select="."></xsl:value-of>
            </xsl:variable>
            <td>
              <xsl:value-of select="count(//i:issue[i:target=$target])"></xsl:value-of>
            </td>
          </xsl:for-each>
          <td>
            <xsl:value-of select="count(//i:issue)"></xsl:value-of>
          </td>
        </tr>
      </tbody>
    </table>
  </xsl:template>

  <!-- key for summaries -->
  <xsl:key name="type" match="//i:states/i:state" use="."/>

  <!-- summary -->
  <xsl:template name="summary">
    <xsl:param name="type"/>
    <h2>
      <a name="{$type}">
        <xsl:value-of select="$type"/> Issues Summary
      </a>
    </h2>
    <table cellspacing="0" width="95%" class="summary">
      <tr>
        <th>id</th>
        <th>owner</th>
        <th>title</th>
        <th>protocol</th>
        <th>type</th>
        <th></th>
        <th></th>
      </tr>
      <xsl:apply-templates select="//i:issue" mode="summaryitem">
        <!--    <xsl:sort select="i:target"/>  
        <xsl:sort select="@id"/> -->
        <xsl:with-param name="type">
          <xsl:value-of select="$type"/>
        </xsl:with-param>
      </xsl:apply-templates>
    </table>
  </xsl:template>

  <!-- summary item -->
  <xsl:template match="i:issue" mode="summaryitem">
    <xsl:param name="type"/>
    <xsl:variable name="issue" select="@id"/>
    <xsl:if test="@status=$type">
      <tr class="{@status}">
        <th>
          <a href="#{@id}">
            <xsl:value-of select="@id"></xsl:value-of>
          </a>
        </th>
        <td>
          <small>
            <xsl:apply-templates select="i:owner" mode="summary"></xsl:apply-templates>
          </small>
        </td>
        <td>
          <small>
            <xsl:value-of select="i:title"></xsl:value-of>
          </small>
        </td>
        <td>
          <small>
            <xsl:for-each select="i:protocol">
              <xsl:value-of select="." />
              <xsl:if test="position()!=last()">,</xsl:if>
            </xsl:for-each>
          </small>
        </td>
        <td>
          <small>
            <xsl:value-of select="i:type"></xsl:value-of>
          </small>
        </td>
        <td>
          <xsl:if test="$type='Open' and count(i:proposal)>0">
            <span class="marker">*</span>
          </xsl:if>
        </td>
      </tr>
    </xsl:if>
  </xsl:template>

  <!-- summary owner -->
  <xsl:template match="i:owner" mode="summary">
    <xsl:choose>
      <xsl:when test="@href">
        <a href="{@href}">
          <xsl:value-of select="."/>
        </a>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- full issue -->
  <xsl:template match="i:issue" mode="full">
    <xsl:variable name="status">
      <xsl:value-of select="@status"/>
    </xsl:variable>
    <xsl:if test="count(//i:head/i:states/i:state[text()=$status])=0">
      <xsl:message>
        <xsl:value-of select="@status"/> not a valid status.
      </xsl:message>
    </xsl:if>
    <tbody class="{@status}">
      <tr class="title">
        <td valign="top" align="right" class="title">
          <a name="{@id}">
            <xsl:value-of select="@id"></xsl:value-of>
          </a>
        </td>
        <td class="title">
          <strong>
            <xsl:value-of select="i:title"></xsl:value-of>
          </strong>
        </td>
        <td class="title" align="right">
          <xsl:value-of select="i:target"/>
          <xsl:if test="i:type">
            - <xsl:value-of select="i:type"></xsl:value-of>
          </xsl:if>
          - <xsl:value-of select="@status"></xsl:value-of>
        </td>
      </tr>
      <tr>
        <td>
        </td>
        <td colspan="2">
          <dl>
            <dt>Description</dt>
            <dd>
              <xsl:apply-templates select="i:description" mode="text"/>
            </dd>
            <dt>Raised against</dt>
            <dd>
              <xsl:apply-templates select="i:protocol"/>
            </dd>
            <xsl:apply-templates select="i:justification"/>
            <dt>Related Issues</dt>
            <dd>
              <xsl:apply-templates select="i:relid"/>
            </dd>
            <xsl:apply-templates select="i:origin"/>
            <xsl:apply-templates select="i:owner"/>
            <xsl:apply-templates select="i:proposal|i:resolution" mode="issue-body"/>
          </dl>
        </td>
      </tr>
    </tbody>
  </xsl:template>

  <!-- protocol revision -->
  <xsl:template match="i:protocol">
    <xsl:variable name="protocol">
      <xsl:value-of select="text()"/>
    </xsl:variable>
    <xsl:variable name="revision">
      <xsl:value-of select="@revision"/>
    </xsl:variable>
    <xsl:variable name="link">
      <xsl:for-each select="//i:head/i:protocols/i:protocol">
        <xsl:if test="@name=$protocol">
          <xsl:for-each select="i:revision">
            <xsl:if test="@artifact=$revision">
              <xsl:value-of select="@href"/>
            </xsl:if>
          </xsl:for-each>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>
    <a href="{$link}">
      <xsl:apply-templates select="." mode="text"/> revision: <xsl:value-of select="@revision"/>
    </a>
  </xsl:template>

  <!-- justification -->
  <xsl:template match="i:justification">
    <dt>Justification</dt>
    <dd>
      <xsl:apply-templates select="." mode="text"/>
    </dd>
  </xsl:template>

  <!-- related issues -->
  <xsl:template match="i:relid">
    <xsl:variable name="relid">
      <xsl:value-of select="."/>
    </xsl:variable>
    <a href="#{$relid}">
      <xsl:apply-templates select="." mode="text"/>
    </a>
  </xsl:template>

  <!-- origin -->
  <xsl:template match="i:origin">
    <dt>Origin</dt>
    <dd>
      <xsl:choose>
        <xsl:when test="@href">
          <a href="{@href}">
            <xsl:value-of select="."/>
          </a>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="."/>
        </xsl:otherwise>
      </xsl:choose>
    </dd>
  </xsl:template>

  <!-- owner -->
  <xsl:template match="i:owner">
    <dt>Owner</dt>
    <dd>
      <xsl:choose>
        <xsl:when test="@href">
          <a href="{@href}">
            <xsl:value-of select="."/>
          </a>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="."/>
        </xsl:otherwise>
      </xsl:choose>
    </dd>
  </xsl:template>

  <!-- proposal -->
  <xsl:template match="i:proposal" mode="issue-body">
    <xsl:variable name="num">
      <xsl:value-of select="count(preceding-sibling::i:proposal)+1"/>
    </xsl:variable>
    <dt class="proposal">
      <xsl:choose>
        <xsl:when test="@href">
          <a class="ext" href="{@href}">
            Proposal <xsl:value-of select="$num"/>
          </a>
        </xsl:when>
        <xsl:otherwise>
          Proposal <xsl:value-of select="$num"/>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:if test="@date">
        <span class="date">
          <xsl:value-of select="@date"/>
        </span>
      </xsl:if>
    </dt>
    <dd class="proposal">
      <xsl:apply-templates select="i:protocol"/>
      <xsl:apply-templates mode="text"/>
    </dd>
  </xsl:template>

  <!-- resolution -->
  <xsl:template match="i:resolution" mode="issue-body">
    <dt class="resolution">
      <xsl:choose>
        <xsl:when test="@href">
          <a class="ext" href="{@href}">Resolution</a>
        </xsl:when>
        <xsl:otherwise>Resolution</xsl:otherwise>
      </xsl:choose>
      <xsl:if test="@date">
        <span class="date">
          <xsl:value-of select="@date"/>
        </span>
      </xsl:if>
    </dt>
    <dd class="resolution">
      <xsl:apply-templates select="i:protocol"/>
      <xsl:apply-templates mode="text"/>
    </dd>
  </xsl:template>

  <!-- text -->
  <xsl:template match="h:p" mode="text">
    <p>
      <xsl:apply-templates mode="text"/>
    </p>
  </xsl:template>
  <xsl:template match="h:br" mode="text">
    <br/>
    <xsl:apply-templates mode="text"/>
  </xsl:template>
  <xsl:template match="h:ol" mode="text">
    <ol>
      <xsl:apply-templates mode="text"/>
    </ol>
  </xsl:template>
  <xsl:template match="h:ul" mode="text">
    <ul>
      <xsl:apply-templates mode="text"/>
    </ul>
  </xsl:template>
  <xsl:template match="h:li" mode="text">
    <li>
      <xsl:apply-templates mode="text"/>
    </li>
  </xsl:template>
  <xsl:template match="h:i" mode="text">
    <i>
      <xsl:apply-templates mode="text"/>
    </i>
  </xsl:template>
  <xsl:template match="h:em" mode="text">
    <em>
      <xsl:apply-templates mode="text"/>
    </em>
  </xsl:template>
  <xsl:template match="h:b" mode="text">
    <b>
      <xsl:apply-templates mode="text"/>
    </b>
  </xsl:template>
  <xsl:template match="h:strong" mode="text">
    <strong>
      <xsl:apply-templates mode="text"/>
    </strong>
  </xsl:template>
  <xsl:template match="h:code" mode="text">
    <code>
      <xsl:apply-templates mode="text"/>
    </code>
  </xsl:template>
  <xsl:template match="h:pre" mode="text">
    <pre>
      <xsl:apply-templates mode="text"/>
    </pre>
  </xsl:template>
  <xsl:template match="h:a" mode="text">
    <a class="ext" href="{@href}">
      <xsl:apply-templates mode="text"/>
    </a>
  </xsl:template>

  <!--<xsl:template match="i:description">
    -->
  <!-- The below always copied the elements as the default namespace of the other doc 
    even though they were qualified otherwise-->
  <!--
    <xsl:copy-of select="@*|*|text()"/>
  </xsl:template>-->
</xsl:stylesheet>