<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />
  <xsl:template match="BuildInformation">
    <xsl:apply-templates select="BuildInformationNode" />
  </xsl:template>
  <xsl:template match="BuildInformationNode[@Type = 'ActivityTracking']">
    <div class="BuildNode">
      <div class="BuildHeader">
        <span class="BuildName"><xsl:value-of select="Fields/InformationField[@Name = 'DisplayName']/@Value" /></span>
        <span class="ExpandCollapseIcon" />
        <span class="BuildDuration"><xsl:value-of select="Fields/InformationField[@Name = 'Duration']/@Value"/></span>    
      </div>
      <xsl:apply-templates select="Children/BuildInformationNode[@Type = 'ActivityInput']" />
      <xsl:apply-templates select="Children/BuildInformationNode[@Type = 'ActivityOutput']" />
      <xsl:apply-templates select="Children/BuildInformationNode[@Type != 'ActivityOutput' and @Type != 'ActivityInput']" />
    </div>
  </xsl:template>
  <xsl:template match="BuildInformationNode[@Type = 'AgentScopeActivityTracking']">
    <xsl:variable name="uri" select="Fields/InformationField[@Name = 'ReservedAgentUri']/@Value" />
    <xsl:variable name="name" select="Fields/InformationField[@Name = 'ReservedAgentName']/@Value" />
    <xsl:variable name="status" select="Fields/InformationField[@Name = 'ReservationStatus']/@Value" />
    <xsl:variable name="displayName" select="Fields/InformationField[@Name = 'DisplayName']/@Value" />
    <div class="BuildNode">
      <div class="BuildHeader">
        <xsl:choose>
          <xsl:when test="$status = 'AgentRequested'">
            <span class="BuildName" _locID="AgentRequested"><xsl:value-of select="$displayName" /> (waiting for a build agent)</span>
          </xsl:when>
          <xsl:when test="$status = 'AgentReserved' or $status = 'AgentReleased'">
            <span class="BuildName" _locID="AgentReserved"><xsl:value-of select="$displayName"/> (reserved build agent <xsl:value-of select="$name" />)</span>
          </xsl:when>
        </xsl:choose>
        <span class="ExpandCollapseIcon" />
        <span class="BuildDuration"><xsl:value-of select="Fields/InformationField[@Name = 'Duration']/@Value"/></span>
      </div>
      <xsl:apply-templates select="Children/BuildInformationNode[@Type = 'ActivityInput']" />
      <xsl:apply-templates select="Children/BuildInformationNode[@Type = 'ActivityOutput']" />
      <xsl:if test="starts-with($uri, 'vstfs:///Build/Agent/')">
        <xsl:variable name="agentLog" select="concat('ActivityLog.AgentScope.', substring-after($uri, 'vstfs:///Build/Agent/'), '.xml')" />
        <div class="BuildNode">
          <a class="BuildLogLink" _locID="ViewLog">
            <xsl:attribute name="href"><xsl:value-of select="$agentLog" /></xsl:attribute>
            <xsl:text>View log</xsl:text>
          </a>
        </div>
      </xsl:if>
      <xsl:apply-templates select="Children/BuildInformationNode[@Type != 'ActivityOutput' and @Type != 'ActivityInput']" />      
    </div>
  </xsl:template>
  <xsl:template match="BuildInformationNode[@Type = 'BuildError' or @Type = 'BuildMessage' or @Type = 'BuildWarning']">
    <div class="BuildNode">
      <span>
        <xsl:choose>
          <xsl:when test="@Type = 'BuildMessage'">
          </xsl:when>
          <xsl:when test="@Type = 'BuildWarning'">
            <xsl:attribute name="class">BuildIcon icon icon-warning</xsl:attribute>
          </xsl:when>
          <xsl:when test="@Type = 'BuildError'">
            <xsl:attribute name="class">BuildIcon icon icon-tfs-build-status-failed</xsl:attribute>
          </xsl:when>
        </xsl:choose>
      </span>
      <span>
        <xsl:attribute name="class"><xsl:value-of select="@Type"/></xsl:attribute>
        <xsl:value-of select="Fields/InformationField[@Name = 'Message']/@Value" />
      </span>
    </div>
  </xsl:template>
  <xsl:template match="BuildInformationNode[@Type = 'ActivityInput' or @Type = 'ActivityOutput']">
    <div class="BuildProperties">
      <dl>
        <xsl:choose>
          <xsl:when test="@Type = 'ActivityInput'">
            <dt _locID="ActivityInput">Inputs</dt>
          </xsl:when>
          <xsl:otherwise>
            <dt _locID="ActivityOutput">Outputs</dt>
          </xsl:otherwise>
        </xsl:choose>
        <dd>
          <ul>
            <xsl:for-each select="Fields/InformationField">
              <li><xsl:value-of select="@Name"/>: <xsl:value-of select="@Value" /></li>
            </xsl:for-each>
          </ul>
        </dd>
      </dl>
    </div>
  </xsl:template>
</xsl:stylesheet>
