<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exsl="http://exslt.org/common">

  <xsl:template name="translateBR">
    <xsl:param name="data"/>
    <xsl:variable name="br">
      <xsl:text>&lt;br /&gt;</xsl:text>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="contains($data, $br)">
        <xsl:value-of select="substring-before($data, $br)"/>
        <br/>
        <xsl:call-template name="translateBR">
          <xsl:with-param name="data">
            <xsl:value-of select="substring-after($data, $br)"/>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$data"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:attribute-set name="row-column-span-style">
    <xsl:attribute name="rowspan">3</xsl:attribute>
    <xsl:attribute name="colspan">3</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="row-span-style">
    <xsl:attribute name="rowspan">3</xsl:attribute>
  </xsl:attribute-set>

  <xsl:template match="/Report">

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

    <html>
      <head>
        <META http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <style type="text/css">
          table{border-collapse: collapse; }
          table td{padding: 0px;font-family: Tahoma;font-size: 12px; min-width: 100; }
          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: none;padding: 0 0 15px 0px;vertical-align: bottom;height: 50px;}
          .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;}
          table.inner .sessionDetails{height: 35px;background-color: #f3f4f4;font-size: 16px;vertical-align: middle;padding: 5px 0 0 15px;color: #626365;}
          table.inner .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;border-top: none;}

          .status_success {background-color: #00B050;}
          .status_error {background-color: #fb9895;}
          .status_warning {background-color: #ffd96c;}

          .success{color: #00B050;}
          .warning{color: #FFC000;}
          .error{color: #FF0000;}
          .small_label{font-size: 10px;}
        </style>
      </head>
      <body>
        <table cellspacing="0" cellpadding="0" width="100%" border="0">
          <tr>
            <td style="border:none; padding: 0px;">
              <table cellspacing="0" cellpadding="0" width="100%" border="0">
                <tr>
                  <td style='width: 80%;border: none;'>
                    <xsl:attribute name="class">
                      <xsl:text>jobHeader_</xsl:text>
                      <xsl:value-of select="translate(./ResultInfo/Result, $uppercase, $smallcase)" />
                    </xsl:attribute>
                    <!--Backup name-->
                    Veeam Backup Validator
                    <!--Description-->
                    <div class="jobDescription">
                      <xsl:value-of select="./DateInfo" />
                    </div>
                  </td>
                  <td style='border: none;'>
                    <xsl:attribute name="class">
                      <xsl:text>jobHeader_</xsl:text>
                      <xsl:value-of select="translate(./ResultInfo/Result, $uppercase, $smallcase)" />
                    </xsl:attribute>
                    <!--Result-->
                    <xsl:value-of select="./ResultInfo/Result" />
                    <!--Num of VMs Processed-->
                    <div class="jobDescription">
                      <xsl:variable name="filesCount" select="./Statistics/Parameter[@Name = 'Backup files count:']"></xsl:variable>
                      <xsl:variable name="vmCount" select="./Statistics/Parameter[@Name = 'VM count:']"></xsl:variable>
                      <xsl:choose>
                        <xsl:when test="$filesCount or $vmCount">
                          <xsl:value-of select="$filesCount"/>
                          <xsl:value-of select="$vmCount"/>
                        </xsl:when>
                        <xsl:otherwise>
                          0
                        </xsl:otherwise>
                      </xsl:choose>
                      <xsl:choose>
                        <xsl:when test="$filesCount">
                          Files
                        </xsl:when>
                        <xsl:otherwise>
                          VMs
                        </xsl:otherwise>
                      </xsl:choose>
                      processed
                    </div>
                  </td>
                </tr>
                <tr>
                  <td colspan="2" style="border: none; padding: 0px;">
                    <!--Content table-->
                    <table width="100%" cellspacing="0" cellpadding="0" class="inner" border="0" style='margin: 0px;'>
                      <!--Parameters-->
                      <xsl:if test='./Parameters'>
                        <tr>
                          <td colspan="9" class="sessionDetails" style='border-bottom: none;'>
                            <span>
                              Parameters
                            </span>
                          </td>
                        </tr>
                        <!--Parameter names-->
                        <xsl:variable name="vmNames" select="./Parameters/Parameter[@Name = 'VM name:']"></xsl:variable>
                        <xsl:variable name="files" select="./Parameters/Parameter[@Name = 'File:']"></xsl:variable>
                        <tr>
                          <xsl:for-each select="./Parameters/Parameter">
                            <xsl:if test="not(@Name = 'Report:' or @Name = 'Silence:' or  @Name = 'VM name:' or @Name = 'File:')">
                              <td nowrap="" >
                                <span>
                                  <b>
                                    <xsl:value-of select="@Name" />
                                  </b>
                                </span>
                              </td>
                            </xsl:if>
                          </xsl:for-each>
                          <!--                          Special VM name column-->
                          <xsl:if test="$vmNames">
                            <td nowrap="">
                              <span>
                                <b>
                                  VM name:
                                </b>
                              </span>
                            </td>
                          </xsl:if>
                          <xsl:if test="$files">
                            <td nowrap="">
                              <span>
                                <b>
                                  File name:
                                </b>
                              </span>
                            </td>
                            <!--  Space reserver-->
                            <td nowrap="" colspan ="9" rowspan="2">
                            </td>
                          </xsl:if>
                        </tr>
                        <!--Parameter values-->
                        <tr>
                          <xsl:for-each select="./Parameters/Parameter">
                            <xsl:if test="not(@Name = 'Report:' or @Name = 'Silence:' or @Name = 'VM name:' or @Name = 'File:')">
                              <td nowrap="">
                                <xsl:value-of select="." />
                              </td>
                            </xsl:if>
                          </xsl:for-each>
                          <!-- VM names-->
                          <xsl:if test="$vmNames">
                            <td nowrap="">
                              <xsl:for-each select="$vmNames">
                                <xsl:value-of select="." />
                                <br/>
                              </xsl:for-each>
                            </td>
                          </xsl:if>
                          <xsl:if test="$files">
                            <td nowrap="">
                              <xsl:for-each select="$files">
                                <xsl:value-of select="." />
                                <br/>
                              </xsl:for-each>
                            </td>
                          </xsl:if>
                        </tr>
                      </xsl:if>
                      <!--VM tables-->
                      <xsl:for-each select="./VMProcessing">
                        <xsl:variable name="vmName" select="./VirtualMachine/Parameter[@Name = 'VM name:']" />
                        <xsl:variable name="fileName" select="./FileSummary/Parameter[@Name = 'Storage path:']"/>
                        <!--VM table-->
                        <xsl:if test="./VirtualMachine">
                          <tr>
                            <td colspan="9" class="sessionDetails" style='border-bottom: none;'>
                              <span>
                                <xsl:value-of select="$vmName" /> info
                              </span>
                            </td>
                          </tr>
                          <!--VM parameter names-->
                          <tr>
                            <xsl:for-each select="./VirtualMachine/Parameter">
                              <td nowrap="">
                                <span>
                                  <b>
                                    <xsl:value-of select="@Name" />
                                  </b>
                                </span>
                              </td>
                            </xsl:for-each>
                            <td rowspan="2" colspan="2">
                            </td>
                          </tr>
                          <!--VM parameter values-->
                          <tr>
                            <xsl:for-each select="./VirtualMachine/Parameter">
                              <td nowrap="">
                                <xsl:value-of select="." />
                              </td>
                            </xsl:for-each>
                          </tr>
                        </xsl:if>
                        <!--  VM summary table-->
                        <xsl:if test="./VMSummary or ./FileSummary">
                          <tr>
                            <td colspan="9" class="sessionDetails" style='border-bottom: none;'>
                              <span>
                                <xsl:choose>
                                  <xsl:when test="./FileSummary">
                                    <xsl:value-of select="$fileName" /> summary
                                  </xsl:when>
                                  <xsl:otherwise>
                                    <xsl:value-of select="$vmName" /> summary
                                  </xsl:otherwise>
                                </xsl:choose>
                              </span>
                            </td>
                          </tr>
                          <!--Summary parameters-->
                          <xsl:variable name="count" select="-1"/>
                          <xsl:for-each select="./VMSummary/Parameter">
                            <tr>
                              <xsl:choose>
                                <!-- If it is file summary then write File $count -->
                                <xsl:when test="@Name = $vmName">
                                  <td nowrap="">
                                    <b>
                                      File <xsl:value-of select="$count + position()"/>:
                                    </b>
                                  </td>
                                </xsl:when>
                                <xsl:otherwise>
                                  <td nowrap="">
                                    <span>
                                      <b>
                                        <xsl:value-of select="@Name" />
                                      </b>
                                    </span>
                                  </td>
                                </xsl:otherwise>
                              </xsl:choose>
                              <!--  Summary parameter value-->
                              <td colspan="9">
                                <xsl:value-of select="." />
                              </td>
                            </tr>
                          </xsl:for-each>
                          <xsl:for-each select="./FileSummary/Parameter">
                            <tr>
                              <xsl:choose>
                                <!-- If it is file summary then write File $count -->
                                <xsl:when test="@Name = $fileName">
                                  <td nowrap="">
                                    <b>
                                      File <xsl:value-of select="$count + position()"/>:
                                    </b>
                                  </td>
                                </xsl:when>
                                <xsl:otherwise>
                                  <td nowrap="">
                                    <span>
                                      <b>
                                        <xsl:value-of select="@Name" />
                                      </b>
                                    </span>
                                  </td>
                                </xsl:otherwise>
                              </xsl:choose>
                              <!--  Summary parameter value-->
                              <td colspan="9">
                                <xsl:value-of select="." />
                              </td>
                            </tr>
                          </xsl:for-each>
                          <!--  Progress tables-->
                          <xsl:if test="./Progress">
                            <tr>
                              <td colspan="9" class="sessionDetails" style='border-bottom: none;'>
                                <span>
                                  Validation progress
                                </span>
                              </td>
                            </tr>
                          </xsl:if>
                          <xsl:for-each select="./Progress">
                            <tr>
                              <td nowrap="">
                                <span>
                                  <!--  FileName-->
                                  <b >
                                    <xsl:value-of select="./FileName" />
                                  </b>
                                </span>
                              </td>
                              <td colspan="9">
                                <span>
                                  <!-- Percentage-->
                                  <xsl:value-of select="./Percentage" />%
                                </span>
                              </td>
                            </tr>
                          </xsl:for-each>
                        </xsl:if>
                      </xsl:for-each>
                      <!--                        Statistics table-->
                      <xsl:choose>
                        <xsl:when test="./Statistics">
                          <tr>
                            <td colspan="12" class="sessionDetails" style='border-bottom: none;'>
                              <span>
                                Statistics
                              </span>
                            </td>
                          </tr>
                          <tr class="processObjectsHeader" style="height: 23px">
                            <xsl:for-each select="./Statistics/Parameter">
                              <td nowrap="">
                                <span>
                                  <b>
                                    <xsl:value-of select="@Name" />
                                  </b>
                                </span>
                              </td>
                            </xsl:for-each>
                            <td colspan="9" width="50%">
                              <b>
                                Details:
                              </b>
                            </td>
                          </tr>
                          <!--Parameter values-->
                          <tr>
                            <xsl:for-each select="./Statistics/Parameter">
                              <td nowrap="">
                                <xsl:value-of select="." />
                              </td>
                            </xsl:for-each>
                            <td nowrap="" colspan="9">
                              <span>
                                <xsl:attribute name="class">
                                  <xsl:value-of select="translate(./ResultInfo/Result, $uppercase, $smallcase)" />
                                </xsl:attribute>
                                <xsl:for-each select="./ResultInfo/Info">
                                  <xsl:value-of select="."/>
                                  <br/>
                                </xsl:for-each>
                                <xsl:for-each select="./ResultInfo/Error">
                                  <xsl:value-of select="."/>
                                  <br/>
                                </xsl:for-each>
                              </span>
                            </td>
                          </tr>
                        </xsl:when>
                        <!--                     If Statistics node is absent (it means error btw)-->
                        <xsl:otherwise>
                          <tr>
                            <td colspan="9" width="50%">
                              <b>
                                Details:
                              </b>
                            </td>
                          </tr>
                          <tr>
                            <td nowrap="" colspan="9" width="50%">
                              <span>
                                <xsl:attribute name="class">
                                  <xsl:value-of select="translate(./ResultInfo/Result, $uppercase, $smallcase)" />
                                </xsl:attribute>
                                <xsl:for-each select="./ResultInfo/Error">
                                  <xsl:value-of select="."/>
                                </xsl:for-each>
                              </span>
                            </td>
                          </tr>
                        </xsl:otherwise>
                      </xsl:choose>
                    </table>
                  </td>
                </tr>
                <tr>
                  <!--                  Signature-->
                  <td style="font-size:12px;color:#626365">
                    <br/>
                    <xsl:value-of select="./Info" />
                  </td>
                </tr>
              </table>
            </td>
          </tr>
          <tr>
            <td>&#160;</td>
          </tr>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
