<?xml version="1.0" encoding="utf-8"?>

<!-- This is to do a simple printed directory of people associated
      with Peace Corps including basic contact info and pictures
      to aid new people associated with the organization.
  -->
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:fo="http://www.w3.org/1999/XSL/Format"
 xmlns:fox="http://xml.apache.org/fop/extensions"
 xmlns:xnl="urn:oasis:names:tc:ciq:xsdschema:xNL:2.0"
 xmlns:cil="urn:oasis:names:tc:ciq:xsdschema:xCIL:2.0"
 xmlns:stf="http://pcvs.org/2004/05/staffML"
 xmlns:vol="http://pcvs.org/2004/05/volunteerML">
  <xsl:import href="translate_sector.xslt"/>
  <xsl:import href="convert_date.xslt"/>
  <xsl:output
   standalone="no"
   indent="yes"/>

  <!-- Site where this file is living, (Used to make links relative
     -  to bypass passwords on resources. A pain, but necessary.)
    -->
  <xsl:variable name="sitename">mr.pcvs.org</xsl:variable>

  <xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <xsl:apply-templates mode="fox-toc"/>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="nonumber"
                               page-height="297mm" page-width="210mm"
                               margin-top="20mm" margin-bottom="20mm"
                               margin-left="25mm" margin-right="25mm">
          <fo:region-body margin-bottom="20mm"/>
          <fo:region-after region-name="nonumber-footer" extent="20mm"/>
        </fo:simple-page-master>
        <fo:simple-page-master master-name="numbered"
                               page-height="297mm" page-width="210mm"
                               margin-top="20mm" margin-bottom="20mm"
                               margin-left="25mm" margin-right="25mm">
          <fo:region-body margin-bottom="20mm"/>
          <fo:region-after region-name="numbered-footer" extent="20mm"/>
        </fo:simple-page-master>
        <fo:simple-page-master master-name="numbered-columnated"
                               page-height="210mm" page-width="297mm"
                               margin-top="10mm" margin-bottom="10mm"
                               margin-left="10mm" margin-right="10mm">
          <fo:region-body column-count="2" column-gap="2mm" margin-bottom="10mm"/>
          <fo:region-after region-name="numbered-columnated-footer" extent="10mm"/>
        </fo:simple-page-master>
        <fo:simple-page-master master-name="chapter-start"
                               page-height="210mm" page-width="297mm"
                               margin-top="10mm" margin-bottom="10mm"
                               margin-left="10mm" margin-right="10mm">
          <fo:region-body column-count="2" column-gap="2mm"
                          margin-top="13mm" margin-bottom="10mm"/>
          <fo:region-before region-name="chapter-header" extent="15mm"/>
          <fo:region-after region-name="numbered-columnated-footer" extent="10mm"/>
        </fo:simple-page-master>
        <fo:page-sequence-master master-name="preface">
          <fo:repeatable-page-master-alternatives>
            <fo:conditional-page-master-reference page-position="first"
                                                  master-reference="nonumber"/>
            <fo:conditional-page-master-reference page-position="rest"
                                                  master-reference="numbered"/>
          </fo:repeatable-page-master-alternatives>
        </fo:page-sequence-master>
        <fo:page-sequence-master master-name="chapter">
          <fo:repeatable-page-master-alternatives>
            <fo:conditional-page-master-reference page-position="first"
                                                  master-reference="chapter-start"/>
            <fo:conditional-page-master-reference page-position="rest"
                                                  master-reference="numbered-columnated"/>
          </fo:repeatable-page-master-alternatives>
        </fo:page-sequence-master>
      </fo:layout-master-set>
      
      <fo:page-sequence master-reference="preface" format="i">
        <fo:static-content flow-name="numbered-footer">
          <fo:block text-align="center">
            <fo:page-number/>
          </fo:block>
        </fo:static-content>
        <fo:flow flow-name="xsl-region-body"
                 font-family="sans-serif" font-size="13pt">
          <fo:block break-before="page">
            <xsl:apply-templates select="/*/preface"/>
          </fo:block>
          <xsl:call-template name="toc"/>
        </fo:flow>
      </fo:page-sequence>
      <xsl:apply-templates select="/*/chapter"/>
    </fo:root>
  </xsl:template>

  <xsl:template match="chapter">
    <xsl:element name="fo:page-sequence">
      <xsl:attribute name="master-reference">chapter</xsl:attribute>
      <xsl:if test="not(preceding::chapter)">
        <xsl:attribute name="initial-page-number">1</xsl:attribute>
      </xsl:if>
      <fo:static-content flow-name="chapter-header">
        <fo:block font-size="30pt" font-weight="bold" text-align="center"
                  background-color="rgb(205, 206, 228)">
          <xsl:value-of select="title"/>
        </fo:block>
      </fo:static-content>
      <fo:static-content flow-name="numbered-columnated-footer">
        <fo:block text-align="center">
          <fo:page-number/>
        </fo:block>
      </fo:static-content>
      <fo:flow flow-name="xsl-region-body"
               font-family="sans-serif"
               font-size="13pt">
        <fo:block break-before="page" id="{generate-id()}">
          <xsl:apply-templates/>
        </fo:block>
      </fo:flow>
    </xsl:element>
  </xsl:template>

  <xsl:template match="bookinfo/title|chapter/title">
  </xsl:template>

  <xsl:template match="preface/title|section/title">
    <fo:block font-size="20pt" space-after.optimum="5pt" text-align="center"
              background-color="rgb(199, 233, 210)">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="section">
    <xsl:element name="fo:block">
      <xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
      <xsl:if test="count(preceding-sibling::section) > 0">
        <xsl:attribute name="break-before">column</xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
  
  <xsl:template match="para">
    <fo:block space-after.optimum="1em"><xsl:apply-templates/></fo:block>
  </xsl:template>

  <xsl:template match="vol:volunteers">
    <xsl:apply-templates select="descendant::vol:volunteer">
      <xsl:sort select="xnl:PersonName/xnl:FirstName"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="stf:staff">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="vol:volunteer|stf:person">
    <fo:table table-layout="fixed"
              space-after.optimum="15pt" width="100%"
              border-width="3pt" border-color="black">
      <fo:table-column column-width="proportional-column-width(1)"/>
      <fo:table-column column-width="proportional-column-width(4)"/>
      <fo:table-body>
        <fo:table-row keep-together="always">
          <xsl:element name="fo:table-cell">
            <xsl:attribute name="display-align">center</xsl:attribute>
            <xsl:attribute name="background-color">rgb(255, 241, 232)</xsl:attribute>
            <xsl:attribute name="text-align">center</xsl:attribute>
            <xsl:if test="count(stf:photo) = 0">
              <xsl:attribute name="border">solid gray</xsl:attribute>
            </xsl:if>
            <fo:block font-size="30pt" text-align="center"
                      font-weight="bold" margin-top="0px">
              <xsl:choose>
                <xsl:when test="count(stf:photo) > 0">
                  <xsl:apply-templates select="stf:photo"/>
                </xsl:when>
                <xsl:otherwise>
                  <!-- <xsl:text>X</xsl:text> -->
                </xsl:otherwise>
              </xsl:choose>
            </fo:block>
          </xsl:element>
          <fo:table-cell display-align="center" background-color="rgb(255, 241, 232)">
            <fo:table table-layout="fixed" width="100%"
                      border-width="3pt" border-color="black">
              <fo:table-column column-width="proportional-column-width(1.3)"/>
              <fo:table-column column-width="proportional-column-width(4)"/>
              <fo:table-body>
                <fo:table-row>
                  <fo:table-cell background-color="rgb(255, 241, 232)"
                                 number-columns-spanned="2">
                    <fo:block font-size="25pt" text-align="center" font-weight="bold"
                              id="{generate-id()}">
                      <xsl:apply-templates select="xnl:PersonName"/>
                    </fo:block>
                  </fo:table-cell>
                </fo:table-row>
                <xsl:apply-templates select="xnl:PersonName/xnl:KnownAs"/>
                <xsl:apply-templates select="@sector"/>
                <xsl:apply-templates select="vol:site"/>
                <xsl:apply-templates select="xnl:Position"/>
                <xsl:apply-templates select="cil:EmailAddresses/cil:EmailAddress[position() = 1]"/>
                <xsl:apply-templates select="cil:ContactNumbers/cil:ContactNumber"/>
                <xsl:apply-templates select="cil:PersonInfo/cil:MaritalStatus"/>
                <xsl:apply-templates select="cil:PersonInfo/stf:NumberChildren"/>
                <xsl:apply-templates select="cil:BirthDate"/>
              </fo:table-body>
            </fo:table>
          </fo:table-cell>
        </fo:table-row>
      </fo:table-body>
    </fo:table>
  </xsl:template>
  
  <xsl:template match="xnl:PersonName">
    <xsl:value-of select="xnl:FirstName"/>
    <xsl:apply-templates select="xnl:MiddleName"/>
    <xsl:if test="count(xnl:LastName) > 0">
      <xsl:text> </xsl:text>
      <xsl:value-of select="xnl:LastName"/>
    </xsl:if>
    <xsl:apply-templates select="xnl:GenerationIdentifier"/>
  </xsl:template>
  
  <xsl:template match="xnl:MiddleName">
    <xsl:text> </xsl:text>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="xnl:GenerationIdentifier">
    <xsl:text> </xsl:text>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="vol:site|xnl:Position|@sector|cil:EmailAddress|xnl:KnownAs|cil:BirthDate|cil:ContactNumber|cil:MaritalStatus|stf:NumberChildren">
    <xsl:variable name="content">
      <xsl:apply-templates select="." mode="content"/>
    </xsl:variable>
    <xsl:if test="$content != ''">
      <fo:table-row>
        <fo:table-cell padding=".1em .25em .1em .25em"
                       background-color="rgb(48, 45, 74)"
                       text-align="right">
          <fo:block font-weight="bold" color="white">
            <xsl:variable name="name"><xsl:value-of select="name()"/></xsl:variable>
            <xsl:if test="count(preceding-sibling::*[name() = $name]) = 0">
              <xsl:choose>
                <xsl:when test="local-name() = 'EmailAddress'">
                  <xsl:text>E-Mail:</xsl:text>
                </xsl:when>
                <xsl:when test="local-name() = 'KnownAs'">
                  <xsl:text>Known As:</xsl:text>
                </xsl:when>
                <xsl:when test="local-name() = 'BirthDate'">
                  <xsl:text>Birthday:</xsl:text>
                </xsl:when>
                <xsl:when test="local-name() = 'MaritalStatus'">
                  <xsl:text>Married:</xsl:text>
                </xsl:when>
                <xsl:when test="local-name() = 'NumberChildren'">
                  <xsl:text>Children:</xsl:text>
                </xsl:when>
                <xsl:when test="local-name() = 'ContactNumber'">
                  <!-- <xsl:if test="count(preceding-sibling::cil:ContactNumber) = 0"> -->
                  <xsl:text>Phone:</xsl:text>
                </xsl:when>
                <xsl:when test="local-name() = 'Position'">
                  <xsl:text>Position:</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="translate(substring(local-name(), 1, 1),
                                'abcdefghijklmnopqrstuvwxyz',
                                'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
                  <xsl:value-of select="substring(local-name(), 2)"/>
                  <xsl:text>:</xsl:text>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:if>
          </fo:block>
        </fo:table-cell>
        <fo:table-cell padding=".1em .25em .1em .25em"
                       background-color="rgb(223, 223, 242)">
          <fo:block>
            <xsl:value-of select="$content"/>
          </fo:block>
        </fo:table-cell>
      </fo:table-row>
    </xsl:if>
  </xsl:template>

  <xsl:template match="@sector" mode="content">
    <xsl:call-template name="sector">
      <xsl:with-param name="sector" select="."/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="cil:BirthDate" mode="content">
    <xsl:call-template name="date">
      <xsl:with-param name="date"><xsl:value-of select="cil:Date"/></xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="cil:ContactNumber" mode="content">
    <xsl:apply-templates select="cil:Number"/>
    <xsl:if test="count(cil:Extension) > 0">
      <xsl:text> [</xsl:text>
      <xsl:value-of select="cil:Extension"/>
      <xsl:text>]</xsl:text>
    </xsl:if>
    <xsl:if test="count(../cil:ContactNumber) > 1 and count(cil:Type) > 0">
      <xsl:text> (</xsl:text>
      <xsl:value-of select="cil:Type"/>
      <xsl:text>)</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template match="cil:Number">
    <xsl:variable name="number"><xsl:value-of select="."/></xsl:variable>
    <xsl:choose>
      <xsl:when test="string-length($number) = 7">
        <xsl:value-of select="substring($number, 1, 3)"/>
        <xsl:text>.</xsl:text>
        <xsl:value-of select="substring($number, 4, 2)"/>
        <xsl:text>.</xsl:text>
        <xsl:value-of select="substring($number, 6, 2)"/>
      </xsl:when>
      <xsl:otherwise><xsl:value-of select="$number"/></xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="cil:EmailAddress" mode="content">
    <xsl:element name="fo:basic-link">
      <xsl:attribute name="external-destination">
        <xsl:text>mailto:</xsl:text>
        <xsl:apply-templates/>
      </xsl:attribute>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <!-- The handling of this is something of a kludge. The problem is that most
     -  of the pictures are only accessible with a password, and FOP will not
     -  allow me to put the password in the URL. (I still get a 401.) So, I
     -  have to check if the picture is on this site and if so make the link
     -  relative.
    -->
  <xsl:template match="stf:photo">
    <xsl:variable name="url"><xsl:value-of select="."/></xsl:variable>
    <xsl:variable name="site" select="concat('http://', $sitename)"/>
    <xsl:choose>
      <xsl:when test="starts-with($url, $site)">
        <fo:external-graphic src="url({concat('..', substring-after($url, $site))})"
                             height="35mm" content-height="auto"
                             margin=".25em"/>
      </xsl:when>
      <xsl:otherwise>
        <fo:external-graphic src="url({$url})"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="toc">
    <fo:block break-before="page">
      <fo:block font-size="20pt" text-align-last="center" font-weight="bold">
        Table of Contents
      </fo:block>
      <fo:block text-align-last="justify"><fo:leader leader-pattern="rule"/></fo:block>
      <xsl:apply-templates select="//chapter|//section|//stf:person|//vol:volunteer" mode="toc"/>
    </fo:block>
  </xsl:template>

  <xsl:template match="chapter|section" mode="toc">
    <xsl:element name="fo:block">
      <xsl:attribute name="space-before">10pt</xsl:attribute>
      <xsl:attribute name="font-weight">bold</xsl:attribute>
      <xsl:attribute name="text-align-last">justify</xsl:attribute>
      <xsl:if test="local-name() = 'section'">
        <xsl:attribute name="margin-left">1em</xsl:attribute>
      </xsl:if>
      <fo:basic-link internal-destination="{generate-id()}" color="blue">
        <xsl:value-of select="title"/>
      </fo:basic-link>
      <fo:leader leader-pattern="dots"/>
      <fo:basic-link internal-destination="{generate-id()}" color="blue">
        <fo:page-number-citation ref-id="{generate-id()}"/>
      </fo:basic-link>
    </xsl:element>
  </xsl:template>

  <xsl:template match="stf:person|vol:volunteer" mode="toc"/>

  <xsl:template match="chapter|section" mode="fox-toc">
    <fox:outline internal-destination="{generate-id()}">
      <fox:label><xsl:value-of select="title"/></fox:label>
      <xsl:apply-templates mode="fox-toc">
        <xsl:sort select="xnl:PersonName/xnl:LastName"/>
      </xsl:apply-templates>
    </fox:outline>
  </xsl:template>

  <xsl:template match="stf:staff|vol:volunteers" mode="fox-toc">
    <xsl:apply-templates mode="fox-toc">
      <xsl:sort select="xnl:PersonName/xnl:LastName"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="stf:person|vol:volunteer" mode="fox-toc">
    <fox:outline internal-destination="{generate-id()}">
      <fox:label><xsl:apply-templates select="xnl:PersonName"/></fox:label>
    </fox:outline>
  </xsl:template>
</xsl:stylesheet>
