<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:fn="http://www.w3.org/2004/07/xpath-functions"
	xmlns:xdt="http://www.w3.org/2004/07/xpath-datatypes"
	xmlns:threshold="data:,threshold">
	
	<xsl:output method="html" />
	
	<xsl:variable name="VIL.root" select="//MetricReport" />
	
	<!-- Thresholds defined -->
	<threshold:x metric="Classes" warning="2" critical="5" />
	<threshold:x metric="Constructors" warning="5" critical="7" />
	<threshold:x metric="Coupling Between Objects" warning="15" critical="30" />
	<threshold:x metric="Cyclomatic Complexity" warning="7" critical="8" />
	<threshold:x metric="Depth in Tree" warning="7" critical="10" />
	<threshold:x metric="Enumerations" warning="3" critical="5" />
	<threshold:x metric="Events" warning="7" critical="10" />
	<threshold:x metric="Fields" warning="20" critical="30" />
	<threshold:x metric="Lines of Code" warning="700" critical="1000" />
	<threshold:x metric="Methods" warning="30" critical="50" />
	<threshold:x metric="Number of Children" warning="7" critical="10" />
	<threshold:x metric="Properties" warning="20" critical="30" />
	<threshold:x metric="Weighted Methods per Class" warning="50" critical="70" />

	<!-- Threshold not defined -->
	<threshold:x metric="Implementations" warning="9999" critical="9999" />
	<threshold:x metric="Implemented Interfaces" warning="9999" critical="9999" />
	<threshold:x metric="Structs" warning="9999" critical="9999" />
	<threshold:x metric="Types" warning="9999" critical="9999" />

	<xsl:template match="/">
			<style>
			<![CDATA[
				#vil .Title {font-family: Verdana; font-size: 14pt; color: black; font-weight: bold}
				#vil .ListItem {font-family: Verdana; font-size: 12pt; color: black; font-weight: bold}
				#vil .ColumnHeader {font-family: Verdana; font-size: 7pt; background-color:white; color: black}
				#vil .Information {font-family: Verdana; font-size: 10pt; color: black; font-weight: bold; text-align: left}
				#vil .Warning {background:#FFFF66; color: black;}
				#vil .Critical {background:#FF9999; color: black;}
				#vil .AlternateRow {background:lightblue}
			]]>
			</style>
			<div id="vil">
				<xsl:apply-templates select="$VIL.root" />
			</div>
	</xsl:template>
	
	<xsl:template match="MetricReport">
		<div class="Title">Code Metric Report</div>
		<xsl:apply-templates select="Specification" />
		<xsl:apply-templates select="Results" />
	</xsl:template>
	
	<xsl:template match="Specification">
		<br />
		<xsl:apply-templates select="Assemblies" />
	</xsl:template>
	
	<xsl:template match="Assemblies">
		<table border="0">
			<tr>
				<th class="ColumnHeader" align="left">Assemblies</th>
			</tr>
			<xsl:apply-templates select="Assembly">
			</xsl:apply-templates>
		</table>
	</xsl:template>
	
	<xsl:template match="Assembly">
		<tr>
			<td nowrap="">
				<xsl:value-of select="." />
			</td>
		</tr>
	</xsl:template>

	<xsl:template match="Results">
		<br />
		<table border="0" cellspacing="1" width="100%">
			<tr>
				<th class="ColumnHeader" style="background:lightblue">Name</th>
				<xsl:for-each select="..//RequestedMetric">
					<th class="ColumnHeader" style="background:lightblue">
						<xsl:value-of select="." />
					</th>
				</xsl:for-each>
			</tr>
			<xsl:apply-templates select="CodeElement">
			</xsl:apply-templates>
		</table>
	</xsl:template>
	
	<xsl:template match="CodeElement">
		<tr>
			<xsl:if test="position() mod 2 = 0">
				<xsl:attribute name="class">AlternateRow</xsl:attribute>
			</xsl:if>
			
			<td>
				<xsl:value-of select="@name" />
			</td>
			<xsl:apply-templates select="Metric" />
		</tr>
	</xsl:template>
	
	<xsl:template match="Metric">
		<td align="right">
			<xsl:choose>
				<xsl:when test=". >= document('')//threshold:x[@metric=current()/@label]/@critical">
					<xsl:attribute name="class">Critical</xsl:attribute>
				</xsl:when>
				<xsl:when test=". >= document('')//threshold:x[@metric=current()/@label]/@warning">
					<xsl:attribute name="class">Warning</xsl:attribute>
				</xsl:when>
			</xsl:choose>
			<xsl:value-of select="." />
		</td>
	</xsl:template>
</xsl:stylesheet>