<!-- 
FlickrScrapr:  A PDF album generator from flickr 
See <http://lapin-bleu.net/software/flickrscrapr/>
Copyright (c) Max froumentin
This software is provided under the W3C Software Licence
<http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
-->

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	       xmlns:fo="http://www.w3.org/1999/XSL/Format" 
	       version="1.0">

  <xsl:output indent="yes"/>

  <xsl:variable name="apikey" select="/FlickrScrapr/APIKey"/>

  <xsl:template match="FlickrScrapr">
    <xsl:message>FlickrScraper</xsl:message>

    <xsl:variable name="username" select="UserName"/>
<!--    <xsl:variable name="username" select="str:replace(UserName,' ','%20')"/> -->
    <xsl:variable name="getUserIdUri" select="concat('http://www.flickr.com/services/rest/?method=flickr.people.findByUsername&amp;api_key=',$apikey,'&amp;username=',$username)"/>

<!--    <xsl:message>** Getting <xsl:value-of select="$getUserIdUri"/></xsl:message> -->
    <xsl:variable name="getUserIdResponse" select="document($getUserIdUri)"/>
    <xsl:variable name="userID" select="$getUserIdResponse/rsp/user/@id"/> 

    <!-- retrieve the user's full name -->
    <xsl:variable name="peopleGetInfoUri" select="concat('http://www.flickr.com/services/rest/?method=flickr.people.getInfo&amp;api_key=',$apikey,'&amp;user_id=',$userID)"/>
<!--    <xsl:message>** Getting <xsl:value-of select="$peopleGetInfoUri"/></xsl:message> -->
    <xsl:variable name="peopleGetInfoResponse" select="document($peopleGetInfoUri)"/>
    <xsl:variable name="realName" select="$peopleGetInfoResponse/rsp/person/realname"/>
    <xsl:message>Real Name: <xsl:value-of select="$realName"/></xsl:message>

    <!-- are we looking for a set? -->
    <xsl:choose>
      <xsl:when test="photoSet">

	<xsl:message>Getting <xsl:value-of select="nbPhotos"/> pictures from user <xsl:value-of select="$username"/>'s set: <xsl:value-of select="photoSet"/></xsl:message>

	<xsl:variable name="flickr.photosets.getList-URI" select="concat('http://www.flickr.com/services/rest/?method=flickr.photosets.getList&amp;api_key=',$apikey,'&amp;user_id=',$userID)"/>

	<xsl:message>** Getting <xsl:value-of select="$flickr.photosets.getList-URI"/></xsl:message>
	<xsl:variable name="photoset-id" select="document($flickr.photosets.getList-URI)/rsp/photosets/photoset[title=current()/photoSet]/@id"/>
	<xsl:variable name="flickr.photosets.getPhotos-URI" select="concat('http://www.flickr.com/services/rest/?method=flickr.photosets.getPhotos&amp;api_key=',$apikey,'&amp;photoset_id=',$photoset-id)"/>

	<xsl:call-template name="generate-fo">
	  <xsl:with-param name="photos" select="document($flickr.photosets.getPhotos-URI)/rsp/photoset"/>
	  <xsl:with-param name="title1" select="photoSet"/>
	  <xsl:with-param name="title2" select="concat('Flickr photo set from ',$username)"/>
	</xsl:call-template>
      </xsl:when>
      <xsl:otherwise> <!-- not a set, just a user -->

	<xsl:message>Getting <xsl:value-of select="nbPhotos"/> pictures from user <xsl:value-of select="$username"/></xsl:message>

	<xsl:variable name="getUserPhotosUri" select="concat('http://www.flickr.com:80/services/rest/?method=flickr.people.getPublicPhotos&amp;api_key=',$apikey,'&amp;user_id=',$userID,'&amp;per_page=',nbPhotos)"/>
<!--	<xsl:message>** Getting &lt;<xsl:value-of select="$getUserPhotosUri"/>&gt;.</xsl:message> -->
	<xsl:call-template name="generate-fo">
	  <xsl:with-param name="photos" select="document($getUserPhotosUri)/rsp/photos"/>
	  <xsl:with-param name="title1" select="concat('Flickr photo album for ',$username)"/>
	  <xsl:with-param name="title2" select="concat('(',$realName,')')"/>
	</xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <xsl:template name="generate-fo">
    <xsl:param name="photos" select="error"/>
    <xsl:param name="title1" select="''"/>
    <xsl:param name="title2" select="''"/>

    <!-- here we go -->
    <fo:root>
      <fo:layout-master-set>
	<fo:simple-page-master master-name="thumbs-page"
			       page-height="29.7cm" 
			       page-width="21cm"
			       margin-top="1cm" 
			       margin-bottom="1cm" 
			       margin-left="1cm" 
			       margin-right="1cm">
	  <fo:region-body margin-top="4cm" margin-bottom="0cm"/>
	</fo:simple-page-master>
	<fo:simple-page-master master-name="photo-page"
			       page-height="29.7cm" 
			       page-width="21cm"
			       margin-top="1cm" 
			       margin-bottom="1cm" 
			       margin-left="1cm" 
			       margin-right="1cm">
	  <fo:region-body margin-top="4cm" margin-bottom="0cm"/>
	</fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="thumbs-page">
	<fo:flow flow-name="xsl-region-body">
	  <fo:block-container font-size="18pt" 
			      font-family="sans-serif" 
			      line-height="24pt"
			      color="black"
			      text-align="right">
	    <fo:block padding-top="3pt"><xsl:value-of select="$title1"/></fo:block>
	    <fo:block border-bottom="5cm"><xsl:value-of select="$title2"/></fo:block>
	  </fo:block-container>
	  <fo:block text-align="center">
	    <fo:table display-align="center" text-align="center">
	      <fo:table-column column-width="3cm"/>
	      <fo:table-column column-width="3cm"/>
	      <fo:table-column column-width="3cm"/>
	    <fo:table-column column-width="3cm"/>
	    <fo:table-column column-width="3cm"/>
	    <fo:table-column column-width="3cm"/>
	    <fo:table-body >
	      <fo:table-row>
		<xsl:for-each select="$photos/photo">
		  <fo:table-cell>
		    <fo:block>
		      <!-- medium http://static.flickr.com/33/49295158_f6d2a82d24_m_d.jpg -->
		      <!-- original http://static.flickr.com/33/49295158_f6d2a82d24_o_d.jpg -->
		      <fo:external-graphic 
			  src="{concat('url(http://static.flickr.com/',@server,'/',@id,'_',@secret,'_o_d.jpg)')}"
			  content-width="2.9cm"
			  content-height="2.9cm"
			  scaling="uniform"/>
		    </fo:block>
		  </fo:table-cell>
		  <xsl:if test="position() mod 6 = 0 and position()!=last()">
		    <xsl:text disable-output-escaping="yes"><![CDATA[</fo:table-row><fo:table-row>]]></xsl:text>
		  </xsl:if>
		</xsl:for-each>
	      </fo:table-row>
	    </fo:table-body>
	  </fo:table>
	  </fo:block>
	</fo:flow>
      </fo:page-sequence>
      <xsl:for-each select="$photos/photo">
	<fo:page-sequence master-reference="photo-page">
	  <fo:flow flow-name="xsl-region-body">
	    <fo:block font-size="18pt" 
		      font-family="sans-serif" 
		      line-height="18pt"
		      color="black"
		      text-align="right"
		      padding-top="3pt"
		      height="20pt"
		      border-bottom="1cm">
	      <xsl:value-of select="@title"/>
	    </fo:block>
	    <fo:table>
	      <fo:table-column column-width="7cm"/>
	      <fo:table-body>
		<fo:table-row>
		  <fo:table-cell>
		    <fo:block font-size="12pt" border-bottom="1cm" width="5cm">
		      <!-- http://www.flickr.com/services/rest/?method=flickr.test.echo&name=value -->
		      <xsl:variable name="getPhotoInfoUri" select="concat('http://www.flickr.com/services/rest/?method=flickr.photos.getInfo&amp;api_key=',$apikey,'&amp;photo_id=',@id)"/>
		      <xsl:message>** Getting &lt;<xsl:value-of select="$getPhotoInfoUri"/>&gt;.</xsl:message>
		      <xsl:variable name="getPhotoInfoResponse" select="document($getPhotoInfoUri)"/>
		      <xsl:apply-templates select="$getPhotoInfoResponse/rsp/photo/description"/>
		    </fo:block>
		  </fo:table-cell>
		</fo:table-row>
	      </fo:table-body>
	    </fo:table>
	    <fo:block>
	      <fo:footnote>
		<fo:inline/>
		<fo:footnote-body>
		  <fo:block text-align="center" display-align="center" border-bottom="2cm">
		    <!-- medium http://static.flickr.com/33/49295158_f6d2a82d24_m_d.jpg-->
		    <!-- original http://static.flickr.com/33/49295158_f6d2a82d24_o_d.jpg-->
		    <fo:external-graphic 
			src="{concat('url(http://static.flickr.com/',@server,'/',@id,'_',@secret,'_o_d.jpg)')}"
			content-width="17cm"
			content-height="17cm"
			scaling="uniform"
			/>
		  </fo:block>
		</fo:footnote-body>
	      </fo:footnote>
	    </fo:block>
	  </fo:flow>
	</fo:page-sequence>
      </xsl:for-each>
    </fo:root>
  </xsl:template>


</xsl:transform>
