<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:georss="http://www.georss.org/georss">
  <xsl:output method="html" indent="yes" omit-xml-declaration="no" encoding="UTF-8"/>
  <xsl:template match="/">
    <xsl:variable name="isParent">
      <xsl:choose>
        <xsl:when test="atom:feed/atom:entry[1]/atom:link[@type='application/atom+xml']/@href">
          <xsl:value-of select="'true'"></xsl:value-of>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="'false'"></xsl:value-of>
        </xsl:otherwise>
      </xsl:choose>

    </xsl:variable>
    <html>
      <head>
        <title>
          <xsl:value-of select="atom:feed/atom:title"/>
        </title>
        <link href="https://geodata.gov.md/atom/assets/images/logos/favicon.ico" rel="icon" />
        <link rel="stylesheet" type="text/css" href="https://geodata.gov.md/atom/assets/deps.css"/>
        <link rel="stylesheet" type="text/css" href="https://geodata.gov.md/atom/style/style.css"/>
        <link rel="stylesheet" type="text/css" href="https://geodata.gov.md/atom/assets/adsc/styles.css"/>
        <script type="text/javascript" src="https://geodata.gov.md/atom/assets/deps.js"/>
        <script type="text/javascript" src="https://geodata.gov.md/atom/assets/adsc/main.js"/>
        <script type="text/javascript" src="https://geodata.gov.md/atom/assets/adsc/polyfills.js"/>
        <script type="text/javascript" src="https://geodata.gov.md/atom/assets/adsc/runtime.js"/>
      </head>

      <body>
        <header id="banner" role="banner">
          <div id="heading">
            <nav role="navigation" class="full-width navbar navbar-default">
              <div class="content">
                <a class="navbar-brand" href="https://knowledge-base.inspire.ec.europa.eu/index_en">
                  <img src="https://geodata.gov.md/atom/assets/images/INSPIRE_logo.png" alt="INSPIRE ATOM"/>
                </a>
              </div>
            </nav>
          </div>
        </header>

        <div class="content">
          <div id="feedTitleContainer" class="entry">
            <h2 id="feedTitleText">
              <xsl:choose>
                <xsl:when test="$isParent='true'">
                  <xsl:text>Service Feed - </xsl:text>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:text>Data Feed - </xsl:text>
                </xsl:otherwise>
              </xsl:choose>
              <xsl:value-of select="atom:feed/atom:title"/>
            </h2>
            <p id="feedSubtitleText">
              <xsl:value-of select="atom:feed/atom:subtitle"/>
            </p>
            <table>
              <xsl:if test="$isParent = 'false'">
                <tr>
                  <td>Main page</td>
                  <td>
                    <a href="{atom:feed/atom:link[@rel='up']/@href}">
                      Parent
                    </a>
                  </td>
                </tr>
              </xsl:if>
              <xsl:if test="$isParent = 'true'">
                <xsl:if test="atom:feed/atom:author">
                  <tr>
                    <td>Service Provider</td>
                    <td>
                      <a href="mailto:{atom:feed/atom:author/atom:email}">
                        <xsl:value-of select="atom:feed/atom:author/atom:name" disable-output-escaping="yes"/>
                      </a>
                    </td>
                  </tr>
                </xsl:if>
              </xsl:if>
              <xsl:if test="$isParent = 'false'">
                <xsl:if test="atom:feed/atom:rights">
                  <tr>
                    <td>Rights</td>
                    <td>
                      <a href="{atom:feed/atom:rights}">
                        <xsl:value-of select="atom:feed/atom:rights" disable-output-escaping="yes"/>
                      </a>
                    </td>
                  </tr>
                </xsl:if>
              </xsl:if>
              <xsl:if test="atom:feed/atom:updated">
                <tr>
                  <td>Updated</td>
                  <td>
                    <xsl:call-template name="date-from-iso8601" disable-output-escaping="yes">
                      <xsl:with-param name="date">
                        <xsl:value-of select="atom:feed/atom:updated"/>
                      </xsl:with-param>
                    </xsl:call-template>
                  </td>
                </tr>
              </xsl:if>
              <xsl:if test="atom:feed/atom:link[@rel='describedby']/@href">
                <tr>
                  <td>
                    <xsl:choose>
                      <xsl:when test="$isParent='true'">
                        <xsl:text>Service Metadata</xsl:text>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:text>Spatial Object Type definition</xsl:text>
                      </xsl:otherwise>
                    </xsl:choose>
                  </td>
                  <td>
                    <a href="{atom:feed/atom:link[@rel='describedby']/@href}">XML</a>
                    /
                    <a href="{atom:feed/atom:link[@rel='related']/@href}">Web page</a>
                  </td>
                </tr>
              </xsl:if>
              <tr>
                <td>ATOM Feed XML</td>
                <td>
                  <button class="small" id="toggleVisibiltyAtomXml" title="Show ATOM XML">Show</button>
                </td>
              </tr>
            </table>
            <div id="xml-wrapper" style="display:none;">
              <pre>
                <code class="language-markup" id="atom-xml" data-prismjs-copy="Copy ATOM XML"></code>
              </pre>
            </div>
          </div>
          <xsl:apply-templates select="//atom:entry">
            <xsl:with-param name="isParent" select="$isParent"></xsl:with-param>
          </xsl:apply-templates>
        </div>
        <script src="https://geodata.gov.md/atom/style/script.js"></script>
      </body>

    </html>
  </xsl:template>

  <xsl:template name="date-from-iso8601">
    <xsl:param name="date"/>
    <xsl:value-of select="concat(substring($date,9,2), '-', substring($date,6,2), '-', substring($date,1,4))"/>
  </xsl:template>


  <xsl:template match="atom:entry">
    <xsl:param name="isParent"/>
    <div id="feedContent">
      <div class="entry">
        <h3 id="feedEntryTitle">
          <xsl:choose>
            <xsl:when test="$isParent='true'">
              <a href="{atom:id}">
                <xsl:value-of select="atom:title" disable-output-escaping="yes"/>
              </a>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="atom:title" disable-output-escaping="yes"/>
            </xsl:otherwise>
          </xsl:choose>
        </h3>

        <xsl:if test="$isParent = 'false'">
          <p>
            <xsl:value-of select="atom:content" disable-output-escaping="yes"/>
          </p>
        </xsl:if>

        <table>
          <xsl:if test="atom:updated">
            <tr>
              <td>Updated</td>
              <td>
                <xsl:call-template name="date-from-iso8601" disable-output-escaping="yes">
                  <xsl:with-param name="date">
                    <xsl:value-of select="atom:updated"/>
                  </xsl:with-param>
                </xsl:call-template>
              </td>
            </tr>
          </xsl:if>
          <xsl:if test="georss:polygon">
            <tr>
              <td>Map area</td>

              <td>
                <xsl:variable name="extent">
                  <xsl:apply-templates select="georss:polygon" disable-output-escaping="yes"/>
                </xsl:variable>
                <xsl:value-of select="$extent" disable-output-escaping="yes"/>
              </td>
            </tr>
          </xsl:if>
          <xsl:if test="atom:category/@term">
            <tr>
              <td>Projection</td>
              <td>
                <xsl:apply-templates select="atom:category" />
              </td>
            </tr>
          </xsl:if>
          <xsl:if test="atom:link/@type and $isParent='false'">
            <tr>
              <td>Filetype</td>
              <td>
                <xsl:value-of select="atom:link[not(@rel='index')]/@type" />
              </td>
            </tr>
          </xsl:if>
          <xsl:if test="not(atom:link[@rel='section']) and atom:link[@rel='alternate'] and $isParent='false'">
            <tr>
              <td>Filesize</td>
              <td>
                <xsl:call-template name="fileSizeHr">
                  <xsl:with-param name="bytes">
                    <xsl:value-of select="atom:link/@length" />
                  </xsl:with-param>
                </xsl:call-template>
              </td>
            </tr>
          </xsl:if>

        </table>
        <xsl:choose>
          <xsl:when test="$isParent='false'">
            <xsl:choose>
              <xsl:when test="atom:link[@rel='index']">
                Downloads
                <app-download-selector index-url="{atom:link[@rel='index']/@href}"></app-download-selector>
                <table id="download-table" class="download">
                  <tr>
                    <td>
                      <a href="{atom:link[@rel='index']/@href}" download="">
                        <xsl:call-template name="fileName">
                          <xsl:with-param name="path">
                            <xsl:value-of select="atom:link[@rel='index']/@href" />
                          </xsl:with-param>
                        </xsl:call-template>
                      </a>
                    </td>
                    <td class="size">
                      <xsl:call-template name="fileSizeHr">
                        <xsl:with-param name="bytes">
                          <xsl:value-of select="atom:link[@rel='index']/@length" />
                        </xsl:with-param>
                      </xsl:call-template>
                    </td>
                    <td colspan="2">Leaf index (<xsl:value-of select="count(atom:link[@rel='section'])"/> Map sheets)</td>
                  </tr >
                  <xsl:for-each select="atom:link[@rel='section']">
                    <xsl:element name="tr">
                      <xsl:if test="@bbox">
                        <xsl:attribute name="bbox">true</xsl:attribute>
                      </xsl:if>
                      <td>
                        <a href="{@href}" download="">
                          <xsl:call-template name="fileName">
                            <xsl:with-param name="path">
                              <xsl:value-of select="@href" />
                            </xsl:with-param>
                          </xsl:call-template>
                        </a>
                      </td>
                      <td class="size">
                        <xsl:call-template name="fileSizeHr">
                          <xsl:with-param name="bytes">
                            <xsl:value-of select="@length" />
                          </xsl:with-param>
                        </xsl:call-template>
                      </td>
                      <td class="time">
                        <xsl:if test="@time">
                          <xsl:call-template name="date-from-iso8601" disable-output-escaping="yes">
                            <xsl:with-param name="date">
                              <xsl:value-of select="@time" />
                            </xsl:with-param>
                          </xsl:call-template>
                        </xsl:if>
                      </td>
                      <td class="bbox">
                        <xsl:if test="@bbox">
                          <xsl:value-of select="@bbox" />
                        </xsl:if>
                      </td>
                    </xsl:element>
                  </xsl:for-each>
                  <xsl:if test="count(atom:link[@rel='section']) > 20">  
                    <tr show-more-bbox="true"><td>...</td></tr>
                  </xsl:if>
                </table>
                <xsl:if test="count(atom:link[@rel='section']) > 20">
                  <button href="#" id="toggleVisibilityDownloads" kaartblad-count="{count(atom:link[@rel='section'])}" onclick="toggleVisibilityDownloads()" title="Show all map sheets ({count(atom:link[@rel='section'])})">Show all</button>
                </xsl:if>

              </xsl:when>
              <xsl:when test="atom:link[@rel='section']">
                Downloads
                <table class="download">
                  <xsl:for-each select="atom:link[@rel='section']">

                    <tr>
                      <td>
                        <a href="{@href}" download="">
                          <xsl:call-template name="fileName">
                            <xsl:with-param name="path">
                              <xsl:value-of select="@href" />
                            </xsl:with-param>
                          </xsl:call-template>
                        </a>
                      </td>
                      <td class="size">
                        <xsl:call-template name="fileSizeHr">
                          <xsl:with-param name="bytes">
                            <xsl:value-of select="@length" />
                          </xsl:with-param>
                        </xsl:call-template>
                      </td>
                      <td class="time">
                        <xsl:if test="@time">
                          <xsl:call-template name="date-from-iso8601" disable-output-escaping="yes">
                            <xsl:with-param name="date">
                              <xsl:value-of select="@time" />
                            </xsl:with-param>
                          </xsl:call-template>
                        </xsl:if>
                      </td>
                      <xsl:if test="@bbox">
                        <td class="bbox">
                          <xsl:value-of select="@bbox" />
                        </td>
                      </xsl:if>
                    </tr>
                  </xsl:for-each>
                </table>
              </xsl:when>
              <xsl:when test="atom:link[@rel='alternate']">
                <xsl:variable name="fileName">
                  <xsl:call-template name="fileName">
                    <xsl:with-param name="path">
                      <xsl:value-of select="atom:link/@href" />
                    </xsl:with-param>
                  </xsl:call-template>
                </xsl:variable>
                <a href="{atom:link/@href}" class="download btn btn-default" title="{$fileName}">DOWNLOAD</a>
              </xsl:when>
            </xsl:choose>
          </xsl:when>
        </xsl:choose>
      </div>
    </div>
  </xsl:template>

  <xsl:template match="georss:polygon">
    <xsl:variable name="item1" select="substring-before(current(),' ')"/>
    <xsl:variable name="input2" select="substring-after(current(),' ')"/>
    <xsl:variable name="item2" select="substring-before($input2,' ')"/>
    <xsl:variable name="input3" select="substring-after($input2,' ')"/>
    <xsl:variable name="item3" select="substring-before($input3,' ')"/>
    <xsl:variable name="input4" select="substring-after($input3,' ')"/>
    <xsl:variable name="item4" select="substring-before($input4,' ')"/>
    <xsl:variable name="input5" select="substring-after($input4,' ')"/>
    <xsl:variable name="item5" select="substring-before($input5,' ')"/>
    <xsl:variable name="input6" select="substring-after($input5,' ')"/>
    <xsl:variable name="item6" select="substring-before($input6,' ')"/>
    <xsl:value-of select="concat($item1,',',$item2,',',$item5,',',$item6)"/>
  </xsl:template>

  <xsl:template name="string-replace-all">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="by"/>
    <xsl:choose>
      <xsl:when test="$text = '' or $replace = ''or not($replace)">
        <!-- Prevent this routine from hanging -->
        <xsl:value-of select="$text"/>
      </xsl:when>
      <xsl:when test="contains($text, $replace)">
        <xsl:value-of select="substring-before($text,$replace)"/>
        <xsl:value-of select="$by"/>
        <xsl:call-template name="string-replace-all">
          <xsl:with-param name="text" select="substring-after($text,$replace)"/>
          <xsl:with-param name="replace" select="$replace"/>
          <xsl:with-param name="by" select="$by"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="fileName">
    <xsl:param name="path" />
    <xsl:choose>
      <xsl:when test="contains($path,'\')">
        <xsl:call-template name="lastToken">
          <xsl:with-param name="token" select="substring-after($path,'\')" />
          <xsl:with-param name="separator">\</xsl:with-param>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="contains($path,'/')">
        <xsl:call-template name="lastToken">
          <xsl:with-param name="token" select="substring-after($path,'/')" />
          <xsl:with-param name="separator">/</xsl:with-param>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$path" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="atom:category">
    <xsl:choose>
      <xsl:when test="contains(@term,'www.opengis.net/def/crs/EPSG/')">
          EPSG:<xsl:call-template name="lastToken">
        <xsl:with-param name="token" select="substring-after(@term,'www.opengis.net/def/crs/EPSG/')" />
        <xsl:with-param name="separator">/</xsl:with-param>
      </xsl:call-template>
      <xsl:if test="@label != ''">
          -
      </xsl:if>
    </xsl:when>
  </xsl:choose>
  <xsl:value-of select="@label" />
</xsl:template>

<xsl:template name="lastToken">
  <xsl:param name="token" />
  <xsl:param name="separator" />
  <xsl:choose>
    <xsl:when test="contains($token,'/')">
      <xsl:call-template name="lastToken">
        <xsl:with-param name="token" select="substring-after($token,'/')" />
        <xsl:with-param name="separator" select="$separator" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$token" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="fileSizeHr">
  <xsl:param name="bytes" />
  <xsl:if test="string-length($bytes) &gt; 0">
    <xsl:if test="number($bytes) &gt;= 0">
      <xsl:choose>
        <xsl:when test="round($bytes div 1024) &lt; 1">
          <xsl:value-of select="$bytes" />
 Bytes</xsl:when>
        <xsl:when test="round($bytes div 1048576) &lt; 1">
          <xsl:value-of select="format-number(($bytes div 1024), '0.0')" />
kb</xsl:when>
        <xsl:when test="round($bytes div 1073741824) &lt; 1">
          <xsl:value-of select="format-number(($bytes div 1048576), '0.0')" />
Mb</xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="format-number(($bytes div 1073741824), '0.00')" />
Gb</xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:if>
</xsl:template>
</xsl:stylesheet>
