<?xml version="1.0"?>
<xsl:stylesheet
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xhtml="http://www.w3.org/1999/xhtml"
	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"/>

	<xsl:template name="insight">
		<xsl:variable name="rings">
			<xsl:call-template name="sum-rings">
				<xsl:with-param name="list" select="/character/rings/*"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:variable name="skills">
			<!-- insight is linear up to 5, which gives a +2 bonus,
				and then to 10, which gives a +5 bonus -->
			<xsl:value-of select="count(skills/skill/rank)+2*count(skills/skill[count(rank)&gt;=5])+5*count(skills/skill[count(rank)&gt;=10])"/>
		</xsl:variable>
		<xsl:value-of select="$rings*10+$skills"/>
	</xsl:template>

	<xsl:template name="sum-rings">
		<xsl:param name="list"></xsl:param>
		<xsl:param name="n" select="1"/>

		<xsl:variable name="car">
			<xsl:call-template name="ring">
				<xsl:with-param name="ring" select="$list[$n]"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="cdr">
			<xsl:choose>
				<xsl:when test="$n &lt; count($list)">
					<xsl:call-template name="sum-rings">
						<xsl:with-param name="list" select="$list"/>
						<xsl:with-param name="n" select="$n + 1"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>0</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:value-of select="$car + $cdr"/>
	</xsl:template>

	<xsl:template name="sum1toN">
		<xsl:param name="n" select="1"/>
		<xsl:value-of select="$n * ( $n + 1 ) div 2"/>
	</xsl:template>

	<xsl:template name="cost-stuff">
		<xsl:param name="list"></xsl:param>
		<xsl:param name="source" select="'.'"/>
		<xsl:param name="scale" select="1"/>
		<xsl:param name="n" select="1"/>

		<xsl:variable name="car">
			<xsl:call-template name="sum1toN">
				<xsl:with-param name="n">
					<xsl:choose>
						<xsl:when test="$source='rank'">
							<xsl:value-of select="count($list[$n]/rank)"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="$list[$n]"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:with-param>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="cdr">
			<xsl:choose>
				<xsl:when test="$n &lt; count($list)">
					<xsl:call-template name="cost-stuff">
						<xsl:with-param name="list" select="$list"/>
						<xsl:with-param name="source" select="$source"/>
						<xsl:with-param name="scale" select="$scale"/>
						<xsl:with-param name="n" select="$n + 1"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>0</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:choose>
			<xsl:when test="$list[$n]/@adjustment != ''">
				<xsl:value-of select="$scale * ($car + $list[$n]/@adjustment) + $cdr"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$scale * $car + $cdr"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>


	<!--
		basic processing begins here
	-->
	<xsl:template match="/character">
		<xsl:processing-instruction name="xml-stylesheet">href="L5R.css" type="text/css"</xsl:processing-instruction>
		<character>
			<!-- IE doesn't understand the PI, and always interprets stuff as HTML -->
			<!-- IE recognizes the @import! -->
			<style>@import 'L5R.css';</style>

			<h1><xsl:value-of select="name"/></h1>
			<xsl:call-template name="insight"/> Insight,
			<xsl:value-of select="floor(10*(honour + sum(log/award/@h))) div 10"/> Honour,
			<xsl:value-of select="floor(10*(glory + sum(log/award/@g))) div 10"/> Glory
			<br/>

			<xsl:variable name="costSkills">
				<xsl:call-template name="cost-stuff">
					<xsl:with-param name="list" select="skills/skill"/>
					<xsl:with-param name="source" select="'rank'"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:variable name="costStats">
				<xsl:call-template name="cost-stuff">
					<xsl:with-param name="list" select="rings/*/*|rings/void"/>
					<xsl:with-param name="scale" select="4"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:variable name="costBackgrounds">
				<xsl:value-of select="sum(backgrounds/*/@cost) + sum(backgrounds/*/@adjustment)"/>
			</xsl:variable>
			<xsl:variable name="costAdjustments">
				<xsl:value-of select="sum(adjustments/*) + sum(log/spent/@xp)"/>
			</xsl:variable>

			<xsl:value-of select="$costStats + $costSkills + $costBackgrounds + $costAdjustments"/>
			/ <xsl:value-of select="45 + sum(log/award/@xp)"/>
			points spent

			<h1>Stats</h1>
			<xsl:apply-templates select="rings"/>

			<h1>Skills</h1>
			<xsl:apply-templates select="skills"/>

			<h1>Backgrounds</h1>
			<xsl:apply-templates select="backgrounds"/>

			<h1>Items</h1>
			<xsl:apply-templates select="items"/>
		</character>

	</xsl:template>

	<!-- Stats -->
	<xsl:template name="ring">
		<xsl:param name="ring" select="/.."/>
		<xsl:choose>
			<xsl:when test="name($ring)='void'">
				<xsl:copy-of select="$ring"/>
			</xsl:when>
			<xsl:when test="$ring/*[last()] &lt; $ring/*[position()=1]">
				<xsl:copy-of select="$ring/*[last()]"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:copy-of select="$ring/*[position()=1]"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="rings">
		<table cellspacing="0" cellpadding="0">
			<tr>
				<xsl:apply-templates select="*"/>
			</tr>
		</table>
	</xsl:template>

	<xsl:template match="void">
		<td class="ring">
			<span class="value"><xsl:value-of select="."/></span>
			<span class="name"><xsl:value-of select="name()"/></span>
		</td>
	</xsl:template>

	<xsl:template match="earth|water|fire|air">
		<td class="ring">
			<span class="value">
				<xsl:call-template name="ring">
					<xsl:with-param name="ring" select="."/>
				</xsl:call-template>
			</span>
			<span class="name"><xsl:value-of select="name()"/></span>
			<div class="attributes">
				<xsl:apply-templates select="*"/>
			</div>
		</td>
	</xsl:template>

	<xsl:template match="stamina|willpower|strength|perception|agility|intelligence|reflexes|awareness">
		<div class="attribute">
			<span class="value"><xsl:value-of select="."/></span>
			<span class="name"><xsl:value-of select="name()"/></span>
		</div>
	</xsl:template>

	<!-- Skills -->
	<xsl:template match="skills">
		<!-- FIX: this is messy -->
		<table cellspacing="0" cellpadding="0">
			<tr>
				<td>
					<xsl:call-template name="skillgroup">
						<xsl:with-param name="group" select="'high'"/>
					</xsl:call-template>
				</td>
				<td>
					<xsl:call-template name="skillgroup">
						<xsl:with-param name="group" select="'bugei'"/>
					</xsl:call-template>
				</td>
				<td>
					<xsl:call-template name="skillgroup">
						<xsl:with-param name="group" select="'low'"/>
					</xsl:call-template>
				</td>
			</tr>
		</table>
	</xsl:template>

	<xsl:template name="skillgroup">
		<xsl:param name="group" select="''"/>
		<table cellspacing="0" cellpadding="0">
			<tr>
				<th><xsl:value-of select="$group"/></th>
			</tr>
			<tr>
				<td>
					<xsl:apply-templates select="*[@class=$group]">
						<xsl:sort select="@name"/>
					</xsl:apply-templates>
				</td>
			</tr>
		</table>
	</xsl:template>

	<xsl:template match="skill">
		<div class="skill">
			<span class="rank"><xsl:value-of select="count(rank)"/></span>
			<span class="name">
				<xsl:value-of select="@name"/>
				<xsl:if test="@type!=''">
					(<xsl:value-of select="@type"/>)
				</xsl:if>
				<xsl:if test="count(emphasis)">
					[<xsl:apply-templates select="emphasis"/>]
				</xsl:if>
			</span>
		</div>
	</xsl:template>

	<xsl:template match="emphasis">
		+<xsl:value-of select="."/>
	</xsl:template>

	<!-- Backgrounds -->
	<xsl:template match="backgrounds">
		<table cellspacing="0" cellpadding="0">
			<xsl:apply-templates select="bg"/>
		</table>
	</xsl:template>

	<xsl:template match="bg">
		<tr class="background">
			<td class="value">
				<xsl:value-of select="@cost"/>
					<xsl:if test="@adjustment != ''">
						(<xsl:value-of select="@cost + @adjustment"/>)
					</xsl:if>
			</td>
			<td class="name">
				<xsl:value-of select="."/>
			</td>
		</tr>
	</xsl:template>

	<!-- Items -->
	<xsl:template match="items">
		<ul>
			<li><xsl:value-of select="sum(koku)"/> koku</li>
			<xsl:apply-templates select="item"/>
		</ul>
	</xsl:template>

	<xsl:template match="item">
		<li>
			<xsl:value-of select="."/>
		</li>
	</xsl:template>


</xsl:stylesheet>
