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

  <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:template name="FreeSpaceStateStyle">
    <xsl:param name="data"/>
    <xsl:param name="advStyle"/>

    <xsl:attribute name="style">
      <xsl:value-of select="$advStyle"/>
      <xsl:choose>
        <xsl:when test="$data = 'ok'">
          <xsl:text>background-color: #ffffff;</xsl:text>
        </xsl:when>
        <xsl:when test="$data = 'critical'">
          <xsl:text>background-color: #fb9895;</xsl:text>
        </xsl:when>
        <xsl:when test="$data = 'warning'">
          <xsl:text>background-color: #ffd96c;</xsl:text>
        </xsl:when>
      </xsl:choose>
    </xsl:attribute>
  </xsl:template>

  <xsl:template name="InfrastructureState">
    <xsl:param name="data"/>
    <xsl:param name="advStyle"/>

    <xsl:attribute name="style">
      <xsl:value-of select="$advStyle"/>
      <xsl:choose>
        <xsl:when test="$data = 'unknown'">
          <xsl:text>color: #000000;</xsl:text>
        </xsl:when>
        <xsl:when test="$data = 'normal'">
          <xsl:text>color: #00B050;</xsl:text>
        </xsl:when>
        <xsl:when test="$data = 'warning'">
          <xsl:text>color: #F59D56;</xsl:text>
        </xsl:when>
        <xsl:when test="$data = 'critical'">
          <xsl:text>color: #FF0000;</xsl:text>
        </xsl:when>
      </xsl:choose>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="/">

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

    <html>
      <head>
      </head>
      <body>

      <div style="font-family: Tahoma; font-size: 18px; padding-top: 5px;">Infrastructure status:</div>
      <div>
          <xsl:call-template name="InfrastructureState">
            <xsl:with-param name="advStyle">
              <xsl:text>font-family: Tahoma; font-size: 15px; padding-top: 5px;</xsl:text>
            </xsl:with-param>
            <xsl:with-param name="data">
              <xsl:choose>
                <xsl:when test="/Document/InfrastructureStatus/@State = 'Normal'">normal</xsl:when>
                <xsl:when test="/Document/InfrastructureStatus/@State = 'Warning'">warning</xsl:when>
                <xsl:when test="/Document/InfrastructureStatus/@State = 'Critical'">critical</xsl:when>
                <xsl:otherwise>unknown</xsl:otherwise>
              </xsl:choose>
            </xsl:with-param>
          </xsl:call-template>
            
          <xsl:choose>
            <xsl:when test="/Document/InfrastructureStatus/@State = 'Unknown'">
              N/A. Cloud Connect server is unavailable
            </xsl:when>
            <xsl:when test="/Document/InfrastructureStatus/@State = 'Normal'">
              OK
            </xsl:when>
            <xsl:when test="/Document/InfrastructureStatus/@State = 'Warning'">
              Reaching capacity. Please do not add new tenants into this Veeam Cloud Connect infrastructure.
            </xsl:when>
            <xsl:when test="/Document/InfrastructureStatus/@State = 'Critical'">
              Out of capacity. Please migrate some of the existing tenants into a different Veeam Cloud Connect infrastructure.
            </xsl:when>
          </xsl:choose>
        </div>

        <xsl:choose>
          <xsl:when test="/Document/LicenseInfo/@ProductLicensingStatus = 'Exceeded'">
            <p style="font-family: Tahoma; font-size: 12px; padding-top: 5px; padding-bottom:5px;">
              <xsl:value-of select="/Document/LicenseInfo/@MessageExceeded" />
            </p>
          </xsl:when>
          <xsl:when test="/Document/LicenseInfo/@ProductLicensingStatus = 'ExceededWithOverhead'">
            <p style="font-family: Tahoma; font-size: 12px; padding-top: 5px; padding-bottom:5px;">
              <xsl:value-of select="/Document/LicenseInfo/@MessageExceededWithOverhead" />
            </p>
          </xsl:when>
          <xsl:when test="/Document/LicenseInfo/@ProductLicensingStatus = 'Expired'">
              <p style="font-family: Tahoma; font-size: 12px; padding-top: 5px; padding-bottom:5px;">Your Cloud Connect license has expired</p>
          </xsl:when>
          <xsl:when test="/Document/LicenseInfo/@ProductLicensingStatus = 'Grace'">
              <p style="font-family: Tahoma; font-size: 12px; padding-top: 5px; padding-bottom:5px;">
                  Your Cloud Connect license has expired, <xsl:value-of select="/Document/LicenseInfo/@ProductGraceRemainingDays" /> days of grace period remaining
              </p>
          </xsl:when>
          <xsl:otherwise>
            <xsl:if test="/Document/LicenseInfo/@ProductFullRemainingDays">
              <p style="font-family: Tahoma; font-size: 12px; padding-top: 5px; padding-bottom:5px;">
                Your Cloud Connect license expires in <xsl:value-of select="/Document/LicenseInfo/@ProductFullRemainingDays" /> days
              </p>
            </xsl:if>
          </xsl:otherwise>
        </xsl:choose>

        <!-- START BACKUP -->

        <p style="font-family: Tahoma; font-size: 18px; padding-top: 5px; padding-bottom:5px;">Backup</p>
        
        <table cellspacing="0" cellpadding="0" width="100%" border="0" style="border-collapse: collapse;">
          <tr style="background-color: #a7a7a7; font-weight:bold;height: 17px;">
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              User
            </td>
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              Number of VM
            </td>
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              Repository Name
            </td>
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              Repository
            </td>
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              Total quota
            </td>
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              Used space
            </td>
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              Free space
            </td>
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              Last active
            </td>
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              Expiration date
            </td>
          </tr>
          <xsl:for-each select="/Document/Backup/TenantQuota">
            <tr>
              <xsl:call-template name="FreeSpaceStateStyle">
                <xsl:with-param name="advStyle">
                  <xsl:text>height: 17px;</xsl:text>
                </xsl:with-param>
                <xsl:with-param name="data">
                  <xsl:choose>
                    <xsl:when test="./@FreeSpaceState = 'OK'">ok</xsl:when>
                    <xsl:when test="./@FreeSpaceState = 'Warning'">warning</xsl:when>
                    <xsl:otherwise>critical</xsl:otherwise>
                  </xsl:choose>
                </xsl:with-param>
              </xsl:call-template>
              <td style="width:150px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@User" />
              </td>
              <td style="width:150px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@VmNumber" />
              </td>
              <td style="width:200px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@RepositoryName" />
              </td>
              <td style="width:200px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@Repository" />
              </td>
              <td style="width:85px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@TotalQuota" />
              </td>
              <td style="width:85px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@UsedSpace" />
              </td>
              <td style="width:120px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@FreeSpace" />
              </td>
              <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@LastActive" />
              </td>
              <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@ExpirationDate" />
              </td>
            </tr>
          </xsl:for-each>
          <tr style="font-weight:bold;height: 17px;">
            <td style="width:150px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              TOTAL
            </td>
            <td style="width:150px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              <xsl:value-of select="/Document/Backup/@TotalVmNumber" />
            </td>
          </tr>
        </table>

        <!-- END BACKUP -->

        <!-- START REPLICATION -->

        <p style="font-family: Tahoma; font-size: 18px; padding-top: 5px; padding-bottom:5px;">Replication</p>
        
        <table cellspacing="0" cellpadding="0" width="100%" border="0" style="border-collapse: collapse;">
          <tr style="background-color: #a7a7a7; font-weight:bold;height: 17px;">
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              User
            </td>
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              Number of VM
            </td>
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              Hardware Plan
            </td>
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              Memory
            </td>
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              CPU
            </td>
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              Storage
            </td>
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              Last active
            </td>
            <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              Expiration date
            </td>
          </tr>
          <xsl:for-each select="/Document/Replica/TenantQuota">
            <tr>
              <xsl:call-template name="FreeSpaceStateStyle">
                <xsl:with-param name="advStyle">
                  <xsl:text>height: 17px;</xsl:text>
                </xsl:with-param>
                <xsl:with-param name="data">
                  <xsl:choose>
                    <xsl:when test="./@State = 'OK'">ok</xsl:when>
                    <xsl:when test="./@State = 'Warning'">warning</xsl:when>
                    <xsl:otherwise>critical</xsl:otherwise>
                  </xsl:choose>
                </xsl:with-param>
              </xsl:call-template>
              <td style="width:150px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@User" />
              </td>
              <td style="width:150px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@VmNumber" />
              </td>
              <td style="width:200px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@PlanName" />
              </td>
              <td style="width:200px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@Memory" />
              </td>
              <td style="width:85px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@CPU" />
              </td>
              <td style="width:85px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@Storage" />
              </td>
              <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@LastActive" />
              </td>
              <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="./@ExpirationDate" />
              </td>
            </tr>
          </xsl:for-each>
          <tr style="font-weight:bold;height: 17px;">
            <td style="width:150px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              TOTAL
            </td>
            <td style="width:150px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
              <xsl:value-of select="/Document/Replica/@TotalVmNumber" />
            </td>
          </tr>

        </table>

        <!-- END REPLICATION -->

        <!-- START AGENTS BACKUP -->
        
        <xsl:if test="/Document/Agents">
          <p style="font-family: Tahoma; font-size: 18px; padding-top: 5px; padding-bottom:5px;">Agent</p>

          <table cellspacing="0" cellpadding="0" width="100%" border="0" style="border-collapse: collapse;">
            <tr style="background-color: #a7a7a7; font-weight:bold;height: 17px;">
              <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                User
              </td>
              <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                Workstation
              </td>
              <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                Server
              </td>
              <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                Repository Name
              </td>
              <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                Repository
              </td>
              <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                Total quota
              </td>
              <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                Used space
              </td>
              <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                Free space
              </td>
              <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                Last active
              </td>
              <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                Expiration date
              </td>
            </tr>
            <xsl:for-each select="/Document/Agents/TenantQuota">
              <tr>
                <xsl:call-template name="FreeSpaceStateStyle">
                  <xsl:with-param name="advStyle">
                    <xsl:text>height: 17px;</xsl:text>
                  </xsl:with-param>
                  <xsl:with-param name="data">
                    <xsl:choose>
                      <xsl:when test="./@FreeSpaceState = 'OK'">ok</xsl:when>
                      <xsl:when test="./@FreeSpaceState = 'Warning'">warning</xsl:when>
                      <xsl:otherwise>critical</xsl:otherwise>
                    </xsl:choose>
                  </xsl:with-param>
                </xsl:call-template>
                <td style="width:150px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                  <xsl:value-of select="./@User" />
                </td>
                <td style="width:150px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                  <xsl:value-of select="./@Workstation" />
                </td>
                <td style="width:150px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                  <xsl:value-of select="./@Server" />
                </td>
                <td style="width:200px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                  <xsl:value-of select="./@RepositoryName" />
                </td>
                <td style="width:200px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                  <xsl:value-of select="./@Repository" />
                </td>
                <td style="width:85px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                  <xsl:value-of select="./@TotalQuota" />
                </td>
                <td style="width:85px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                  <xsl:value-of select="./@UsedSpace" />
                </td>
                <td style="width:120px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                  <xsl:value-of select="./@FreeSpace" />
                </td>
                <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                  <xsl:value-of select="./@LastActive" />
                </td>
                <td style="font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                  <xsl:value-of select="./@ExpirationDate" />
                </td>
              </tr>
            </xsl:for-each>
            <tr style="font-weight:bold;height: 17px;">
              <td style="width:150px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                TOTAL
              </td>
              <td style="width:150px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="/Document/Agents/@TotalWorkstationNumber" />
              </td>
              <td style="width:150px;font-family: Tahoma; font-size: 12px; white-space:nowrap; border:1px solid #a7a7a7; padding:2px;">
                <xsl:value-of select="/Document/Agents/@TotalServerNumber" />
              </td>
            </tr>
          </table>
        </xsl:if>

        <!-- END AGENTS BACKUP -->

        <table>
          <xsl:choose>
            <xsl:when test="/Document/WriteSignature = 'true'">
              <tr style="padding-top:20px;height: 17px;">
                <td style="font-size:12px;color:#626365; border:0px;font-family: Tahoma; white-space:nowrap; padding:2px;">
                  Veeam Backup &amp; Replication <xsl:value-of select="/Document/Shell_App_Version" />
                </td>
              </tr>
            </xsl:when>
          </xsl:choose>
        </table>
      </body>
    </html>

  </xsl:template>

</xsl:stylesheet>
