<?xml version="1.0" encoding="UTF-8" ?>

<!--Document to try and replicate the summary used in the CoC rulebook.
			author: Gary Stewart-->
<xsl:stylesheet version="2.0" xml:lang="en" 
	xmlns:description="http://byakhee.sourceforge.net/xsd/description" 
	xmlns:xlink="http://www.w3.org/1999/xlink"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns="http://www.w3.org/1999/xhtml">

<xsl:output method="xml" indent="yes" />

<xsl:template match="/characters">
	<xsl:processing-instruction name="xml-stylesheet">href="summary.css" type="text/css"</xsl:processing-instruction>
	<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
		<head>
			<title>List of characters</title>
			<link rel="stylesheet" type="text/css" href="summary.css" /> 
		</head>
		<body>
		<p>Number of characters: <xsl:value-of select="count(character)"/></p> 
		<xsl:for-each select="character">
			<xsl:variable name="current-character" select="document(@href)"/>
				<xsl:call-template name="coc-summary">
				<xsl:with-param name="current-character" select="$current-character/character" />
			</xsl:call-template>
		</xsl:for-each>
		</body>
		</html>
		
</xsl:template>

<!--This creates a summary like character sheet as seen in the CoC main rulebook.-->
<xsl:template match="/character">
	<!--TODO: Find full name, for now just assuming name > full is there.-->
	<xsl:param name="name" select="description:name/description:full"/>
	<xsl:processing-instruction name="xml-stylesheet">href="summary.css" type="text/css"</xsl:processing-instruction>
	<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
		<head>
			<title>Summary for <xsl:value-of select="$name"/></title>
			<link type="text/css" href="summary.css" />
		</head>
		<body>
			<!--Keeping HTML seperate from the summary generator (same code could be used to generate a list or such-->
			<xsl:call-template name="coc-summary">
				<xsl:with-param name="current-character" select="." />
			</xsl:call-template>
		</body>
	</html>
</xsl:template>

<xsl:template name="coc-summary">
	<xsl:param name="current-character" select="." />
	<!--TODO: Find full name, for now just assuming name > full is there, keeping this independent as well.-->
	<xsl:param name="name" select="$current-character/description:name/description:full"/>
	<div class="character">
		<div class="character_description">
		<xsl:if test="$current-character/description:image"> 
			<div class="character_portrait"><p><img alt="{$current-character/description:image/@xlink:title}" src="{$current-character/description:image/@xlink:href}"/></p></div>
		</xsl:if>
		<p class="character_name"><xsl:value-of select="$name"></xsl:value-of>.
			 <xsl:if test="$current-character/description:quote">
			 	<q><xsl:value-of select="$current-character/description:quote"/><!--TODO: Add author and location if they exist.--></q>
			 </xsl:if>
		</p>
		<xsl:for-each select="$current-character/description:description">
			<xsl:sort select="@type" order="descending"/>
			<xsl:choose>
				<xsl:when test="@format='text/html'">
					<xsl:copy-of select="."/>
				</xsl:when>
				<xsl:otherwise>
					<p><xsl:value-of select="."/></p>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each>
		</div>
		<div class="character_stats">
			<p><strong><xsl:value-of select="$name"/></strong>, Age: <xsl:value-of select="$current-character/description:age"/>, <xsl:value-of select="$current-character/description:profession"/></p>
			<ul class="attributes">
				<xsl:for-each select="$current-character/attribute">
					<li><span class="name"><xsl:value-of select="@name"/></span><xsl:text> </xsl:text><span class="value"><xsl:call-template name="stat-picker"><xsl:with-param name="stat" select="."/></xsl:call-template></span></li>
				</xsl:for-each>
			</ul>
			<!--Skills next, as it is a summary only want calculated and "value" skills, not base, which can be looked up.-->
			<ul class="skills">
				<xsl:for-each select="$current-character/skill[@value or @calculated or (@sub='true' and count(./*) &gt; 0)]">
					<xsl:if test="@sub='true'">
						<li><span class="name"><xsl:value-of select="@name"/></span>
							<ul class="subskills">
							<xsl:for-each select="./skill">
							  <li><span class="name"><xsl:value-of select="@name"/></span><xsl:text> </xsl:text><span class="value"><xsl:call-template name="stat-picker"><xsl:with-param name="stat" select="."/></xsl:call-template></span></li>
							</xsl:for-each>
							</ul>
						</li>
					</xsl:if>
					<li><span class="name"><xsl:value-of select="@name"/></span><xsl:text> </xsl:text><span class="value"><xsl:call-template name="stat-picker"><xsl:with-param name="stat" select="."/></xsl:call-template></span></li>
				</xsl:for-each>
			</ul>
		</div>
	</div>
</xsl:template>

<xsl:template name="stat-picker">
	<xsl:param name="stat"/>
	<xsl:choose>
		<xsl:when test="@value"><!--value should always be displayed first by choice (though non-summaries might also represent the max values-->
			<xsl:value-of select="@value"/>
		</xsl:when>
		<xsl:when test="@calculated"><!--this has a calculated value, which should be in an XPath form, TODO: need to find a way to make this execute-->
			<xsl:variable name="calculation" select="@calculated" />
			<xsl:value-of select="@base"/><!--temp fix, this might need to be another function that matches the calculation.-->
		</xsl:when>
		<xsl:when test="@base"><!--Use the base value-->
			<xsl:value-of select="@base"/>
		</xsl:when>
		<xsl:otherwise><!--Give the unknown value (00)-->
			<xsl:text>00</xsl:text>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>
</xsl:stylesheet>

