﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <xsl:output method="xml" indent="yes"/>

    <xsl:key name="distinctSeqNumDataDump" match="AjaxRequest/Survey/SurveyTakenAnswers/DataDump/Record" use="./DataListCategory"></xsl:key>
    <xsl:key name="distinctSeqNum" match="AjaxRequest/Survey/SurveyTakenAnswers" use="./ParentQuestionID"></xsl:key>
    
    <xsl:variable name="Level1MarginLeft">
        <xsl:text>20pt</xsl:text>
    </xsl:variable>
    <xsl:variable name="Level2MarginLeft">
        <xsl:text>40pt</xsl:text>
    </xsl:variable>
    <xsl:variable name="BlockLevelMargin">
        <xsl:text>10pt</xsl:text>
    </xsl:variable>
    <xsl:variable name="StatementLevelMargin">
        <xsl:text>2pt</xsl:text>
    </xsl:variable>    
    <xsl:variable name="DefaultFontColor">
        <xsl:text>#4B4B4B</xsl:text>
    </xsl:variable>

    <xsl:template match="/">

        <xsl:variable name="SurveyTakenAnswers" select="AjaxRequest/Survey/SurveyTakenAnswers/."></xsl:variable>
        <xsl:variable name="HostName" select="normalize-space(AjaxRequest/HostName)"></xsl:variable>
        <xsl:variable name="DomainName" select="normalize-space(AjaxRequest/DomainName)"></xsl:variable>
        <xsl:variable name="PortalItemID" select="AjaxRequest/Survey/PortalItemID"></xsl:variable>
        <xsl:variable name="SurveyID" select="AjaxRequest/Survey/SurveyID"></xsl:variable>
        <xsl:variable name="DisplayName" select="AjaxRequest/Survey/DisplayName"></xsl:variable>
        <xsl:variable name="UpdateDate" select="AjaxRequest/Survey/UpdateDate"></xsl:variable>
        <xsl:variable name="SurveyName" select="AjaxRequest/Survey/SurveyName"></xsl:variable>
        <xsl:variable name="Description" select="AjaxRequest/Survey/Description"></xsl:variable>

        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master master-name="A4-portrait-workout"
                          page-height="29.7cm" page-width="21.0cm" margin="2cm">
                    <fo:region-body/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="A4-portrait-workout">
                <fo:flow flow-name="xsl-region-body">
                    <xsl:call-template name="SurveyHeader">
                        <xsl:with-param name="SurveyName" select="$SurveyName"></xsl:with-param>
                        <xsl:with-param name="Description" select="$Description"></xsl:with-param>
                        <xsl:with-param name="DisplayName" select="$DisplayName"></xsl:with-param>
                        <xsl:with-param name="UpdateDate" select="$UpdateDate"></xsl:with-param>
                        <xsl:with-param name="HostName" select="$HostName"></xsl:with-param>
                        <xsl:with-param name="DomainName" select="$DomainName"></xsl:with-param>
                    </xsl:call-template>
                    <fo:block margin-top="5pt">
                        <fo:leader leader-length="7in" leader-pattern="rule" baseline-shift="left" alignment-baseline="middle"
                                    rule-thickness="0.5pt" color="black"/>
                    </fo:block>
                    <fo:block text-align="left" space-before="2mm" space-after="4mm" background-color="#D2D3D4">
                        <fo:inline font-family="PT Sans , sans-serif" font-size="9.25pt" color="#4B4B4B" font-weight="bold">
                        </fo:inline>
                    </fo:block>

                    <fo:table>
                        <fo:table-column column-width="2.5cm"/>
                        <fo:table-column column-width="7.7cm"/>
                        <fo:table-column column-width="8.5cm"/>
                        <fo:table-body>
                            <xsl:for-each select="$SurveyTakenAnswers[generate-id() = generate-id(key('distinctSeqNum', ./ParentQuestionID))]">
                                
                                <xsl:call-template name="SurveyQuestionRow">
                                    <xsl:with-param name="HostName" select="$HostName"></xsl:with-param>
                                    <xsl:with-param name="PortalItemID" select="$PortalItemID"></xsl:with-param>
                                    <xsl:with-param name="SurveyID" select="$SurveyID"></xsl:with-param>
                                    <xsl:with-param name="QuestionID" select="QuestionID"></xsl:with-param>
                                    <xsl:with-param name="ControlID" select="ControlID"></xsl:with-param>

                                    <xsl:with-param name="DataDumpRecords" select="$SurveyTakenAnswers/DataDump/."></xsl:with-param>

                                    <xsl:with-param name="ParentControlType" select="ParentControlType"></xsl:with-param>
                                    <xsl:with-param name="ParentQuestionStatement" select="ParentQuestionStatement"></xsl:with-param>
                                    <xsl:with-param name="Statement" select="Statement"></xsl:with-param>
                                </xsl:call-template>

                                <xsl:call-template name="SurveyAnswerRow">
                                    <xsl:with-param name="ParentControlType" select="ParentControlType"></xsl:with-param>
                                    <xsl:with-param name="SurveyTakenAnswers" select="$SurveyTakenAnswers"></xsl:with-param>
                                    <xsl:with-param name="ParentQuestionID" select="ParentQuestionID"></xsl:with-param>
                                    <xsl:with-param name="ParentQuestionTotal" select="ParentQuestionTotal"></xsl:with-param>
                                    <xsl:with-param name="ParentQuestionAverage" select="ParentQuestionAverage"></xsl:with-param>
                                    <xsl:with-param name="QuestionID" select="QuestionID"></xsl:with-param>
                                </xsl:call-template>
                                

                                <fo:table-row>
                                    <fo:table-cell number-columns-spanned="3">
                                        <fo:block padding="5pt"></fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                            </xsl:for-each>
                        </fo:table-body>
                    </fo:table>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>

    <xsl:template name="SurveyHeader">
        <xsl:param name="SurveyName" select="'N/A'"></xsl:param>
        <xsl:param name="Description" select="'N/A'"></xsl:param>
        <xsl:param name="DisplayName" select="'N/A'"></xsl:param>
        <xsl:param name="UpdateDate" select="'N/A'"></xsl:param>
        <xsl:param name="HostName" select="'N/A'"></xsl:param>
        <xsl:param name="DomainName" select="'N/A'"></xsl:param>

        <xsl:variable name="RightCellWidth">
            <xsl:choose>
                <xsl:when test="string-length($DisplayName) &gt; string-length($SurveyName)">
                    <xsl:value-of select="round(1.034 * string-length($DisplayName))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="round(1.034 * string-length($SurveyName))"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="LeftCellWidth" select="100 - $RightCellWidth"></xsl:variable>
        
        <fo:table>
            <fo:table-column column-width="{$LeftCellWidth}%"/>
            <fo:table-column column-width="{$RightCellWidth}%"/>
            <fo:table-body>
                <fo:table-row>
                    <fo:table-cell>
                        <fo:block margin-bottom="5pt">
                            <fo:inline>
                                <fo:external-graphic src="http://{$HostName}/Portals/0/Brands/{$DomainName}/Logo.jpg" content-height="50%" content-width="50%"/>
                            </fo:inline>
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell>
                        <fo:block>
                            <fo:inline font-family="PT Sans ,  sans-serif" font-size="9.50pt" color="#4B4B4B" font-weight="bold">
                                <xsl:value-of select="$SurveyName"/> 
                            </fo:inline>
                        </fo:block>
                        <!--<fo:block>
                            <fo:inline font-family="PT Sans ,  sans-serif" font-size="9.50pt" color="#4B4B4B" font-weight="bold">
                                <xsl:value-of select="$RightCellWidth"></xsl:value-of> - <xsl:value-of select="$LeftCellWidth"/> - <xsl:value-of select="string-length($SurveyName)" />
                            </fo:inline>
                        </fo:block>-->                        
                        <fo:block>
                            <fo:inline font-family="PT Sans , sans-serif" font-size="9.50pt" color="#4B4B4B" font-weight="bold">
                                <xsl:value-of select="$DisplayName"/>
                            </fo:inline>
                        </fo:block>
                        <fo:block>
                            <fo:inline font-family="PT Sans , sans-serif" font-size="9.50pt" color="#4B4B4B" font-weight="bold">
                                <xsl:value-of select="$UpdateDate"/>
                            </fo:inline>
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
            </fo:table-body>
        </fo:table>

        <!--<fo:block text-align="center" space-before="2mm" space-after="2mm"  font-family="Calibri, Verdana, Arial" font-size="12pt" font-weight="bold" color="{$DefaultFontColor}">
            <xsl:value-of select="$SurveyName"/>
        </fo:block>
        <fo:block text-align="center" space-before="2mm" space-after="2mm"  font-family="Calibri, Verdana, Arial" font-size="10pt" font-weight="bold" color="{$DefaultFontColor}">
            <xsl:value-of select="$Description"/>
        </fo:block>-->
    </xsl:template>

    <xsl:template name="SurveyTakenSignature">
        <xsl:param name="DisplayName" select="'N/A'"></xsl:param>
        <xsl:param name="UpdateDate" select="'N/A'"></xsl:param>
        <fo:block text-align="left" space-before="2mm" space-after="2mm" font-family="Calibri, Verdana, Arial" font-size="12pt" font-weight="bold" color="{$DefaultFontColor}">
            <fo:table>
                <fo:table-column/>
                <fo:table-body>
                    <fo:table-row>
                        <fo:table-cell>
                            <fo:block text-align="left" font-family="Calibri, Verdana, Arial" font-size="10pt" font-weight="bold" color="{$DefaultFontColor}">
                                <fo:inline>
                                    <xsl:value-of select="$DisplayName"/>
                                </fo:inline>
                                <fo:inline space-start="2mm">
                                    <xsl:value-of select="$UpdateDate"/>
                                </fo:inline>
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                </fo:table-body>
            </fo:table>
        </fo:block>
    </xsl:template>


    <xsl:template name="SurveyQuestionRow">
        <xsl:param name="HostName" select="'N/A'"></xsl:param>
        <xsl:param name="PortalItemID" select="'N/A'"></xsl:param>
        <xsl:param name="SurveyID" select="'N/A'"></xsl:param>
        <xsl:param name="QuestionID" select="'N/A'"></xsl:param>
        <xsl:param name="ControlID" select="'N/A'"></xsl:param>

        <xsl:param name="DataDumpRecords" select="'N/A'"></xsl:param>

        <xsl:param name="ParentControlType" select="'N/A'"></xsl:param>
        <xsl:param name="ParentQuestionStatement" select="'N/A'"></xsl:param>
        <xsl:param name="Statement" select="'N/A'"></xsl:param>

        <fo:table-row>
            <fo:table-cell number-columns-spanned="3">

                <xsl:choose>
                    <xsl:when test="ControlType = 'Graph'">
                        <xsl:call-template name="Graph">
                            <xsl:with-param name="HostName" select="$HostName"></xsl:with-param>
                            <xsl:with-param name="PortalItemID" select="$PortalItemID"></xsl:with-param>
                            <xsl:with-param name="SurveyID" select="$SurveyID"></xsl:with-param>
                            <xsl:with-param name="QuestionID" select="$QuestionID"></xsl:with-param>
                            <xsl:with-param name="ControlID" select="$ControlID"></xsl:with-param>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:when test="ControlType = 'DataDump'">
                        <xsl:call-template name="DataDump">
                            <xsl:with-param name="DataDumpRecords" select="$DataDumpRecords"></xsl:with-param>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:call-template name="SurveyQuestionStatement">
                            <xsl:with-param name="ParentControlType" select="$ParentControlType"></xsl:with-param>
                            <xsl:with-param name="ParentQuestionStatement" select="$ParentQuestionStatement"></xsl:with-param>
                            <xsl:with-param name="Statement" select="$Statement"></xsl:with-param>
                        </xsl:call-template>
                    </xsl:otherwise>
                </xsl:choose>

            </fo:table-cell>
        </fo:table-row>
    </xsl:template>

    <xsl:template name="SurveyAnswerRow">
        <xsl:param name="ParentControlType" select="'N/A'"></xsl:param>
        <xsl:param name="SurveyTakenAnswers" select="'N/A'"></xsl:param>
        <xsl:param name="ParentQuestionID" select="'N/A'"></xsl:param>
        <xsl:param name="ParentQuestionTotal" select="'N/A'"></xsl:param>
        <xsl:param name="ParentQuestionAverage" select="'N/A'"></xsl:param>
        <xsl:param name="QuestionID" select="'N/A'"></xsl:param>
        
        <fo:table-row>
            <fo:table-cell number-columns-spanned="3">
                <xsl:choose>
                    <xsl:when test="$ParentControlType = 'Matrix' or $ParentControlType = 'RatingMatrix'">
                        <xsl:for-each select="$SurveyTakenAnswers[ParentQuestionID = $ParentQuestionID]" >
                            <fo:block text-align="left" font-family="Calibri, Verdana, Arial" font-size="9pt"  color="{$DefaultFontColor}"
                                      margin-left="{$Level1MarginLeft}"  margin-top="{$StatementLevelMargin}">
                                <xsl:value-of select="Statement"/>
                            </fo:block>
                            <fo:block text-align="left" font-family="Calibri, Verdana, Arial" font-size="8pt" color="{$DefaultFontColor}"
                                      margin-left="{$Level2MarginLeft}">
                                <xsl:choose>
                                    <xsl:when test="Option[text()]">
                                        <xsl:value-of select="Option"/>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:value-of select="Answer"/>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </fo:block>
                        </xsl:for-each>
                        <xsl:if test="ParentControlType = 'RatingMatrix'">
                            <fo:block text-align="left" font-family="Calibri, Verdana, Arial" font-size="9pt"  color="{$DefaultFontColor}" 
                                      margin-left="{$Level1MarginLeft}" margin-top="{$StatementLevelMargin}">
                                Total = <xsl:value-of select="format-number($ParentQuestionTotal, '#,###')"/>, Average = <xsl:value-of select="format-number($ParentQuestionAverage, '#,###')"/>
                            </fo:block>
                        </xsl:if>
                    </xsl:when>
                    <xsl:when test="$ParentControlType = 'QuickListText'">
                        <xsl:for-each select="$SurveyTakenAnswers[ParentQuestionID = $ParentQuestionID]" >
                            <fo:block text-align="left" font-family="Calibri, Verdana, Arial" font-size="8pt" color="{$DefaultFontColor}"
                                      margin-left="{$Level1MarginLeft}" margin-top="{$StatementLevelMargin}">
                                <xsl:value-of select="Answer"/>
                            </fo:block>
                        </xsl:for-each>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:for-each select="$SurveyTakenAnswers[QuestionID = $QuestionID]" >
                            <fo:block text-align="left" font-family="Calibri, Verdana, Arial" font-size="8pt" color="{$DefaultFontColor}"
                                      margin-left="{$Level1MarginLeft}" margin-top="{$StatementLevelMargin}">
                                <xsl:value-of select="Answer"/>
                            </fo:block>
                        </xsl:for-each>
                        <fo:block text-align="left" font-family="Calibri, Verdana, Arial" font-size="10pt"  color="{$DefaultFontColor}" margin-left="{$Level1MarginLeft}">
                        </fo:block>
                    </xsl:otherwise>
                </xsl:choose>
            </fo:table-cell>

        </fo:table-row>
    </xsl:template>

    <xsl:template name="Graph">
        <xsl:param name="HostName" select="'N/A'"></xsl:param>
        <xsl:param name="PortalItemID" select="'N/A'"></xsl:param>
        <xsl:param name="SurveyID" select="'N/A'"></xsl:param>
        <xsl:param name="QuestionID" select="'N/A'"></xsl:param>
        <xsl:param name="ControlID" select="'N/A'"></xsl:param>

        <fo:block text-align="left" font-family="Calibri, Verdana, Arial" font-size="10pt"  color="{$DefaultFontColor}">
            <fo:external-graphic
                src="http://{$HostName}/DesktopModules/LifeSpaceTemplates/Web/Images/SurveyChartImages/SurveyGraphOmage_{$PortalItemID}_{$SurveyID}_{$QuestionID}_{$ControlID}.png"
                content-height="50%" content-width="50%"/>
        </fo:block>
    </xsl:template>

    <xsl:template name="DataDump">
        <xsl:param name="DataDumpRecords" select="'N/A'"></xsl:param>

        <xsl:if test="$DataDumpRecords/HeaderContent[text()]">
            <fo:block text-align="left" font-family="Calibri, Verdana, Arial" font-size="9pt"  color="#4645A9" margin-bottom="{$BlockLevelMargin}">
                <xsl:value-of select="$DataDumpRecords/HeaderContent"></xsl:value-of>
            </fo:block>
        </xsl:if>

        <xsl:for-each select="$DataDumpRecords/Record[generate-id() = generate-id(key('distinctSeqNumDataDump', ./DataListCategory))]">
            <xsl:variable name="DataListCategory" select="DataListCategory"></xsl:variable>

            <fo:block text-align="left" font-family="Calibri, Verdana, Arial" font-size="9pt"  color="{$DefaultFontColor}" margin-left="{$Level1MarginLeft}"
                      margin-top="{$BlockLevelMargin}">
                <xsl:value-of select="$DataListCategory"/>
            </fo:block>


            <xsl:for-each select="$DataDumpRecords/Record[DataListCategory = $DataListCategory]">
                <fo:block text-align="left" font-family="Calibri, Verdana, Arial" font-size="8pt"  color="{$DefaultFontColor}"
                          margin-left="{$Level2MarginLeft}" margin-top="{$StatementLevelMargin}">
                    <xsl:value-of select="DataListText"/>
                </fo:block>
            </xsl:for-each>


        </xsl:for-each>

        <fo:block padding="5pt"></fo:block>
    </xsl:template>

    <xsl:template name="SurveyQuestionStatement">
        <xsl:param name="ParentControlType" select="'N/A'"></xsl:param>
        <xsl:param name="ParentQuestionStatement" select="'N/A'"></xsl:param>
        <xsl:param name="Statement" select="'N/A'"></xsl:param>

        <fo:block text-align="left" font-family="Calibri, Verdana, Arial" font-size="9pt" color="{$DefaultFontColor}">
            <xsl:choose>
                <xsl:when test="$ParentControlType = 'Matrix' or $ParentControlType = 'RatingMatrix' or $ParentControlType = 'QuickListText'">
                    <xsl:value-of select="$ParentQuestionStatement"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$Statement"/>
                </xsl:otherwise>
            </xsl:choose>
        </fo:block>
    </xsl:template>

</xsl:stylesheet>