<?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="xml"/>

	<!-- utility templates -->
	<xsl:template name="dots">
		<xsl:param name="total" select="5"/>
		<xsl:param name="filled" select="."/>

		<xsl:param name="dot-filled">&#x25CF;</xsl:param>
		<xsl:param name="dot-empty">&#x25CB;</xsl:param>
		<xsl:param name="depth" select="1"/>
<!--
		<xsl:variable name="dot-filled">&#x25CF;</xsl:variable>
		<xsl:variable name="dot-empty">&#x25CB;</xsl:variable>
-->
		<xsl:choose>
			<xsl:when test="$filled &gt; 0">
				<dot type="filled"><xsl:value-of select="$dot-filled"/></dot>
			</xsl:when>
			<xsl:otherwise>
				<dot type="empty"><xsl:value-of select="$dot-empty"/></dot>
			</xsl:otherwise>
		</xsl:choose>

		<xsl:if test="$depth = 5 or $depth = 10">&#x2008;</xsl:if>

		<xsl:if test="$total &gt; 1">
			<xsl:call-template name="dots">
				<xsl:with-param name="dot-empty" select="$dot-empty"/>
				<xsl:with-param name="dot-filled" select="$dot-filled"/>
				<xsl:with-param name="total" select="$total - 1"/>
				<xsl:with-param name="filled" select="$filled - 1"/>
				<xsl:with-param name="depth" select="$depth + 1"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>

	<!-- document templates -->
	<xsl:template match="abilities">
		<grid>
			<row>
			<cell>
				<subheading>Dawn</subheading>
				<!--<xsl:apply-templates select="dawn"/>-->
				<grid>
				<xsl:apply-templates select="ability[@name='archery' or @name='brawl' or @name='martial arts' or @name='melee' or @name='thrown']"/>
				</grid>
			</cell>
			<cell>
				<subheading>Zenith</subheading>
				<xsl:apply-templates select="ability[@name='endurance' or @name='performance' or @name='presence' or @name='resistance' or @name='survival']"/>
			</cell>
			<cell>
				<subheading>Twilight</subheading>
				<xsl:apply-templates select="ability[@name='crafts' or @name='investigation' or @name='lore' or @name='medicine' or @name='occult']"/>
			</cell>
			</row>

			<row>
			<cell>
				<subheading>Night</subheading>
				<xsl:apply-templates select="ability[@name='athletics' or @name='awareness' or @name='dodge' or @name='larceny' or @name='stealth']"/>
			</cell>
			<cell>
				<subheading>Eclipse</subheading>
				<xsl:apply-templates select="ability[@name='bureaucracy' or @name='linguistics' or @name='ride' or @name='sail' or @name='socialize']"/>
			</cell>
			<cell>
				<subheading>Specialties</subheading>
				<xsl:apply-templates select="specialties"/>
			</cell>
			</row>
		</grid>
	</xsl:template>

	<xsl:template match="abilities/*">
		<grid>
			<xsl:apply-templates select="ability"/>
		</grid>
	</xsl:template>

	<xsl:template match="abilities/ability">
		<xsl:variable name="caste"><xsl:value-of select="ancestor::character/caste"/></xsl:variable>
		<row>
			<xsl:choose>
				<xsl:when test="@type = 'favoured' or name(..) = $caste">
					<favoured>
						<xsl:if test="@calling = 'true'">
							<xsl:attribute name="type">calling</xsl:attribute>
						</xsl:if>
						<xsl:value-of select="@name"/>
						<xsl:if test="@detail != ''">
							<detail><xsl:value-of select="@detail"/></detail>
						</xsl:if>
					</favoured>
				</xsl:when>
				<xsl:otherwise>
					<ability>
						<xsl:if test="@calling = 'true'">
							<xsl:attribute name="type">calling</xsl:attribute>
						</xsl:if>
						<xsl:value-of select="@name"/>
						<xsl:if test="@detail != ''">
							<detail><xsl:value-of select="@detail"/></detail>
						</xsl:if>
					</ability>
				</xsl:otherwise>
			</xsl:choose>
			<dots><xsl:call-template name="dots">
					<xsl:with-param name="total" select="(/character/essence &gt; 5) * /character/essence + (/character/essence &lt;= 5) * 5"/>
				</xsl:call-template></dots>
		</row>
	</xsl:template>

	<xsl:template match="attributes">
		<grid id="attributes">
			<row>
			<cell><xsl:apply-templates select="physical"/></cell>
			<cell><xsl:apply-templates select="social"/></cell>
			<cell><xsl:apply-templates select="mental"/></cell>
			</row>
		</grid>
	</xsl:template>

	<xsl:template match="attributes/*">
		<grid>
			<xsl:apply-templates select="*"/>
		</grid>
	</xsl:template>

	<xsl:template match="attributes/*/*">
		<row>
			<attribute><xsl:value-of select="name()"/></attribute>
			<dots><xsl:call-template name="dots">
					<xsl:with-param name="total" select="(/character/essence &gt; 5) * /character/essence + (/character/essence &lt;= 5) * 5"/>
				</xsl:call-template></dots>
		</row>
	</xsl:template>

	<xsl:template match="backgrounds">
		<grid>
			<xsl:apply-templates select="background"/>
		</grid>
	</xsl:template>

	<xsl:template match="backgrounds/background">
		<background>
			<cell>
				<xsl:value-of select="type"/>
				<xsl:if test="detail != ''">
					<note><xsl:value-of select="detail"/></note>
				</xsl:if>
			</cell>
			<cell><xsl:apply-templates select="level"/></cell>
		</background>
	</xsl:template>

	<xsl:template match="background/level">
		<dots><xsl:call-template name="dots"/></dots>
	</xsl:template>

	<xsl:template match="character">

		<xsl:processing-instruction name="xml-stylesheet">href="Exalted.css" type="text/css"</xsl:processing-instruction>
		<xsl:variable name="ref">Ox-Body Technique</xsl:variable>
		<character>
			<!-- IE doesn't understand the PI, and always interprets stuff as HTML -->
			<!-- IE thinks XSL should trump CSS, not work together =p -->
			<!--<style>* {display: block;}</style>-->
			<!-- IE recognizes the @import! -->
			<style>@import 'Exalted.css';</style>

			<watermark>
				<xsl:choose>
					<xsl:when test="caste = 'dawn'"> &#x263c;</xsl:when>
					<xsl:when test="caste = 'zenith'"> &#x25cf;</xsl:when>
					<xsl:when test="caste = 'twilight'"> &#x25d3;</xsl:when>
					<xsl:when test="caste = 'night'"> &#x25cb;</xsl:when>
					<xsl:when test="caste = 'eclipse'"> &#x25c9;</xsl:when>
				</xsl:choose>
			</watermark>

			<grid>
				<row>
					<logo>Exalted</logo>
					<grid>
					<row>
						<cell>Name:</cell><cell><xsl:value-of select="name"/></cell>
						<cell>Concept:</cell><cell><xsl:value-of select="concept"/></cell>
					</row>
					<row>
						<!--<cell>Player:</cell><cell><xsl:value-of select="player"/></cell>-->
						<cell>Caste:</cell>
						<caste>
							<xsl:value-of select="caste"/>
						</caste>
						<cell>Nature:</cell><cell><xsl:value-of select="nature"/></cell>
					</row>
					<row>
						<cell>Experience:</cell>
						<cell><xsl:value-of select="sum(log/award/points) - sum(log/spent/points)"/>
							/ <xsl:value-of select="sum(log/award/points)"/></cell>
						<cell>Anima:</cell><cell><xsl:value-of select="anima/appearance"/></cell>
					</row>
					</grid>
				</row>
			</grid>

			<heading>Attributes</heading>
			<xsl:apply-templates select="attributes"/>

			<heading>Abilities</heading>
			<xsl:apply-templates select="abilities"/>

			<heading>Advantages</heading>
			<grid>
				<row>
				<cell>
					<subheading>Backgrounds</subheading>
					<xsl:apply-templates select="backgrounds"/>

					<heading>Weapons</heading>
					<xsl:apply-templates select="weapons"/>

					<heading>Anima Effect</heading>
					<xsl:apply-templates select="anima/effect"/>

					<heading>Virtues</heading>
					<xsl:apply-templates select="virtues"/>

					<heading>Limit Break</heading>
					<blocks>
						<xsl:call-template name="dots">
							<xsl:with-param name="dot-empty">&#x25A1;</xsl:with-param>
							<xsl:with-param name="total" select="10"/>
							<xsl:with-param name="filled" select="0"/>
						</xsl:call-template>
					</blocks>

					<xsl:if test="virtue != ''">
						<heading>Virtue Flaw</heading>
					</xsl:if>

					<heading>Essence</heading>
					<xsl:apply-templates select="essence"/>

					<heading>Willpower</heading>
					<xsl:apply-templates select="willpower"/>

					<heading>Health</heading>
					<xsl:call-template name="health"/>
				</cell>
				<cell>
					<subheading>Charms</subheading>
					<xsl:apply-templates select="charms"/>
				</cell>
				</row>
			</grid>

 	</character>

	</xsl:template>

	<xsl:template match="charm/reference">
		<reference>
			<page><xsl:value-of select="page"/></page>
		</reference>
	</xsl:template>

	<xsl:template match="charms">
		<grid>
			<xsl:apply-templates select="charm">
				<xsl:sort select="reference/skill"/>
			</xsl:apply-templates>
			<xsl:apply-templates select="combo"/>
		</grid>
	</xsl:template>

	<xsl:template match="charms/charm">
		<charm>
			<cost>
				<xsl:if test="activate/motes != ''">
					<xsl:value-of select="activate/motes"/>m
				</xsl:if>
				<xsl:if test="activate/willpower != ''">
					<xsl:value-of select="activate/willpower"/>w
				</xsl:if>
				<xsl:if test="activate/special != ''">
					<xsl:value-of select="activate/special"/>
				</xsl:if>
			</cost>
			<description>
				<charm><xsl:value-of select="reference/skill"/>: <xsl:value-of select="name"/></charm>
				<note><xsl:value-of select="detail"/></note>
			</description>
			<notes>
				<xsl:apply-templates select="reference"/>
				<duration><xsl:value-of select="duration"/></duration>
				<type><xsl:value-of select="type"/></type>
			</notes>
		</charm>
	</xsl:template>

	<xsl:template match="charms/combo">
		<xsl:variable name="componentnames" select="charm"/>
		<xsl:variable name="components" select="/character/charms/charm[name=$componentnames]"/>
		<row>
			<cost>
				<xsl:if test="sum($components/activate/motes) &gt; 0">
					<xsl:value-of select="sum($components/activate/motes)"/>m
				</xsl:if>
				<xsl:if test="sum($components/activate/willpower) &gt; 0">
					<xsl:value-of select="sum($components/activate/willpower)"/>w
				</xsl:if>
				<xsl:for-each select="$components/activate/special">
						+ <xsl:value-of select="."/>
				</xsl:for-each>
			</cost>
			<cell>
				<name>combo: <xsl:value-of select="name"/></name>
				<note><xsl:value-of select="detail"/></note>
			</cell>
			<notes>Combo</notes>
		</row>
		<charm>
			<cost/>
			<cell>
				<combo>
					<xsl:for-each select="charm">
						<charm><xsl:value-of select="."/></charm>
					</xsl:for-each>
				</combo>
			</cell>
			<notes/>
		</charm>
	</xsl:template>

	<xsl:template match="essence">
		<grid>
			<row>
				<dots>
					<xsl:call-template name="dots">
						<xsl:with-param name="total" select="6"/>
						<xsl:with-param name="filled" select="."/>
					</xsl:call-template>
				</dots>
			</row>
			<row>
				<cell>Committed:</cell>
				<cell><xsl:value-of select="/character/spent/motes/committed"/></cell>
			</row>
			<row>
				<cell>Personal:</cell>
				<cell>
					<xsl:value-of select="/character/spent/motes/personal"/>
					/
					<xsl:value-of select=". * 3 + /character/willpower"/>
				</cell>
			</row>
			<row>
				<cell>Peripheral:</cell>
				<cell><xsl:value-of select="/character/spent/motes/peripheral + /character/spent/motes/committed"/>
				/
				<xsl:value-of select=". * 7 + /character/willpower + sum(/character/virtues/*)"/></cell>
			</row>
		</grid>
	</xsl:template>

	<xsl:template name="health">
		<xsl:variable name="addhealth" select="/character/charms/charm[name='Ox-Body Technique']/addhealth"/>
		<xsl:variable name="wounds" select="sum(/character/wounds/*)"/>
		<xsl:variable name="level0" select="1 + sum($addhealth/attribute::level0)"/>
		<xsl:variable name="level1" select="2 + sum($addhealth/attribute::level1)"/>
		<xsl:variable name="level2" select="2 + sum($addhealth/attribute::level2)"/>
		<xsl:variable name="level4" select="1 + sum($addhealth/attribute::level4)"/>
		<grid>
			<row>
			<cell>0</cell>
			<blocks>
				<xsl:call-template name="dots">
					<xsl:with-param name="dot-empty">&#x25A1;</xsl:with-param>
					<xsl:with-param name="total" select="$level0"/>
					<xsl:with-param name="filled" select="$wounds"/>
				</xsl:call-template>
			</blocks>
			</row>
			<row>
			<cell>1</cell>
			<blocks>
				<xsl:call-template name="dots">
					<xsl:with-param name="dot-empty">&#x25A1;</xsl:with-param>
					<xsl:with-param name="total" select="$level1"/>
					<xsl:with-param name="filled" select="$wounds - $level0"/>
				</xsl:call-template>
			</blocks>
			</row>
			<row>
			<cell>2</cell>
			<blocks>
				<xsl:call-template name="dots">
					<xsl:with-param name="dot-empty">&#x25A1;</xsl:with-param>
					<xsl:with-param name="total" select="$level2"/>
					<xsl:with-param name="filled" select="$wounds - $level0 - $level1"/>
				</xsl:call-template>
			</blocks>
			</row>
			<row>
			<cell>4</cell>
			<blocks>
				<xsl:call-template name="dots">
					<xsl:with-param name="dot-empty">&#x25A1;</xsl:with-param>
					<xsl:with-param name="total" select="$level4"/>
					<xsl:with-param name="filled" select="$wounds - $level0 - $level1 - $level2"/>
				</xsl:call-template>
			</blocks>
			</row>
			<row>
			<cell><!--&#x2620;-->X</cell>
			<blocks>
				<xsl:call-template name="dots">
					<xsl:with-param name="dot-empty">&#x25A1;</xsl:with-param>
					<xsl:with-param name="total" select="1"/>
					<xsl:with-param name="filled" select="$wounds - $level0 - $level1 - $level2 - $level4"/>
				</xsl:call-template>
			</blocks>
			</row>
		</grid>
	</xsl:template>

	<xsl:template match="virtues">
		<grid>
			<xsl:apply-templates select="*"/>
		</grid>
	</xsl:template>

	<xsl:template match="virtues/*">
		<row>
			<virtue><xsl:value-of select="name()"/></virtue>
			<cell>
				<dots><xsl:call-template name="dots"/></dots>
				<blocks>
					<xsl:call-template name="dots">
						<xsl:with-param name="dot-empty">&#x25A1;</xsl:with-param>
						<xsl:with-param name="filled" select="0"/>
					</xsl:call-template>
				</blocks>
			</cell>
		</row>
	</xsl:template>


	<xsl:template match="weapons">
		<grid>
			<row>
				<cellheader>Name</cellheader>
				<cellheader>Spd</cellheader>
				<cellheader>Acc</cellheader>
				<cellheader>Def</cellheader>
				<cellheader>Dmg</cellheader>
			</row>
			<xsl:apply-templates select="weapon"/>
		</grid>
	</xsl:template>

	<xsl:template match="weapons/weapon">
		<row>
			<cell><xsl:value-of select="name"/></cell>
			<cell><xsl:value-of select="speed"/></cell>
			<cell><xsl:value-of select="accuracy"/></cell>
			<cell><xsl:value-of select="defense"/></cell>
			<cell>
				<xsl:value-of select="damage"/>
				<xsl:if test="lethal">L</xsl:if>
			</cell>
		</row>
	</xsl:template>

	<xsl:template match="willpower">
		<dots>
			<xsl:call-template name="dots">
				<xsl:with-param name="total" select="10"/>
				<xsl:with-param name="filled" select="."/>
			</xsl:call-template>
		</dots>
		<blocks>
			<xsl:call-template name="dots">
				<xsl:with-param name="dot-empty">&#x25A1;</xsl:with-param>
				<xsl:with-param name="total" select="10"/>
				<xsl:with-param name="filled" select="0"/>
			</xsl:call-template>
		</blocks>
	</xsl:template>

</xsl:stylesheet>
