<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.sistory.si/schemas/sistory-ar/"
    xmlns:sistory="http://www.sistory.si/schemas/sistory-ar/"
    xmlns:junaio="http://dev.junaio.com/index/mychannels"
    xmlns:layar="http://www.layar.com/documentation/browser/"
    xmlns:arel="http://www.junaio.com/develop/docs/arel-xml-schema/"
    xmlns:kml="http://www.opengis.net/kml/2.2"
    xmlns:ar="http://www.openarml.org/arml/1.0"
    xmlns:wikitude="http://www.openarml.org/wikitude/1.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    version="2.0"
    exclude-result-prefixes="sistory kml junaio layar arel dc">
    
    <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl" scope="stylesheet" type="stylesheet">
        <desc>
            <p>SIstory augmented reality stylesheet for converting SIstory augmented reality XML to ARML XML (Wikitude).</p>
            <p>This software is dual-licensed:
                
                1. Distributed under a Creative Commons Attribution-ShareAlike 3.0
                Unported License http://creativecommons.org/licenses/by-sa/3.0/ 
                
                2. http://www.opensource.org/licenses/BSD-2-Clause
                
                All rights reserved.
                
                Redistribution and use in source and binary forms, with or without
                modification, are permitted provided that the following conditions are
                met:
                
                * Redistributions of source code must retain the above copyright
                notice, this list of conditions and the following disclaimer.
                
                * Redistributions in binary form must reproduce the above copyright
                notice, this list of conditions and the following disclaimer in the
                documentation and/or other materials provided with the distribution.
                
                This software is provided by the copyright holders and contributors
                "as is" and any express or implied warranties, including, but not
                limited to, the implied warranties of merchantability and fitness for
                a particular purpose are disclaimed. In no event shall the copyright
                holder or contributors be liable for any direct, indirect, incidental,
                special, exemplary, or consequential damages (including, but not
                limited to, procurement of substitute goods or services; loss of use,
                data, or profits; or business interruption) however caused and on any
                theory of liability, whether in contract, strict liability, or tort
                (including negligence or otherwise) arising in any way out of the use
                of this software, even if advised of the possibility of such damage.
            </p>
            <p xml:lang="en">Andrej Pančur, Institute of contemporary History, Ljubljana</p>
            <p xml:lang="sl">Andrej Pančur, Inštitut za novejšo zgodovino, Ljubljana</p>
            <p>2013-10-10</p>
        </desc>
    </doc>
    
    <!-- Identifikacijska številka Wikituda bo ostala enaka. Lahko se jo poljubno spremeni. -->
    <xsl:param name="wikitudeID">sistory</xsl:param>
    
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="/">
        <xsl:for-each select="sistory:AR/sistory:providerINFO/sistory:name">
            <xsl:variable name="language" select="@lang"/>
            <xsl:variable name="document">
                <xsl:value-of select="concat('wikitude-',$language,'.xml')"/>
            </xsl:variable>
            <!-- Za vsak jezik posebej naredi nov dokument. -->
            <xsl:result-document href="{$document}">
                <kml xmlns="http://www.opengis.net/kml/2.2"
                    xmlns:wikitude="http://www.openarml.org/wikitude/1.0"
                    xmlns:ar="http://www.openarml.org/arml/1.0">
                    <Document>
                        <ar:provider id="{$wikitudeID}">
                            <ar:name><xsl:value-of select=".[@lang = $language]"/></ar:name>
                            <ar:description>
                                <xsl:value-of select="ancestor::sistory:providerINFO/sistory:wikitude/sistory:xml/ar:providerDescription[@lang = $language]"/>
                            </ar:description>
                            <wikitude:providerUrl>
                                <xsl:value-of select="ancestor::sistory:providerINFO/sistory:wikitude/sistory:xml/wikitude:providerURL"/>
                            </wikitude:providerUrl>
                            <wikitude:tags>
                                <xsl:value-of select="ancestor::sistory:providerINFO/sistory:wikitude/sistory:xml/wikitude:tags[@lang = $language]"/>
                            </wikitude:tags>
                            <wikitude:logo>
                                <xsl:value-of select="ancestor::sistory:providerINFO/sistory:wikitude/sistory:xml/wikitude:logo"/>
                            </wikitude:logo>
                            <wikitude:icon>
                                <xsl:value-of select="ancestor::sistory:providerINFO/sistory:wikitude/sistory:xml/wikitude:icon"/>
                            </wikitude:icon>
                            <wikitude:shortName>
                                <xsl:value-of select="ancestor::sistory:providerINFO/sistory:wikitude/sistory:xml/wikitude:shortName[@lang = $language]"/>
                            </wikitude:shortName>
                            <!-- Kopiram podobno besedilo iz AR/providerINFO/wikitude/admin/ar:description -->
                            <wikitude:promotionText>
                                <xsl:value-of select=" ancestor::sistory:providerINFO/sistory:wikitude/sistory:admin/ar:description"/>
                            </wikitude:promotionText>
                            <wikitude:noPromotion>
                                <xsl:value-of select="ancestor::sistory:providerINFO/sistory:wikitude/sistory:xml/wikitude:noPromotion"/>
                            </wikitude:noPromotion>
                        </ar:provider>
                        <!-- TOČKE INTERESA POI -->
                        <!-- samo tiste, ki so aktivne  - se pravi, da je boolean vrednost atributa active true -->
                        <xsl:for-each select="ancestor::sistory:AR/sistory:POIs/sistory:poi[@active=true()]">
                            <Placemark>
                                <xsl:attribute name="id">
                                    <xsl:value-of select="concat(@id,'_',$language)"/>
                                </xsl:attribute>
                                <ar:provider>
                                    <xsl:value-of select="ancestor::sistory:AR/sistory:providerINFO/sistory:wikitude/sistory:xml/ar:providerID"/>
                                </ar:provider>
                                <!-- naslov -->
                                <xsl:choose>
                                    <xsl:when test="sistory:text/sistory:title">
                                        <name>
                                            <xsl:value-of select="sistory:text/sistory:title[@lang = $language]"/>
                                        </name>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <name>
                                            <xsl:value-of select="sistory:text/layar:title[@lang = $language]"/>
                                        </name>
                                    </xsl:otherwise>
                                </xsl:choose>
                                <!-- opis -->
                                <xsl:choose>
                                    <xsl:when test="sistory:text/sistory:description[@lang = $language]">
                                        <description>
                                            <xsl:value-of select="normalize-space(sistory:text/sistory:description[@lang = $language])"/>
                                        </description>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <description>
                                            <xsl:value-of select="normalize-space(sistory:text/layar:description[@lang = $language])"/>
                                        </description>
                                    </xsl:otherwise>
                                </xsl:choose>
                                <!-- informacije -->
                                <wikitude:info>
                                    <!-- Slike.
                                         Vedno izbere samo prvo sliko po vrsti.
                                         Morebitne ostale slike so lahko vključene v galerijo na samostojni spletni strani.
                                    -->
                                    <xsl:choose>
                                        <xsl:when test="sistory:images/sistory:image[1]/sistory:imageResize/sistory:path">
                                            <wikitude:thumbnail>
                                                <xsl:value-of select="concat(sistory:images/sistory:image[1]/sistory:imageResize/sistory:path,'&amp;width=64&amp;height=64')"/>
                                            </wikitude:thumbnail>
                                            <wikitude:hiResImageUrl>
                                                <xsl:value-of select="concat(sistory:images/sistory:image[1]/sistory:imageResize/sistory:path,'&amp;width=500&amp;height=370')"/>
                                            </wikitude:hiResImageUrl>
                                        </xsl:when>
                                        <xsl:when test="sistory:images/sistory:image[1]/sistory:imageResize/sistory:imageID">
                                            <wikitude:thumbnail>
                                                <xsl:value-of select="concat(ancestor::sistory:AR/sistory:providerINFO/sistory:imageResizePath,sistory:images/sistory:image[1]/sistory:imageResize/sistory:imageID,'&amp;width=64&amp;height=64')"/>
                                            </wikitude:thumbnail>
                                            <wikitude:hiResImageUrl>
                                                <xsl:value-of select="concat(ancestor::sistory:AR/sistory:providerINFO/sistory:imageResizePath,sistory:images/sistory:image[1]/sistory:imageResize/sistory:imageID,'&amp;width=500&amp;height=370')"/>
                                            </wikitude:hiResImageUrl>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:if test="sistory:images/sistory:image[1]/wikitude:thumbnail">
                                                <wikitude:thumbnail>
                                                    <xsl:value-of select="sistory:images/sistory:image[1]/wikitude:thumbnail"/>
                                                </wikitude:thumbnail>
                                            </xsl:if>
                                            <xsl:if test="sistory:images/sistory:image[1]/wikitude:hiResImageUrl">
                                                <wikitude:hiResImageUrl>
                                                    <xsl:value-of select="sistory:images/sistory:image[1]/wikitude:hiResImageUrl"/>
                                                </wikitude:hiResImageUrl>
                                            </xsl:if>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                    <!-- KER WIKITUDE NE MORE PRIKAZATI VEČ KOT ENO OD ISTIH VRST AKCIJ
                                         (npr. samo eno telefonsko številko), vedno izberemo prvo od njih.
                                    -->
                                    <!-- Izbere prvo telefonsko številko. -->
                                    <xsl:if test="sistory:actions/sistory:phone">
                                        <wikitude:phone>
                                            <xsl:value-of select="sistory:actions/sistory:phone[1]"/>
                                        </wikitude:phone>
                                    </xsl:if>
                                    <!-- Povezava na spletno stran:
                                         Ker je lahko samo ena povezava wikitude:url, je najprej potrebno preveriti, če obstajata
                                         sistory:link in sistory:linksGroup.
                                         Pri pretvorbi upoštevamo sledeče pravilo:
                                         - če sta tako sistory:link kot sistory:linksGroup izberemo sistory:link
                                         - če je več kot eden sistory:link izberemo prvega
                                         - če je več kot eden sistory:linksGroup izberemo prvega
                                    -->
                                    <!-- če sta tako sistory:link kot sistory:linksGroup -->
                                    <!-- Vsaka jezikovna varianta Wikitude Worlda dobi povezavo na to spletno stran. -->
                                    <xsl:if test="sistory:actions/sistory:link 
                                              and sistory:actions/sistory:linksGroup">
                                        <wikitude:url name="{sistory:actions/sistory:link[1]/@label}">
                                            <xsl:value-of select="sistory:actions/sistory:link[1]"/>
                                        </wikitude:url>
                                    </xsl:if>
                                    <!-- če je sistory:link in ni sistory:linksGroup -->
                                    <!-- Vsaka jezikovna varianta Wikitude Worlda dobi povezavo na to spletno stran. -->
                                    <xsl:if test="sistory:actions/sistory:link 
                                          and not(sistory:actions/sistory:linksGroup)">
                                        <wikitude:url name="{sistory:actions/sistory:link[1]/@label}">
                                            <xsl:value-of select="sistory:actions/sistory:link[1]"/>
                                        </wikitude:url>
                                    </xsl:if>
                                    <!-- če ni sistory:link in je sistory:linksGroup -->
                                    <!-- Če je več jezikovnih variant posamezne spletne strani, se jezikovnim variantam Junaio aplikacije
                                             določajo glede na podatek o jeziku in sicer po sledečih prioritetah:
                                             - če obstaja spletna stran z istim jezikom kot aplikacija, se da to spletno stran,
                                             - če ne obstaja spletna stran z istim jezikom kot aplikacija, se da angleško spletno stran,
                                             - če ne obstaja angleška spletna stran, se da slovensko spletno stran.
                                        -->
                                    <xsl:if test="not(sistory:actions/sistory:link)
                                                  and sistory:actions/sistory:linksGroup">
                                        <xsl:choose>
                                            <xsl:when test="sistory:actions/sistory:linksGroup[1]/sistory:link[@lang = $language]">
                                                <wikitude:url name="{sistory:actions/sistory:linksGroup[1]/sistory:link[@lang = $language]/@label}">
                                                    <xsl:value-of select="sistory:actions/sistory:linksGroup[1]/sistory:link[@lang = $language]"/>
                                                </wikitude:url>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <xsl:choose>
                                                    <xsl:when test="sistory:actions/sistory:linksGroup[1]/sistory:link[@lang != $language]
                                                                and sistory:actions/sistory:linksGroup[1]/sistory:link[@lang='eng']">
                                                        <wikitude:url name="{sistory:actions/sistory:linksGroup[1]/sistory:link[@lang='eng']/@label}">
                                                            <xsl:value-of select="sistory:actions/sistory:linksGroup[1]/sistory:link[@lang='eng']"/>
                                                        </wikitude:url>
                                                    </xsl:when>
                                                    <!-- predpostavljam, da je ena spletna stran vedno v slovenščini -->
                                                    <xsl:otherwise>
                                                        <wikitude:url name="{sistory:actions/sistory:linksGroup[1]/sistory:link[@lang='slv']/@label}">
                                                            <xsl:value-of select="sistory:actions/sistory:linksGroup[1]/sistory:link[@lang='slv']"/>
                                                        </wikitude:url>
                                                    </xsl:otherwise>
                                                </xsl:choose>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:if>
                                    <!-- Izbere prvi naslov spletne pošte. -->
                                    <xsl:if test="sistory:actions/sistory:email">
                                        <wikitude:email>
                                            <xsl:value-of select="sistory:actions/sistory:email[1]"/>
                                        </wikitude:email>
                                    </xsl:if>
                                    <!-- naslov -->
                                    <xsl:if test="sistory:actions/sistory:address">
                                        <wikitude:address>
                                            <xsl:value-of select="concat(sistory:actions/sistory:address/sistory:street,' ',
                                                sistory:actions/sistory:address/sistory:houseNumber,', ',
                                                sistory:actions/sistory:address/sistory:place,', ',
                                                sistory:actions/sistory:address/sistory:state)"/>
                                        </wikitude:address>
                                    </xsl:if>
                                </wikitude:info>
                                <!-- geografske koordinate (so obvezne - ne smejo manjkati)
                                     najprej je lon, potem je lat in nazadnje obvezen (četudi vrednost 0) alt-->
                                <xsl:variable name="alt">
                                    <xsl:choose>
                                        <xsl:when test="sistory:location/sistory:alt">
                                            <xsl:value-of select="sistory:location/sistory:alt"/>
                                        </xsl:when>
                                        <xsl:otherwise>0</xsl:otherwise>
                                    </xsl:choose>
                                </xsl:variable>
                                <Point>
                                    <coordinates>
                                        <xsl:value-of select="concat(sistory:location/sistory:lon,',',sistory:location/sistory:lat,',',$alt)"/>
                                    </coordinates>
                                </Point>
                            </Placemark>
                        </xsl:for-each><!-- konec procesiranja POI -->
                    </Document>
                </kml>
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>
    
</xsl:stylesheet>