<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output method="html"/>

  <xsl:variable name="ccmetrics.result.list" select="//ccMetricsResults"/>
  <xsl:variable name="ccmetrics.complexity.list" select="$ccmetrics.result.list//methodsByComplexity"/>
  <xsl:variable name="ccmetrics.summary" select="$ccmetrics.result.list/summary"/>

  <xsl:variable name="ccmetrics.summary.averagecomplexity" select="$ccmetrics.summary/totalCCAverage/@value"/>
  <xsl:variable name="ccmetrics.summary.averagecomplexityrating" select="$ccmetrics.summary/totalCCAverage/@rating"/>

  <xsl:variable name="ccmetrics.summary.reuse" select="$ccmetrics.summary/reuseGeneral/@value"/>
  <xsl:variable name="ccmetrics.summary.reuserating" select="$ccmetrics.summary/reuseGeneral/@rating"/>
  
  <xsl:variable name="ccmetrics.summary.mostcoupled" select="$ccmetrics.summary/mostCoupledClass/@value"/>
  <xsl:variable name="ccmetrics.summary.mostcoupledclass" select="$ccmetrics.summary/mostCoupledClass/@culprit"/>
  <xsl:variable name="ccmetrics.summary.mostcoupledrating" select="$ccmetrics.summary/mostCoupledClass/@rating"/>

  <xsl:variable name="ccmetrics.summary.mostcomplex" select="$ccmetrics.summary/worstClass/@value"/>
  <xsl:variable name="ccmetrics.summary.mostcomplexclass" select="$ccmetrics.summary/worstClass/@className"/>
  <xsl:variable name="ccmetrics.summary.mostcomplexrating" select="$ccmetrics.summary/worstClass/@rating"/>

  <xsl:template match="/">
    <table class="section-table" cellpadding="2" cellspacing="0" border="0" width="98%">
      <tr>
        <td class="sectionheader" colspan="5">
          CCMetrics run: Average complexity <xsl:value-of select="$ccmetrics.summary.averagecomplexity"/> (<xsl:value-of select="$ccmetrics.summary.averagecomplexityrating"/>), Reuse: <xsl:value-of select="$ccmetrics.summary.reuse"/> (<xsl:value-of select="$ccmetrics.summary.reuserating"/>)
        </td>
      </tr>
      <tr>
        <td class="section-data" valign="top">
          <b>Method</b>
        </td>
        <td class="section-data" valign="top">
          <b>Complexity</b>
        </td>
        <td class="section-data" valign="top">
          <b>Coupling</b>
        </td>
        <td class="section-data" valign="top">
          <b>Size</b>
        </td>
        <td class="section-data" valign="top">
          <b>Called</b>
        </td>
      </tr>
      <xsl:apply-templates select="$ccmetrics.complexity.list">
        <xsl:sort select="date" order="descending" data-type="text" />
      </xsl:apply-templates>
      <tr>
        <td colspan="5">
          <br/>
        </td>
      </tr>

      <tr>
        <td class="section-data" colspan="5" valign="top" xml:space="preserve">
          <b>Most Complex Class:</b> <xsl:value-of select="$ccmetrics.summary.mostcomplexclass"/> <xsl:value-of select="$ccmetrics.summary.mostcomplex"/> (<xsl:value-of select="$ccmetrics.summary.mostcomplexrating"/>)
        </td>
      </tr>
      <tr>
        <td  class="section-data" colspan="5" valign="top" xml:space="preserve">
          <b>Most Coupled Class:</b> <xsl:value-of select="$ccmetrics.summary.mostcoupledclass"/> <xsl:value-of select="$ccmetrics.summary.mostcoupled"/> (<xsl:value-of select="$ccmetrics.summary.mostcoupledrating"/>)
        </td>
      </tr>
      <tr>
        <td colspan="5" ></td>
      </tr>
    </table>
  </xsl:template>

  <!-- Method list template -->
  <xsl:template match="method">
    <tr>
      <xsl:if test="position() mod 2=0">
        <xsl:attribute name="class">section-oddrow</xsl:attribute>
      </xsl:if>
      <xsl:if test="position() mod 2!=0">
        <xsl:attribute name="class">section-evenrow</xsl:attribute>
      </xsl:if>

      <td class="section-data" valign="top">
        <xsl:value-of select="@namespace"/>.<xsl:value-of select="@class"/>.<xsl:value-of select="@method"/>
      </td>
      <td class="section-data" valign="top">
        <xsl:value-of select="@complexity"/> (<xsl:value-of select="@rating"/>)
      </td>
      <td class="section-data" valign="top">
        <xsl:value-of select="@coupling"/>
      </td>
      <td class="section-data" valign="top">
        <xsl:value-of select="@size"/> blocks
      </td>
      <td class="section-data" valign="top">
        <xsl:value-of select="@called"/>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>
