<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

	<!--
		Author: james.ojaste@ec.gc.ca
		License: Creative Commons Attribution-ShareAlike License
					http://creativecommons.org/licenses/by-sa/2.0/
	-->

	<xsl:output method="html"/>

	<!-- document templates -->
	<xsl:template match="/character">
		<html>
		<head><title><xsl:value-of select="name"/></title></head>
		<body>
			<h1>-=Summary=-</h1>
			<div><xsl:apply-templates select="summary"/></div>
			<br/>

			<h1>-=WFRP 2ed stats
				(<xsl:value-of select="count(advancements/*) - count(advancements/skill|advancements/talent|advancements/career)"/>
				/ <xsl:value-of select="count(advancements/*)"/> advances)=-</h1>
			<div>Race: <xsl:value-of select="race"/></div>
			<div>Career Path: <xsl:apply-templates select="careers/career"/>
				(<xsl:value-of select="count(advancements/career)"/>
				/ <xsl:value-of select="count(advancements/*)"/> advances)</div>
			<div><xsl:apply-templates select="stats"/></div>
			<br/>

			<h1>-=Skills
				(<xsl:value-of select="count(advancements/skill)"/>
				/ <xsl:value-of select="count(advancements/*)"/> advances)=-</h1>
			<div>
				<xsl:apply-templates select="start/skill|advancements/skill">
					<xsl:sort select="text()"/>
				</xsl:apply-templates>
			</div>
			<br/>

			<h1>-=Talents
				(<xsl:value-of select="count(advancements/talent)"/>
				/ <xsl:value-of select="count(advancements/*)"/> advances)=-</h1>
			<div>
				<xsl:apply-templates select="start/talent|advancements/talent">
					<xsl:sort select="text()"/>
				</xsl:apply-templates>
			</div>
			<br/>

			<h1>-=Gear=-</h1>
			<div><xsl:call-template name="cash"/></div>
			<div><xsl:apply-templates select="gear/item"/></div>
		</body>
		</html>
	</xsl:template>

	<xsl:template name="cash">
		*Purse with <xsl:value-of select="sum(/character/gear/gc)"/>gc
	</xsl:template>

	<xsl:template match="gc">
	</xsl:template>

	<xsl:template match="skill|talent|item">
		<xsl:param name="prefix">*</xsl:param>
		<xsl:value-of select="$prefix"/>
		<xsl:value-of select="text()"/>
		<br/>
		<xsl:apply-templates select="*">
			<xsl:with-param name="prefix" select="concat ('*', $prefix)"/>
		</xsl:apply-templates>
	</xsl:template>

	<xsl:template name="advancements">
		<xsl:param name="format"/>
		<xsl:param name="st" select="'bs'"/>
		<xsl:value-of select="sum(/character/advancements/*[name()=$st])"/>
		<xsl:value-of select="substring ($format, 1, sum(/character/advancements/*[name()=$st]) &gt;= 0)"/>
	</xsl:template>

	<xsl:template match="careers/career">
		<xsl:value-of select="@name"/>
		<xsl:if test="position()!=last()">
			&#x21D2;
		</xsl:if>
	</xsl:template>

	<xsl:template name="scheme">
		<xsl:param name="step" select="1"/>
		<xsl:param name="format"/>
		<xsl:param name="st" select="'bs'"/>
		<xsl:value-of select="substring ('+', 1, count(/character/careers/career[position()=last()]/*[name()=$st]))"/>
		<xsl:value-of select="$step * count(/character/careers/career[position()=last()]/*[name()=$st])"/>
		<xsl:value-of select="substring ($format, 1, count(/character/careers/career[position()=last()]/*[name()=$st]))"/>
		<!--
		<xsl:value-of select="substring ('+', 1, /character/careers/career[position()=last()]/stats/*[name()=$st] &gt;= 0)"/>
		<xsl:value-of select="/character/careers/career[position()=last()]/stats/*[name()=$st]"/>
		<xsl:value-of select="substring ($format, 1, /character/careers/career[position()=last()]/stats/*[name()=$st] &gt;= 0)"/>
		-->
	</xsl:template>

	<xsl:template name="stat">
		<xsl:param name="format"/>
		<xsl:param name="st" select="'bs'"/>
		<xsl:value-of select="/character/stats/*[name()=$st] + sum(/character/advancements/*[name()=$st])"/><xsl:value-of select="$format"/>
	</xsl:template>

	<xsl:template match="stats">
		||
		<xsl:call-template name="statline">
			<xsl:with-param name="stats" select="WS|BS|S|T|Ag|Int|WP|Fel"/>
			<xsl:with-param name="format">%</xsl:with-param>
		</xsl:call-template>
		<br/><br/>
		<xsl:call-template name="statline">
			<xsl:with-param name="stats" select="A|W|SB|TB|M|Mag|IP|FP"/>
		</xsl:call-template>
		||
	</xsl:template>

	<xsl:template name="statline">
		<!-- |WS|BS|S|T|Ag|Int|WP|Fel -->
		<!-- |A|W|SB|TB|M|Mag|IP|FP -->
		<xsl:param name="stats"/>
		<xsl:param name="format"/>
		__Profile__
			<xsl:for-each select="$stats">
				|__<xsl:value-of select="name()"/>__
			</xsl:for-each>
			<br/>
		__Current__
			<xsl:for-each select="$stats">
				|<xsl:call-template name="stat">
					<xsl:with-param name="format" select="$format"/>
					<xsl:with-param name="st">
						<xsl:value-of select="name()"/>
					</xsl:with-param>
				</xsl:call-template>
			</xsl:for-each>
			<br/>
		__Advance Scheme__
			<xsl:for-each select="$stats">
				|<xsl:call-template name="scheme">
					<xsl:with-param name="format" select="$format"/>
					<xsl:with-param name="st">
						<xsl:value-of select="name()"/>
					</xsl:with-param>
					<xsl:with-param name="step">
						<xsl:choose>
							<xsl:when test="$format='%'">
								<xsl:value-of select="5"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="1"/>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:with-param>
				</xsl:call-template>
			</xsl:for-each>
			<br/>
		__Advances Taken__
			<xsl:for-each select="$stats">
				|<xsl:call-template name="advancements">
					<xsl:with-param name="format" select="$format"/>
					<xsl:with-param name="st">
						<xsl:value-of select="name()"/>
					</xsl:with-param>
				</xsl:call-template>
			</xsl:for-each>
	</xsl:template>

	<xsl:template match="summary">
		<xsl:apply-templates select="*"/>
	</xsl:template>

	<xsl:template match="@*|node()">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()"/>
		</xsl:copy>
	</xsl:template>
</xsl:stylesheet>
