<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
    xmlns:idea="http://www.ideadistributors.com/idea/xsl" exclude-result-prefixes="#all">
    <xsl:output indent="yes"/>
    <xsl:include href="idea-functions.xsl"/>
    <xsl:variable name="countriesList" select="document('countries.xml')"/>


    <!-- These are for testing ONLY!!!
    <xsl:variable name="shipment-name" select="'testing'"/>
    <xsl:variable name="shipment-date" select="'testing'"/>
    <xsl:variable name="distributor-name" select="'IDE'"/>
    -->
    <xsl:template match="/catalog">
        <delivery>
            <delivery-info>
                <distributor-name>
                    <xsl:value-of select="distributor-name"/>
                </distributor-name>
                <delivery-name>
                    <xsl:value-of select="shipment-name"/>
                </delivery-name>
                <!-- YYYYMMDD -->
                <delivery-date>
                    <xsl:value-of select="shipment-date"/>
                </delivery-date>
            </delivery-info>
            <xsl:for-each select="album">
                <xsl:if test="string-length(upc) >= 12">
                    <album>
                        <upc>
                            <xsl:value-of select="upc"/>
                        </upc>
                        <!--
                <digital-upc><xsl:value-of select="upc"/></digital-upc>
                <grid><xsl:value-of select=""/></grid>
                <album-id><xsl:value-of select=""/></album-id>
                -->
                        <reporting-id>
                            <xsl:value-of select="upc"/>
                        </reporting-id>
                        <album-name>
                            <xsl:value-of select="title"/>
                        </album-name>

                        <xsl:choose>
                            <xsl:when test="exists(album-type)">
                                <album-version><xsl:value-of select="album-type"/></album-version>
                            </xsl:when>
                            <xsl:when test="compare(@is-explicit, 'explicit') = 0">
                                <album-version>Explicit</album-version>
                            </xsl:when>
                            <xsl:when test="compare(@is-explicit, 'clean') = 0">
                                <album-version>Clean</album-version>
                            </xsl:when>
                            <xsl:otherwise>
                                <!-- blank one needed for validation -->
                                <album-version/>
                            </xsl:otherwise>
                        </xsl:choose>

                        <!-- not implemented, but needed for validation -->
                        <label-code/>

                        <label-name>
                            <xsl:value-of select="label"/>
                        </label-name>
                        <disc-count>
                            <xsl:value-of select="count(volume)"/>
                        </disc-count>
                        <track-count>
                            <xsl:value-of select="count(volume/track)"/>
                        </track-count>
                        <release-date>
                            <xsl:value-of select="replace(release-date, '-', '')"/>
                        </release-date>
                        <copyright>
                            <xsl:text>(P) </xsl:text>
                            <xsl:value-of select="pline"/>
                        </copyright>
                        <xsl:choose>
                            <xsl:when test="compare(@is-explicit, 'explicit') = 0">
                                <is-explicit>Y</is-explicit>
                            </xsl:when>
                            <xsl:otherwise>
                                <is-explicit>N</is-explicit>
                            </xsl:otherwise>
                        </xsl:choose>
                        <contributors>
                            <contributor role="Artist"><xsl:value-of select="display-artist"/></contributor>
                        </contributors>
                        <xsl:for-each select="genre">
                            <genre>
                                <xsl:attribute name="name">
                                    <xsl:value-of select="node()"/>
                                </xsl:attribute>
                            </genre>
                        </xsl:for-each>
            <xsl:choose>
			    <xsl:when test="exists(/catalog/album/territories/allowed)">
			      	<xsl:for-each select="/catalog/album/territories/allowed">
                        <album-rights>
                            <country-code><xsl:value-of select="node()" /></country-code>
                            <bundled>N</bundled>
                        </album-rights>
			      	</xsl:for-each>
			    </xsl:when>
                <xsl:when test="exists(/catalog/album/territories/denied)">
                    <xsl:for-each select="$countriesList/CountryCodes/Code">
                        <!--must loop through each denied to find a match, if found, don't display -->
                        <xsl:if test="not(idea:excluded(node(), territories)=1)">
                            <album-rights>
                                <country-code><xsl:value-of select="node()" /></country-code>
                                <bundled>N</bundled>
                            </album-rights>
                        </xsl:if>
                    </xsl:for-each>
                </xsl:when>
                <xsl:otherwise>
                    <album-rights>
                        <country-code>WWD</country-code>
				        <bundled>N</bundled>
		            </album-rights>
                </xsl:otherwise>
			</xsl:choose>
                        <media>
                            <xsl:attribute name="filename-prefix">
                                <xsl:value-of select="album-filename-prefix"/>
                            </xsl:attribute>
                            <image codec="JPEG" width="709" height="709" type="cover">
                                <xsl:value-of select="image-filename"/>
                            </image>
                        </media>
                        <track-list>
                            <xsl:for-each select="volume">
                                <xsl:variable name="volume" select="@id"/>
                                <xsl:for-each select="track">
                                    <track>
                                        <isrc>
                                            <xsl:value-of select="replace(isrc, '-', '')"/>
                                        </isrc>
                                        <!--
                            <track-id></track-id>
                            -->
                                        <reporting-id>
                                            <xsl:value-of select="replace(isrc, '-', '')"/>
                                        </reporting-id>
                                        <disc-number>
                                            <xsl:value-of select="$volume"/>
                                        </disc-number>
                                        <track-number>
                                            <xsl:value-of select="sequence"/>
                                        </track-number>
                                        <track-name>
                                            <xsl:value-of select="title"/>
                                        </track-name>
                                        <track-version/>
                                        <xsl:variable name="duration">
                                            <xsl:value-of
                                                select="idea:createFriendlyMinutesAndSeconds(length)"
                                            />
                                        </xsl:variable>
                                        <duration>
                                            <xsl:value-of select="$duration"/>
                                        </duration>
                                        <xsl:if test="exists(pline)">
                                            <copyright>
                                                <xsl:text>(P) </xsl:text>
                                                <xsl:value-of select="pline"/>
                                            </copyright>
                                        </xsl:if>
                                        <xsl:choose>
                                            <xsl:when test="compare(@is-explicit, 'true') = 0">
                                                <is-explicit>Y</is-explicit>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <is-explicit>N</is-explicit>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                        <contributors>
                                            <contributor role="Artist"><xsl:value-of select="display-artist"/></contributor>
                                        </contributors>
                                        <xsl:for-each select="genre">
                                            <genre>
                                                <xsl:attribute name="name">
                                                  <xsl:value-of select="node()"/>
                                                </xsl:attribute>
                                            </genre>
                                        </xsl:for-each>
                                        <!--
                            <label-code><xsl:value-of select="name"/></label-code>
                            <label-name><xsl:value-of select="name"/></label-name>

                            <price><xsl:value-of select="name"/></price>
                            -->

                            <xsl:choose>
                                <xsl:when test="exists(/catalog/album/territories/allowed)">
                                    <xsl:for-each select="/catalog/album/territories/allowed">
                                        <rights>
                                            <country-code><xsl:value-of select="node()" /></country-code>
                                            <effective-date/>
                                            <purchase>Y</purchase>
                                            <subscription>Y</subscription>
                                        </rights>
                                    </xsl:for-each>
                                </xsl:when>
                                <xsl:when test="exists(/catalog/album/territories/denied)">
                                    <xsl:for-each select="$countriesList/CountryCodes/Code">
                                        <!--must loop through each denied to find a match, if found, don't display -->
                                        <xsl:if test="not(idea:excluded(node(), territories)=1)">
                                            <rights>
                                                <country-code><xsl:value-of select="node()" /></country-code>
                                                <effective-date/>
                                                <purchase>Y</purchase>
                                                <subscription>Y</subscription>
                                            </rights>
                                        </xsl:if>
                                    </xsl:for-each>
                                </xsl:when>
                                <xsl:otherwise>
                                    <rights>
                                        <country-code>WWD</country-code>
                                        <effective-date/>
                                        <purchase>Y</purchase>
                                        <subscription>Y</subscription>
                                    </rights>
                                </xsl:otherwise>
                            </xsl:choose>
                                        <media>
                                            <xsl:attribute name="filename-prefix">
                                                <xsl:value-of select="track-filename-prefix"/>
                                            </xsl:attribute>
                                            <audio codec="WMA" bitrate="20" type="full">
                                                <xsl:attribute name="duration">
                                                  <xsl:value-of select="$duration"/>
                                                </xsl:attribute>
                                                <xsl:value-of select="audio-filename-1"/>
                                            </audio>
                                            <audio codec="WMA" bitrate="32" type="full">
                                                <xsl:attribute name="duration">
                                                  <xsl:value-of select="$duration"/>
                                                </xsl:attribute>
                                                <xsl:value-of select="audio-filename-2"/>
                                            </audio>
                                            <audio codec="WMA" bitrate="128" type="full">
                                                <xsl:attribute name="duration">
                                                  <xsl:value-of select="$duration"/>
                                                </xsl:attribute>
                                                <xsl:value-of select="audio-filename-7"/>
                                            </audio>

                                            <audio codec="WMA" bitrate="192" type="full">
                                                <xsl:attribute name="duration">
                                                  <xsl:value-of select="$duration"/>
                                                </xsl:attribute>
                                                <xsl:value-of select="audio-filename-8"/>
                                            </audio>
                                            <audio codec="MP3" bitrate="256" type="full">
                                                <xsl:attribute name="duration">
                                                  <xsl:value-of select="$duration"/>
                                                </xsl:attribute>
                                                <xsl:value-of select="audio-filename-12"/>
                                            </audio>
                                        </media>
                                    </track>
                                </xsl:for-each>
                            </xsl:for-each>
                        </track-list>
                    </album>
                </xsl:if>
            </xsl:for-each>
        </delivery>
    </xsl:template>
</xsl:stylesheet>
