<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:import href="results_utils.xsl"/>
  
  <xsl:param name="league"/>
  <xsl:param name="team"/>
  <xsl:param name="player"/>
  <xsl:param name="year"/>
  
  <xsl:key name="players" match="/chess/players/player" use="@ref"/>
  
  <xsl:template match="/">
    <xsl:apply-templates select="chess/players"/>
    <xsl:if test="($player or $league or $team) and not($team and not($player) and not($league))">
      <div style="clear:left;">
        <h2>
          <xsl:value-of select="key('players',$player)"/>
        </h2>
        <table class="tablesorter">
          <thead>
            <tr>
              <xsl:if test="not($league)">
                <th>League</th>
              </xsl:if>
              <th>Date</th>
              <xsl:if test="not($league) or chess/league/@name=$league">
                <th>Home</th>
                <th>Away</th>
                <th>Board</th>
              </xsl:if>
              <th>White</th>
              <th width="55px">Result</th>
              <th>Black</th>
              <xsl:if test="$player">
                <th width="55px">Opp. Grade</th>
              </xsl:if>
            </tr>
          </thead>
          <tbody>
            <xsl:apply-templates select="chess/league|chess/competition"/>
          </tbody>
        </table>
      </div>
    </xsl:if>
    <xsl:if test="$team and not($player) and not($league)">
      <table>
        <tr>
          <xsl:for-each select="chess/league[match/home/team=$team or starts-with(match/home/team, $team) or starts-with(match/away/team, $team)]">
            <td>
              <xsl:element name="a">
                <xsl:attribute name="href"><xsl:value-of select="$team_url"/><xsl:value-of select="$team"/>&amp;league=<xsl:value-of select="@name"/></xsl:attribute>
                <xsl:attribute name="title">
                  <xsl:text>Show all results for </xsl:text>
                  <xsl:value-of select="$team"/>
                  <xsl:text> in </xsl:text>
                  <xsl:value-of select="@name"/>
                </xsl:attribute>
                <xsl:value-of select="@name"/>
              </xsl:element>
            </td>
          </xsl:for-each>
        </tr>
      </table>
    </xsl:if>
  </xsl:template>
  <!-- Table of players with their win/lose/draw results -->
  <xsl:template match="chess/players">
    <div style="clear:left;">
      <h2>Player Records
      <xsl:choose><xsl:when test="not($team)"/><xsl:otherwise>
      for <xsl:value-of select="$team"/>
      </xsl:otherwise></xsl:choose>


      <xsl:choose><xsl:when test="$league">
      in <xsl:value-of select="$league"/>
      </xsl:when><xsl:otherwise>
      (all games)
      </xsl:otherwise></xsl:choose>

      
      <xsl:choose><xsl:when test="not($year)"/><xsl:otherwise><span style="font-size:50%"><xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="$team_url"/><xsl:value-of select="$team"/><xsl:text>&amp;year=</xsl:text><xsl:value-of select="$year"/></xsl:attribute> (<xsl:value-of select="$year"/>)
      </xsl:element></span></xsl:otherwise></xsl:choose>
      </h2>
      <table id="players" class="tablesorter">
        <thead>
          <tr>
            <th>Name</th>
            <th>Club</th>
            <th width="55px">Grade</th>
            <th width="55px" class="digit">Played</th>
            <th width="55px">Won</th>
            <th width="55px">Drawn</th>
            <th width="55px">Lost</th>
            <th width="55px">Grading Performance</th>
          </tr>
        </thead>
        <xsl:choose>
          <xsl:when test="$player and not($league)">
            <tbody>
              <xsl:for-each select="//league | //competition">
                <xsl:call-template name="player-summary">
                  <xsl:with-param name="type" select="'filtered'"/>
                  <xsl:with-param name="player" select="key('players',$player)"/>
                  <xsl:with-param name="league" select="current()/@name"/>
                  <xsl:with-param name="team" select="$team"/>
                </xsl:call-template>
              </xsl:for-each>
            </tbody>
            <tfoot>
              <xsl:call-template name="player-summary">
                <xsl:with-param name="player" select="player[@ref=$player]"/>
                <xsl:with-param name="team" select="$team"/>
              </xsl:call-template>
            </tfoot>
          </xsl:when>
          <xsl:otherwise>
            <tbody>
              <xsl:for-each select="player[@ref=$player or (not($player) and (not($team) or @team=$team))]">
                <xsl:call-template name="player-summary">
                  <xsl:with-param name="player" select="current()"/>
                  <xsl:with-param name="league" select="$league"/>
                  <xsl:with-param name="team" select="$team"/>
                </xsl:call-template>
              </xsl:for-each>
            </tbody>
          </xsl:otherwise>
        </xsl:choose>
      </table>
    </div>
  </xsl:template>
  
  <!-- Individual Game Results -->
  <xsl:template match="chess/league|chess/competition">
    <xsl:for-each select="match[(not($league) or ../@name=$league) and (not($team) or starts-with(home/team, $team) or starts-with(away/team, $team))]">
      <xsl:for-each select="gameinfo[opponents/white/player-ref/@ref=$player and not(opponents/black/player-ref/@ref='Default')] |                                          gameinfo[opponents/black/player-ref/@ref=$player and not(opponents/white/player-ref/@ref='Default')] |                                                                                      gameinfo[not($player) and ../../@name=$league]">
        <tr>
          <xsl:if test="not($league)">
            <td>
              <xsl:call-template name="league-format">
                <xsl:with-param name="ref" select="$player"/>
                <xsl:with-param name="league" select="../../@name"/>
              </xsl:call-template>
            </td>
          </xsl:if>
          <td>
            <xsl:call-template name="matchdate-format">
              <xsl:with-param name="match" select=".."/>
            </xsl:call-template>
          </td>
          <xsl:if test="not($league) or (../../@name=$league and local-name(../..) = 'league')">
            <td>
              <xsl:element name="a">
                <xsl:attribute name="href">
                  <xsl:value-of select="$team_url"/>
                  <xsl:value-of select="../home/team"/>
                </xsl:attribute>
                <xsl:value-of select="../home/team"/>
              </xsl:element>
            </td>
            <td>
              <xsl:element name="a">
                <xsl:attribute name="href">
                  <xsl:value-of select="$team_url"/>
                  <xsl:value-of select="../away/team"/>
                </xsl:attribute>
                <xsl:value-of select="../away/team"/>
              </xsl:element>
            </td>
            <td style="text-align:center;">
              <xsl:value-of select="@board"/>
            </td>
          </xsl:if>
          <td>
            <xsl:call-template name="player-format">
              <xsl:with-param name="ref" select="opponents/white/player-ref/@ref"/>
            </xsl:call-template>
          </td>
            <!-- Game Result -->
          <xsl:choose>
            <xsl:when test="opponents/white/player-ref/@ref=$player or (not($player) and ((../home/team=$team and @board mod 2 = 0) or (../away/team=$team and @board mod 2 = 1)))">
              <xsl:choose>
                <xsl:when test="result/@res='1-0'">
                  <td style="text-align:center; background-color:#98FB98;">
                    <xsl:call-template name="string-format">
                      <xsl:with-param name="text" select="result/@res"/>
                    </xsl:call-template>
                  </td>
                </xsl:when>
                <xsl:when test="result/@res='0-1'">
                  <td style="text-align:center; background-color:#FB9898;">
                    <xsl:call-template name="string-format">
                      <xsl:with-param name="text" select="result/@res"/>
                    </xsl:call-template>
                  </td>
                </xsl:when>
                <xsl:otherwise>
                  <td style="text-align:center;">
                    <xsl:call-template name="string-format">
                      <xsl:with-param name="text" select="result/@res"/>
                    </xsl:call-template>
                  </td>
                </xsl:otherwise>
              </xsl:choose>
              <td>
                <xsl:call-template name="player-format">
                  <xsl:with-param name="ref" select="opponents/black/player-ref/@ref"/>
                </xsl:call-template>
              </td>
              <xsl:if test="$player">
              <td>
                <xsl:call-template name="grade-format">
                  <xsl:with-param name="ref" select="key('players',opponents/black/player-ref/@ref)/@ref"/>
                </xsl:call-template>
              </td>
              </xsl:if>
            </xsl:when>
            <xsl:when test="opponents/black/player-ref/@ref=$player or (not($player) and ((../away/team=$team and @board mod 2 = 0) or (../home/team=$team and @board mod 2 = 1)))  ">
              <xsl:choose>
                <xsl:when test="result/@res='1-0'">
                  <td style="text-align:center; background-color:#FB9898;">
                    <xsl:call-template name="string-format">
                      <xsl:with-param name="text" select="result/@res"/>
                    </xsl:call-template>
                  </td>
                </xsl:when>
                <xsl:when test="result/@res='0-1'">
                  <td style="text-align:center; background-color:#98FB98;">
                    <xsl:call-template name="string-format">
                      <xsl:with-param name="text" select="result/@res"/>
                    </xsl:call-template>
                  </td>
                </xsl:when>
                <xsl:otherwise>
                  <td style="text-align:center;">
                    <xsl:call-template name="string-format">
                      <xsl:with-param name="text" select="result/@res"/>
                    </xsl:call-template>
                  </td>
                </xsl:otherwise>
              </xsl:choose>
              <td>
                <xsl:call-template name="player-format">
                  <xsl:with-param name="ref" select="opponents/black/player-ref/@ref"/>
                </xsl:call-template>
              </td>
               <xsl:if test="$player">
              <td>
                <xsl:call-template name="grade-format">
                  <xsl:with-param name="ref" select="key('players',opponents/white/player-ref/@ref)/@ref"/>
                </xsl:call-template>
              </td>
              </xsl:if>
            </xsl:when>
            <xsl:otherwise>
              <td style="text-align:center;">
                <xsl:call-template name="string-format">
                  <xsl:with-param name="text" select="result/@res"/>
                </xsl:call-template>
              </td>
              <td>
                <xsl:call-template name="player-format">
                  <xsl:with-param name="ref" select="opponents/black/player-ref/@ref"/>
                </xsl:call-template>
              </td>
            </xsl:otherwise>
          </xsl:choose>
        </tr>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

