<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:idea="http://www.ideadistributors.com/idea/xsl"
    xmlns:xdt="http://www.w3.org/2005/04/xpath-datatypes"
    xmlns:fn="http://www.w3.org/2005/04/xpath-functions"
    exclude-result-prefixes="#all">

    <xsl:include href="idea-functions.xsl"/>
    <xsl:include href="mapping/emusic-mapping.xsl"/>

    <xsl:variable name="action" select="'insert'"/>

    <xsl:template match="album">

    <feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="http://www.emusic.com/ingest/docs/remote_ingest_2.0.xsd"
        version="2.0.1">
        <xsl:attribute name="partner_album_id" select="upc"/>
        <xsl:attribute name="action" select="$action"/>
        <xsl:attribute name="partner_id" select="vendor-id"/>

        <album>
            <title><xsl:value-of select="title"/></title>
            <title_sort><xsl:value-of select="idea:createSortName(title)"/></title_sort>
            <UPC><xsl:value-of select="upc"/></UPC>
            <label>
                <name><xsl:value-of select="label"/></name>
                <name_sort><xsl:value-of select="idea:createSortName(label)"/></name_sort>
            </label>
            <number_of_discs><xsl:value-of select="count(volume)"/></number_of_discs>
            <!-- using our genres for now  -->

            <xsl:copy-of copy-namespaces="no" exclude-result-prefixes="#all" select="idea:mapGenre(genre[1])"></xsl:copy-of>

            <release_date><xsl:value-of select="release-date"/></release_date>

            <xsl:if test="(translate(release-date cast as xs:string, '-', '')) cast as xs:integer > (translate(substring(current-date() cast as xs:string, 1, 10), '-', ''))cast as xs:integer">
                <street_date><xsl:value-of select="release-date"/></street_date>
            </xsl:if>

            <artist_display><xsl:value-of select="display-artist"/></artist_display>
            <artist_display_sort><xsl:value-of select="idea:createSortName(display-artist)"/></artist_display_sort>

            <artists>
                <artist>
                    <xsl:attribute name="is_primary" select="'true'"/>
                    <name><xsl:value-of select="display-artist"/></name>
                    <name_sort><xsl:value-of select="idea:createSortName(display-artist)"/></name_sort>
                    <role>performer</role>
                </artist>
                <!--<xsl:if test="/album/is-classical = 1">
                  <artist>
                    <name><xsl:value-of select="/album/volume/track/composer"/></name>
                    <name_sort><xsl:value-of select="/album/volume/track/composer"/></name_sort>
                    <role>composer</role>
                  </artist>
              </xsl:if>-->
            </artists>

            <discs>
                <xsl:for-each select="volume">
                <disc>
                    <disc_name>
                        <xsl:choose>
                            <xsl:when test="exists(title)">
                                <xsl:value-of select="title"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="concat('Disc ', @id)"/>
                            </xsl:otherwise>
                    </xsl:choose>
                    </disc_name>
                    <disc_number><xsl:value-of select="@id"/></disc_number>
                    <number_of_songs><xsl:value-of select="count(track)"/></number_of_songs>
                    <songs>
                        <xsl:for-each select="track">
                        <song>
                            <name><xsl:value-of select="title"/></name>
                            <name_sort><xsl:value-of select="idea:createSortName(title)"/></name_sort>
                            <song_rights>download</song_rights>
                            <isrc><xsl:value-of select="replace(isrc, '-', '')"/></isrc>
                            <length><xsl:value-of select="audio-duration"/></length>
                            <track_number><xsl:value-of select="sequence"/></track_number>

                            <artist_display>
                                <xsl:value-of select="display-artist"/>
                            </artist_display>

                            <artists>
                                <artist>
                                    <xsl:attribute name="is_primary" select="'true'"/>
                                    <name><xsl:value-of select="display-artist"/></name>
                                    <name_sort><xsl:value-of select="idea:createSortName(display-artist)"/></name_sort>
                                    <role>performer</role>
                                </artist>
                                <xsl:if test="/album/is-classical = 1">
                                <artist>
                                    <name><xsl:value-of select="composer"/></name>
                                    <name_sort><xsl:value-of select="composer"/></name_sort>
                                    <role>composer</role>
                                </artist>
                                </xsl:if>
                            </artists>
                        </song>
                        </xsl:for-each>
                    </songs>
                </disc>
                </xsl:for-each>
            </discs>

            <xsl:if test="normalize-space(territories/allowed[1]) or normalize-space(territories/denied[1])" >
            <country_restrictions>
                <xsl:for-each select="territories/allowed">
            	    <xsl:if test="normalize-space(node())" >
                	    <allow><xsl:value-of select="node()"/></allow>
                	</xsl:if>
                </xsl:for-each>
                <xsl:for-each select="territories/denied">
            	    <xsl:if test="normalize-space(node())" >
                    	<disallow><xsl:value-of select="node()"/></disallow>
                	</xsl:if>
                </xsl:for-each>
            </country_restrictions>
            </xsl:if>

            <explicit_lyrics>
                <xsl:choose>
                    <xsl:when test="compare(is-explicit, 'explicit') = 0">
                        <xsl:text>true</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:text>false</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
            </explicit_lyrics>
        </album>
    </feed>



</xsl:template>


</xsl:stylesheet>
