<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" exclude-result-prefixes="i h" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 xmlns:i="http://docs.oasis-open.org/ws-rx/issues/IssuesDoc.xsd" 
 xmlns:h="http://docs.oasis-open.org/ws-rx/issues/Markup.xsd" 
 xmlns="http://www.w3.org/1999/xhtml"
>

	<!--
TODO
 - dependsOn / relatedTo
 - edStatus
 - issue graphs
-->

	<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; }
					<xsl:for-each select="i:head/i:states/i:state[@type='Closed']">
						tbody.<xsl:value-of select="."/> { display: none; }
					</xsl:for-each>
					}

				</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>
				<!--Printing the revision doesn't seem to make sense, there wil be a new version of the main doc after every call-->
				<!--<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: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>target</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="key('type', @status)/@type=$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:value-of select="i:target"></xsl:value-of>
					</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>
				<!--<td>
					<xsl:if test="count($AIs/ai:AI[@issue=$issue])>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="@participant">
				<xsl:call-template name="participant">
					<xsl:with-param name="participant">
						<xsl:value-of select="@participant"/>
					</xsl:with-param>
				</xsl:call-template>
			</xsl:when>-->
			<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>
					<!--<xsl:if test="//i:head/i:search">
						<span class="nav">
							&#8212; <xsl:element name="a">
								<xsl:attribute name="href">
									<xsl:apply-templates select="//i:search">
										<xsl:with-param name="issueID" select="@id"/>
									</xsl:apply-templates>
								</xsl:attribute>
								<xsl:text>search</xsl:text>
							</xsl:element>
						</span>
					</xsl:if>-->
				</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>
						<!--<xsl:call-template name="issueAIs">
							<xsl:with-param name="issue" select="@id"/>
						</xsl:call-template>-->
						<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>

	<!-- 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="@participant">
					<xsl:call-template name="participant">
						<xsl:with-param name="participant">
							<xsl:value-of select="@participant"/>
						</xsl:with-param>
					</xsl:call-template>
				</xsl:when>-->
				<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="@participant">
					<xsl:call-template name="participant">
						<xsl:with-param name="participant">
							<xsl:value-of select="@participant"/>
						</xsl:with-param>
					</xsl:call-template>
				</xsl:when>-->
				<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 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 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: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:em" mode="text">
		<em>
			<xsl:apply-templates mode="text"/>
		</em>
	</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>
	<!-- issue AIs -->
	<!--<xsl:template name="issueAIs">
		<xsl:param name="issue"/>
		<xsl:if test="count($AIs/ai:AI[@issue=$issue])>0">
			<dt>Action Items</dt>
			<xsl:for-each select="$AIs/ai:AI[@issue=$issue]">
				<xsl:variable name="ownerID" select="@owner"/>
				<xsl:variable name="owner" select="$participants/p:participant[@id=$ownerID]"/>
				<xsl:if test="count($owner)&lt;1">
					<xsl:message>
						<xsl:value-of select="$ownerID"/> not found.
					</xsl:message>
				</xsl:if>
				<dd>
					<xsl:value-of select="@assignDate"/>:
					<xsl:value-of select="$owner/p:name"/> to
					<xsl:value-of select="."/>
					<xsl:if test="@dueDate">
						<em> 
							Due <xsl:value-of select="@dueDate"/>
						</em>
					</xsl:if>
					<small> 
						(<xsl:value-of select="@status"/>)
					</small>
				</dd>
			</xsl:for-each>
		</xsl:if>
	</xsl:template>-->

	<!-- search -->
	<!--
	<xsl:template match="i:search">
		<xsl:param name="issueID"/>
		<xsl:value-of select="@base"/>
		<xsl:for-each select="id:pathSegment">
			<xsl:choose>
				<xsl:when test="@value">
					<xsl:value-of select="@value"/>
				</xsl:when>
				<xsl:when test="@type">
					<xsl:value-of select="saxon:evaluate(concat('$', @type))"/>
				</xsl:when>
			</xsl:choose>
		</xsl:for-each>
		<xsl:if test="id:queryArg">?</xsl:if>
		<xsl:for-each select="id:queryArg">
			<xsl:value-of select="@name"/>=<xsl:choose>
				<xsl:when test="@value">
					<xsl:value-of select="@value"/>
				</xsl:when>
				<xsl:when test="@type">
					<xsl:value-of select="saxon:evaluate(concat('$', @type))"/>
				</xsl:when>
			</xsl:choose>
			<xsl:if test="position()!=last()">&amp;</xsl:if>
		</xsl:for-each>
	</xsl:template>-->
	<!-- participant -->
	<!--<xsl:template name="participant">
		<xsl:param name="participant"/>
		<xsl:variable name="p" select="$participants/p:participant[@id=$participant]"/>
		<xsl:if test="count($p)=0">
			<xsl:message>
				<xsl:value-of select="$participant"/> not found.
			</xsl:message>
		</xsl:if>
		<xsl:choose>
			<xsl:when test="$p/p:email">
				<a href="mailto:{$p/p:email}" title="{$p/p:org}">
					<xsl:value-of select="$p/p:name"/>
				</a>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$p/p:name"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>-->
</xsl:stylesheet>