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

<xsl:variable name="font" select="'sans-serif'"/>
<xsl:template match="ganttproject:report">
  <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    &layout;

	    <fo:page-sequence master-reference="simple">

      <fo:static-content flow-name="xsl-region-before">
        <fo:block text-align="right" font-size="9pt">
	      <xsl:value-of select="//ganttproject:project/@nameValue"/>
	    </fo:block>
        <fo:block text-align="right" font-size="9pt">
	      <xsl:value-of select="//ganttproject:project/@beginValue"/><xsl:text>-</xsl:text><xsl:value-of select="//ganttproject:project/@endValue"/>
	    </fo:block>
      </fo:static-content>
      <fo:static-content flow-name="xsl-region-after">
        <fo:block text-align="right" font-size="9pt">
          <xsl:attribute name="font-family">
            <xsl:value-of select="$font" />
          </xsl:attribute>
	      Page <fo:page-number/>
	  </fo:block>
      </fo:static-content>

      <fo:flow flow-name="xsl-region-body">

        <fo:block font-size="15pt"
                  line-height="24pt"
                  space-after.optimum="15pt"
                  color="black"
                  text-align="center"
                  padding-top="3pt">
          <xsl:attribute name="font-family">
            <xsl:value-of select="$font" />
          </xsl:attribute>

          <xsl:apply-templates select="ganttproject:project" />
        </fo:block>


        <fo:block font-size="10pt"
                  line-height="24pt"
                  space-after.optimum="15pt"
                  color="black"
                  text-align="center"
                  padding-top="2pt"
                  padding-bottom="5pt">
          <xsl:attribute name="font-family">
            <xsl:value-of select="$font" />
          </xsl:attribute>
          <xsl:apply-templates select="ganttproject:tasks" />
        </fo:block>


        <fo:block font-size="10pt"
                  line-height="24pt"
                  space-after.optimum="15pt"
                  color="black"
                  text-align="center"
                  padding-top="2pt">
          <xsl:attribute name="font-family">
            <xsl:value-of select="$font" />
          </xsl:attribute>
          <xsl:apply-templates select="ganttproject:resources" />
        </fo:block>


        <fo:block font-size="10pt"
                  line-height="24pt"
                  space-after.optimum="15pt"
                  color="black"
                  text-align="center"
                  padding-top="3pt">
          <xsl:attribute name="font-family">
            <xsl:value-of select="$font" />
          </xsl:attribute>
          <xsl:apply-templates select="ganttproject:ganttchart" />
        </fo:block>

        <fo:block font-size="10pt"
                  line-height="24pt"
                  space-after.optimum="15pt"
                  color="black"
                  text-align="center"
                  padding-top="3pt">
          <xsl:attribute name="font-family">
            <xsl:value-of select="$font" />
          </xsl:attribute>
          <xsl:apply-templates select="ganttproject:resourceschart" />
        </fo:block>
      </fo:flow>
    </fo:page-sequence>
  </fo:root>
</xsl:template>


<!-- ========================================== Project settings ========================================== -->
<xsl:template match="ganttproject:project">

  <fo:block font-size="20pt"
            line-height="16pt"
            background-color="#145277"
            color="white"
            space-after.optimum="15pt"
            text-align="center"
            padding-top="8pt"
            padding-bottom="8pt">
    <xsl:attribute name="font-family">
      <xsl:value-of select="$font" />
    </xsl:attribute>
    <xsl:apply-templates select="@title" />
  </fo:block>

  <fo:block font-size="10pt"
            line-height="1pt"
            color="#145277"
            space-after.optimum="2pt"
            text-align="left"
            padding-top="15pt">
    <xsl:attribute name="font-family">
      <xsl:value-of select="$font" />
    </xsl:attribute>
    <xsl:apply-templates select="@name" />
    <xsl:text> : </xsl:text>
    <xsl:apply-templates select="@nameValue" />
  </fo:block>

  <fo:block font-size="10pt"
            line-height="1pt"
            color="#145277"
            space-after.optimum="2pt"
            text-align="left"
            padding-top="15pt">
    <xsl:attribute name="font-family">
      <xsl:value-of select="$font" />
    </xsl:attribute>
    <xsl:apply-templates select="@begin" />
    <xsl:text> : </xsl:text>
    <xsl:apply-templates select="@beginValue" />
  </fo:block>

  <fo:block font-size="10pt"
            line-height="1pt"
            color="#145277"
            space-after.optimum="2pt"
            text-align="left"
            padding-top="15pt">
    <xsl:attribute name="font-family">
      <xsl:value-of select="$font" />
    </xsl:attribute>
    <xsl:apply-templates select="@end" />
    <xsl:text> : </xsl:text>
    <xsl:apply-templates select="@endValue" />
  </fo:block>

  <fo:block font-size="10pt"
            line-height="1pt"
            color="#145277"
            space-after.optimum="2pt"
            text-align="left"
            padding-top="15pt">
    <xsl:attribute name="font-family">
      <xsl:value-of select="$font" />
    </xsl:attribute>
    <xsl:apply-templates select="@organisation" />
    <xsl:text> : </xsl:text>
    <xsl:apply-templates select="@organisationValue" />
  </fo:block>

  <fo:block font-size="10pt"
            line-height="1pt"
            color="#145277"
            space-after.optimum="2pt"
            text-align="left"
            padding-top="15pt">
    <xsl:attribute name="font-family">
      <xsl:value-of select="$font" />
    </xsl:attribute>
    <xsl:apply-templates select="@webLink" />
    <xsl:text> : </xsl:text>
    <xsl:apply-templates select="@webLinkValue" />
  </fo:block>

  <fo:block font-size="10pt"
            color="#145277"
            space-after.optimum="2pt"
            text-align="left"
            padding-top="15pt">
    <xsl:attribute name="font-family">
      <xsl:value-of select="$font" />
    </xsl:attribute>
    <xsl:apply-templates select="@description" />
    <xsl:text> : </xsl:text>
    <fo:block font-size="8pt"
              start-indent="10pt"
              line-height="10pt"
              white-space-collapse="false">
      <xsl:value-of select="descriptionValue/text()" />
    </fo:block>
  </fo:block>


  <fo:block font-size="10pt"
            line-height="1pt"
            color="#145277"
            space-after.optimum="2pt"
            text-align="left"
            padding-top="15pt">
    <xsl:attribute name="font-family">
      <xsl:value-of select="$font" />
    </xsl:attribute>
    <xsl:text>Date : </xsl:text>
    <xsl:apply-templates select="@currentDateTimeValue" />
  </fo:block>


</xsl:template>

<!-- ========================================== Task List ========================================== -->
<xsl:template match="ganttproject:tasks">
  <xsl:variable name="tasks-i18n" select="." />
  <fo:block font-size="20pt"
            line-height="20pt"
            background-color="#145277"
            color="white"
            text-align="center"
            break-before="page">
    <xsl:attribute name="font-family">
      <xsl:value-of select="$font" />
    </xsl:attribute>
    <xsl:apply-templates select="@title" />
  </fo:block>


  <fo:block space-after.optimum="0pt" font-size="10pt" padding-bottom="30pt">
    <xsl:attribute name="font-family">
      <xsl:value-of select="$font" />
    </xsl:attribute>

    <fo:table border-bottom-color="#bbbbbb"
              border-bottom-width="2pt"
              border-bottom-style="solid">
      <fo:table-column column-width="1em" />
      <xsl:for-each select="//view[@id='task-table']/field">
        <fo:table-column column-width="proportional-column-width({@width})" />
      </xsl:for-each>
      <fo:table-column column-width="10em" />
      <fo:table-body>
        <fo:table-row background-color="#bbbbbb"
                      color="black"
                      font-size="11pt">
          <fo:table-cell/>

          <xsl:for-each select="//view[@id='task-table']/field">
            <fo:table-cell>
              <xsl:variable name="padding-top"
                            select="((position()-1) mod 3)*13" />
              <fo:block space-after.optimum="0pt"
                        space-before.optimum="0pt"
                        wrap-option="no-wrap"
                        text-align="left">
                <xsl:attribute name="padding-top">
                  <xsl:value-of select="concat(string($padding-top), 'pt')" />
                </xsl:attribute>
                <xsl:value-of select="@name" />
              </fo:block>
            </fo:table-cell>
          </xsl:for-each>

          <fo:table-cell>
            <fo:block text-align="left">
              <xsl:value-of select="$tasks-i18n/@assigned-to" />
            </fo:block>
          </fo:table-cell>
        </fo:table-row>

        <xsl:for-each select="ganttproject:task">
          <xsl:variable name="current-task" select="." />
          <fo:table-row color="black"
                        font-size="9pt"
                        space-after.optimum="0pt"
                        space-before.optimum="0pt"
                        line-height="13pt"
                        text-align="left"
                        background-color="white">

            <fo:table-cell border-left-color="#bbbbbb"
                           border-left-width="1pt"
                           border-left-style="solid"
                           border-right-color="#bbbbbb"
                           border-right-width="1pt"
                           border-right-style="solid">
              <xsl:attribute name="background-color">
                <xsl:value-of select="$current-task/color" />
              </xsl:attribute>
              <fo:block>
              </fo:block>
            </fo:table-cell>
            <xsl:for-each select="//view[@id='task-table']/field">
              <xsl:variable name="field-id" select="@id" />

              <xsl:variable name="indent">
                <xsl:choose>
                  <xsl:when test="$field-id='tpd3'">
                    <xsl:value-of select="(($current-task/@depth)+1)*0.5"/>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:text>0.5</xsl:text>
                  </xsl:otherwise>
                </xsl:choose>
                <xsl:text>em</xsl:text>
              </xsl:variable>

              <fo:table-cell>
                <fo:block start-indent="{$indent}">
                  <xsl:for-each select='$current-task//*[@id=$field-id]'>
                    <xsl:value-of select="text()" />
                  </xsl:for-each>
                  <xsl:if test="$field-id='tpd3'">
                    <xsl:if test="$current-task/notes/text()">
	                    <fo:block font-size="8pt"
	                              start-indent="10pt+{$indent}"
	                              line-height="10pt"
	                              white-space-collapse="false">
	                      <xsl:value-of select='$current-task/notes/text()' />
	                    </fo:block>
	                </xsl:if>
                    <xsl:if test="$current-task/attachment/text()">
	                    <fo:block font-size="8pt"
	                              start-indent="10pt+{$indent}"
	                              line-height="10pt"
	                              white-space-collapse="false">
	                      <fo:basic-link color="blue"
	                                     external-destination="url({$current-task/attachment/text()})">
	                        <xsl:choose>
		                        <xsl:when test="$current-task/attachment/@display-name">
			                        <xsl:value-of select='$current-task/attachment/@display-name'/>
		                        </xsl:when>
		                        <xsl:otherwise>
			                        <xsl:value-of select='$current-task/attachment/text()'/>
		                        </xsl:otherwise>
	                        </xsl:choose>
	                      </fo:basic-link>
	                    </fo:block>
                    </xsl:if>
                  </xsl:if>
                </fo:block>
              </fo:table-cell>
            </xsl:for-each>
            <!-- field -->
            <fo:table-cell white-space-collapse="false"
                           border-right-color="#bbbbbb"
                           border-right-width="1pt"
                           border-right-style="solid">
              <fo:block start-indent="0.5em">
                <xsl:value-of select="assigned-to" />
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
        </xsl:for-each>
      </fo:table-body>
    </fo:table>

  </fo:block>

</xsl:template>

<!-- ========================================== Resources List ========================================== -->
<xsl:template match="ganttproject:resources">
  <fo:block font-size="20pt"
            line-height="16pt"
            background-color="#145277"
            color="white"
            text-align="center"
            padding-top="8pt"
            padding-bottom="8pt"
            break-before="page">
    <xsl:attribute name="font-family">
      <xsl:value-of select="$font" />
    </xsl:attribute>
    <xsl:apply-templates select="@title" />
  </fo:block>


  <fo:block space-after.optimum="0pt" font-size="10pt" padding-bottom="30pt">
    <xsl:attribute name="font-family">
      <xsl:value-of select="$font" />
    </xsl:attribute>

    <fo:table border-bottom-color="#bbbbbb"
              border-bottom-width="2pt"
              border-bottom-style="solid">
      <xsl:for-each select="//view[@id='resource-table']/field">
        <fo:table-column column-width="proportional-column-width({@width})" />
      </xsl:for-each>
      <fo:table-body>
        <fo:table-row background-color="#bbbbbb" color="black" font-size="11pt">
          <xsl:for-each select="//view[@id='resource-table']/field">
            <fo:table-cell>
              <xsl:variable name="padding-top"
                            select="((position()-1) mod 3)*13" />
              <fo:block space-after.optimum="0pt"
                        space-before.optimum="0pt"
                        wrap-option="no-wrap"
                        text-align="left">
                <xsl:attribute name="padding-top">
                  <xsl:value-of select="concat(string($padding-top), 'pt')" />
                </xsl:attribute>
                <xsl:value-of select="@name" />
              </fo:block>
            </fo:table-cell>
          </xsl:for-each>
        </fo:table-row>



        <xsl:for-each select="ganttproject:resource">
          <xsl:variable name="current-resource" select="." />
          <fo:table-row>
            <xsl:for-each select="//view[@id='resource-table']/field">
              <xsl:variable name="field-id" select="@id" />
              <fo:table-cell>
                <fo:block start-indent="0.5em"
                          text-align="left">
                  <xsl:for-each select='$current-resource//*[@id=$field-id]'>
                    <xsl:value-of select="text()" />
                  </xsl:for-each>
                </fo:block>
              </fo:table-cell>
            </xsl:for-each>
            <!-- field -->
          </fo:table-row>
        </xsl:for-each>

      </fo:table-body>
    </fo:table>
  </fo:block>

</xsl:template>

<!-- ========================================== GanttChart ========================================== -->
<xsl:template match="ganttproject:ganttchart">
  <fo:block font-size="20pt"
            line-height="16pt"
            background-color="#145277"
            color="white"
            space-after.optimum="15pt"
            text-align="center"
            padding-top="8pt"
            padding-bottom="8pt"
            break-before="page">
    <xsl:attribute name="font-family">
      <xsl:value-of select="$font" />
    </xsl:attribute>
    <xsl:apply-templates select="@title" />
  </fo:block>

  <!-- Show the real chart from a picture -->
  <fo:block text-align="right">
    <fo:external-graphic>
      <xsl:attribute name="src">
        <xsl:value-of select="@src" />
      </xsl:attribute>
      <xsl:attribute name="height">24cm</xsl:attribute>
    </fo:external-graphic>
  </fo:block>
</xsl:template>

<!-- ========================================== ResourcesCHART ========================================== -->
<xsl:template match="ganttproject:resourceschart">
  <fo:block font-size="20pt"
            line-height="16pt"
            background-color="#145277"
            color="white"
            space-after.optimum="15pt"
            text-align="center"
            padding-top="8pt"
            padding-bottom="8pt"
            break-before="page">
    <xsl:attribute name="font-family">
      <xsl:value-of select="$font" />
    </xsl:attribute>
    <xsl:apply-templates select="@title" />
  </fo:block>

  <!-- Show the real chart from a picture -->
  <fo:block text-align="right">
    <fo:external-graphic>
      <xsl:attribute name="src">
        <xsl:value-of select="@src" />
      </xsl:attribute>
      <xsl:attribute name="height">24cm</xsl:attribute>
    </fo:external-graphic>
  </fo:block>
</xsl:template>
