<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">

    <html>
      <head>
        <style type="text/css">

          body  {
          }

          table td
          {
          padding: 0;
          font-family: Tahoma;
          font-size: 12px;
          border-color: #a7a9ac;
          border-width: 0 0 0 0;
          border-style: solid;
          }

          table {border-collapse: collapse;}
          table.inner tr{height: 17px;}
          table.inner td{padding: 2px 3px 2px 3px;vertical-align: top;border: 1px solid #a7a9ac;}

          .reportHeader
          {
          font-size: 13px;
          border-width: 0px;
          padding: 0 0 15px 15px;
          vertical-align: bottom;
          height: 80px;
          }

          .reportHeader span
          {
          font-size: 14px;
          font-weight: bold;
          padding: 0;
          }

          .jobHeader_success{background-color: #00B050;color: White;font-weight: bold;font-size: 16px;height: 70px;vertical-align: bottom;padding: 0 0 15px 15px;}
          .jobHeader_error{background-color: #fb9895;color: White;font-weight: bold;font-size: 16px;height: 70px;vertical-align: bottom;padding: 0 0 15px 15px;}
          .jobHeader_warning{background-color: #ffd96c;color: White;font-weight: bold;font-size: 16px;height: 70px;vertical-align: bottom;padding: 0 0 15px 15px;}

          .jobDescription
          {
          margin-top: 5px;
          font-size: 12px;
          }

          .sessionDetails{height: 35px;background-color: #f3f4f4;font-size: 16px;vertical-align: middle;padding: 5px 0 0 15px;color: #626365;}
          .processObjects{height: 35px;background-color: #f3f4f4;font-size: 16px;vertical-align: middle;padding: 5px 0 0 15px;color: #626365;}

          .processObjectsHeader td
          {
          background-color: #e3e3e3;
          padding: 3px 3px 3px 3px;
          }

          .errorStatus
          {
          color: #FF0000;
          }

          .warningStatus
          {
          color: #FFC000;
          }

          .successStatus
          {
          color: #00B050;
          }

        </style>
      </head>
      <body>
        <table cellspacing="0" cellpadding="0" width="100%">
          <xsl:apply-templates select="/Document/JobSession"/>

            <tr>
            <td style="font-size:12px;color:#626365">Veeam Backup &amp; Replication <xsl:value-of select="/Document/Shell_App_Version" />
          </td>
          </tr>

        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="JobSession">
    <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
    <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

    <tr>
      <td style="border-width: 0;">
        <table cellspacing="0" cellpadding="0" width="100%">
          <tr>
            <td>
              <xsl:attribute name="class">
                <xsl:text>jobHeader_</xsl:text>
                <xsl:choose>
                  <xsl:when test="./Status = 'InProgress'">
                    <xsl:choose>
                      <xsl:when test="count(./TaskSession[Status = 'Error']) > 0">error</xsl:when>
                      <xsl:when test="count(./TaskSession[Status = 'Warning']) > 0">warning</xsl:when>
                      <xsl:otherwise>success</xsl:otherwise>
                    </xsl:choose>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select="translate(./Status, $uppercase, $smallcase)" />
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:attribute>

              <xsl:value-of select="./JobType"/>
              <xsl:text>: </xsl:text>
              <xsl:value-of select="./JobName"/>
              <div class="jobDescription">
                <xsl:value-of select="./JobDescription"/>
              </div>
            </td>
          </tr>
          <tr class="inner">
            <td class="sessionDetails" style="border-width: 1px">
              <xsl:text>Session Details</xsl:text>
            </td>
          </tr>
          <tr>
            <td style="border-width: 0;">
              <table cellspacing="0" cellpadding="0" border="0" class="inner" width="100%">
                <tr>
                  <td style="width:14%; border-top-width: 0px;">
                    <strong>
                      <xsl:text>Status</xsl:text>
                    </strong>
                  </td>
                  <td style="width:14%; border-top-width: 0px;">
                    <xsl:call-template name="setStatusSlassAttribute">
                      <xsl:with-param name="statusValue" select="string(./Status)"/>
                    </xsl:call-template>
                    <xsl:value-of select="./Status"/>
                  </td>
                  <td nowrap="nowrap" style="border-top-width: 0px;">
                    <strong>
                      <xsl:text>Start time</xsl:text>
                    </strong>
                  </td>
                  <td nowrap="nowrap" style="border-top-width: 0px;">
                    <xsl:value-of select="./StartTime"/>
                  </td>
                  <td style="border-width: 0 1px 1px 1px;width: 56%" rowspan="7">
                    <strong>
                      <xsl:text>Details</xsl:text>
                    </strong>
                    <br/>
                    <xsl:call-template name="setCellValue">
                      <xsl:with-param name="value">
                        <xsl:value-of select="./Details"/>
                      </xsl:with-param>
                    </xsl:call-template>
                  </td>
                </tr>
                <tr>
                  <td nowrap="nowrap">
                    <strong>
                      <xsl:text>Total tasks</xsl:text>
                    </strong>
                  </td>
                  <td>
                    <xsl:value-of select="./TotalTasks"/>
                  </td>
                  <td nowrap="nowrap">
                    <strong>
                      <xsl:text>End time</xsl:text>
                    </strong>
                  </td>
                  <td>
                    <xsl:value-of select="./EndTime"/>&#xA0;
                  </td>
                </tr>
                <tr>
                  <td nowrap="nowrap">
                    <strong>
                      <xsl:text>Processed tasks</xsl:text>
                    </strong>
                  </td>
                  <td>
                    <xsl:value-of select="./ProcessedTasks"/>
                  </td>
                  <td nowrap="nowrap">
                    <strong>
                      <xsl:text>Duration</xsl:text>
                    </strong>
                  </td>
                  <td>
                    <xsl:value-of select="./Duration"/>&#xA0;
                  </td>
                </tr>
                <tr>
                  <td nowrap="nowrap" style="width:14%">
                    <strong>
                      <xsl:text>Successful tasks</xsl:text>
                    </strong>
                  </td>
                  <td style="width:14%">
                    <xsl:value-of select="./SuccessfulTasks"/>
                  </td>
                  <td nowrap="nowrap">
                    <strong>
                      <xsl:text>Warning tasks</xsl:text>
                    </strong>
                  </td>
                  <td>
                    <xsl:value-of select="./WarningTasks"/>
                  </td>
                </tr>
                <tr>
                  <td nowrap="nowrap">
                    <strong>
                      <xsl:text>Failed tasks</xsl:text>
                    </strong>
                  </td>
                  <td>
                    <xsl:value-of select="./FailedTasks"/>
                  </td>
                  <td nowrap="nowrap">
                    <strong>
                      <xsl:text>Skipped tasks</xsl:text>
                    </strong>
                  </td>
                  <td>
                    <xsl:value-of select="./SkippedTasks"/>
                  </td>
                </tr>
                <tr>
                  <td nowrap="nowrap">
                    <strong>
                      <xsl:text>Progress</xsl:text>
                    </strong>
                  </td>
                  <td colspan="3">
                    <xsl:value-of select="./Progress"/>
                  </td>
                </tr>
              </table>
            </td>
          </tr>
          <tr>
            <td nowrap="nowrap" class="processObjects" style="border-width: 0 1 0 1">
              <xsl:text>Virtual machines status</xsl:text>
            </td>
          </tr>
          <tr>
            <td style="border-width: 0;">
              <table cellspacing="0" cellpadding="0" class="inner" width="100%">
                <tr class="processObjectsHeader" style="height: 23px">
                  <td nowrap="nowrap">
                    <strong>
                      <xsl:text>VM name</xsl:text>
                    </strong>
                  </td>
                  <td>
                    <strong>
                      <xsl:text>Status</xsl:text>
                    </strong>
                  </td>
                  <td nowrap="nowrap">
                    <strong>
                      <xsl:text>Start time</xsl:text>
                    </strong>
                  </td>
                  <td nowrap="nowrap">
                    <strong>
                      <xsl:text>End time</xsl:text>
                    </strong>
                  </td>
                  <td nowrap="nowrap">
                    <strong>
                      <xsl:text>Heartbeat test</xsl:text>
                    </strong>
                  </td>
                  <td nowrap="nowrap">
                    <strong>
                      <xsl:text>Ping test</xsl:text>
                    </strong>
                  </td>
                  <td nowrap="nowrap">
                    <strong>
                      <xsl:text>Custom script test</xsl:text>
                    </strong>
                  </td>
                  <td nowrap="nowrap">
                    <strong>
                      <xsl:text>Validation test</xsl:text>
                    </strong>
                  </td>
                </tr>
                <xsl:apply-templates select="./TaskSession"/>
              </table>
            </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr style="height: 10px;">
      <td style="border-width: 0;">
        &#xA0;
      </td>
    </tr>
  </xsl:template>
  <xsl:template match="TaskSession">
    <tr>
      <td>
        <xsl:value-of select="./ObjectName"/>
      </td>
      <td>
        <xsl:call-template name="setStatusSlassAttribute">
          <xsl:with-param name="statusValue" select="string(./Status)"/>
        </xsl:call-template>
        <xsl:value-of select="./Status"/>&#xA0;
      </td>
      <td nowrap="nowrap">
        <xsl:value-of select="./StartTime"/>
      </td>
      <td nowrap="nowrap">
        <xsl:value-of select="./EndTime"/>&#xA0;
      </td>
      <td nowrap="nowrap">
        <xsl:call-template name="setStatusSlassAttribute">
          <xsl:with-param name="statusValue" select="string(./HeartBeatTest)"/>
        </xsl:call-template>
        <xsl:value-of select="./HeartBeatTest"/>
      </td>
      <td>
        <xsl:call-template name="setStatusSlassAttribute">
          <xsl:with-param name="statusValue" select="string(./PingTest)"/>
        </xsl:call-template>
        <xsl:value-of select="./PingTest"/>
      </td>
      <td>
        <xsl:call-template name="setStatusSlassAttribute">
          <xsl:with-param name="statusValue" select="string(./CustomScriptTest)"/>
        </xsl:call-template>
        <xsl:call-template name="setCellValue">
          <xsl:with-param name="value">
            <xsl:value-of select="./CustomScriptTest"/>
          </xsl:with-param>
        </xsl:call-template>
      </td>
      <td>
        <xsl:call-template name="setStatusSlassAttribute">
          <xsl:with-param name="statusValue" select="string(./ValidationTest)"/>
        </xsl:call-template>
        <xsl:value-of select="./ValidationTest"/>
      </td>
    </tr>
  </xsl:template>

  <xsl:template name="setCellValue">
    <xsl:param name="value"/>
    <xsl:choose>
      <xsl:when test="string($value) = ''">
        &#xA0;
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="replace">
          <xsl:with-param name="string" select="$value"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="setStatusSlassAttribute">
    <xsl:param name="statusValue"/>
    <xsl:choose>
      <xsl:when test="string($statusValue) = 'Failed'">
        <xsl:attribute name="class">
          <xsl:text>errorStatus</xsl:text>
        </xsl:attribute>
      </xsl:when>
      <xsl:when test="string($statusValue) = 'Error'">
        <xsl:attribute name="class">
          <xsl:text>errorStatus</xsl:text>
        </xsl:attribute>
      </xsl:when>
      <xsl:when test="string($statusValue) = 'Warning'">
        <xsl:attribute name="class">
          <xsl:text>warningStatus</xsl:text>
        </xsl:attribute>
      </xsl:when>
      <xsl:when test="string($statusValue) = 'Success'">
        <xsl:attribute name="class">
          <xsl:text>successStatus</xsl:text>
        </xsl:attribute>
      </xsl:when>
    </xsl:choose>
  </xsl:template>


  <xsl:template name="replace">
    <xsl:param name="string"/>
    <xsl:choose>
      <xsl:when test="contains($string,'&#10;')">
        <xsl:value-of select="substring-before($string,'&#10;')"/>
        <br/>
        <xsl:call-template name="replace">
          <xsl:with-param name="string" select="substring-after($string,'&#10;')"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$string"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

