﻿<?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:key name="distinctSeqNum" match="AjaxRequest/Workout/WorkoutDetail" use="./SeqNum"></xsl:key>

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <xsl:variable name="WorkoutName" select="AjaxRequest/Workout/WorkoutDetail/WorkoutName"></xsl:variable>
        <xsl:variable name="WorkoutDueDate" select="AjaxRequest/Workout/WorkoutDetail/WorkoutDueDate"></xsl:variable>
        <xsl:variable name="ResponsiblePerson" select="AjaxRequest/Workout/WorkoutDetail/ResponsiblePerson"></xsl:variable>
        <xsl:variable name="ThumbnailURL" select="AjaxRequest/Workout/WorkoutDetail/ThumbnailURL"></xsl:variable>
        <xsl:variable name="BrandLogo" select="AjaxRequest/Workout/WorkoutDetail/BrandLogo"></xsl:variable>
        <xsl:variable name="TotalCalories" select="AjaxRequest/Workout/WorkoutDetail/TotalCalories"></xsl:variable>
        <xsl:variable name="TotalTime" select="AjaxRequest/Workout/WorkoutDetail/TotalTime"></xsl:variable>
        <xsl:variable name="TotalWorkoutTime" select="AjaxRequest/Workout/WorkoutDetail/TotalWorkoutTime"></xsl:variable>
        <xsl:variable name="TotalDistance" select="AjaxRequest/Workout/WorkoutDetail/TotalDistance"></xsl:variable>
        <xsl:variable name="WorkoutNotesCount" select="count(AjaxRequest/Workout/WorkoutNotes/Note)"></xsl:variable>
        <xsl:variable name="AccountName" select="AjaxRequest/Workout/WorkoutDetail/AccountName"></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">
                    <fo:block text-align="center" space-before="2mm" space-after="2mm">
                        <fo:external-graphic src="{$BrandLogo}" content-height="7em" content-width="7em"/>
                    </fo:block>
                    <fo:block text-decoration="underline" text-align="center" font-weight="bold" space-before="2mm" space-after="2mm" color="#376092">
                        <xsl:value-of select="$WorkoutName"/>
                    </fo:block>
                    <fo:block text-align="center" space-before="2mm" space-after="2mm">
                        <fo:inline font-family="arial" font-size="12pt" font-weight="bold"> Username: </fo:inline>
                        <xsl:value-of select="$ResponsiblePerson"/>
                    </fo:block>
                    <xsl:if test="$AccountName  != ''">
                        <fo:block text-align="center" space-before="2mm" space-after="2mm">
                            <fo:inline font-family="arial" font-size="12pt" font-weight="bold"> Account: </fo:inline>
                            <xsl:value-of select="$AccountName"/>
                        </fo:block>
                    </xsl:if>
                    <fo:block text-align="center" space-before="2mm" space-after="15mm">
                        <xsl:value-of select="$WorkoutDueDate"/>
                    </fo:block>

                    <fo:block text-align="left" space-before="2mm" space-after="2mm" font-family="Calibri, Verdana, Arial" font-size="10pt" font-weight="bold">
                        Workout Summary
                    </fo:block>

                    <fo:table>
                        <fo:table-column column-width="16.0cm"/>
                        <fo:table-body>
                            <xsl:if test="$TotalWorkoutTime &gt; 0">
                                <fo:table-row>
                                    <fo:table-cell>
                                        <fo:block margin-bottom="5pt" text-align="left" font-family="Calibri, Verdana, Arial" font-size="9pt">
                                            <fo:inline font-family="Calibri, Verdana, Arial" font-size="10pt" font-weight="bold" color="#376092">Total Time:</fo:inline>&#xa0;
                                            <fo:inline font-family="Calibri, Verdana, Arial" font-size="9pt" font-weight="bold">
                                                <xsl:value-of select="$TotalWorkoutTime"/>
                                            </fo:inline> Minutes
                                        </fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                            </xsl:if>
                            <xsl:if test="$TotalTime &gt; 0">
                                <fo:table-row>
                                    <fo:table-cell>
                                        <fo:block margin-bottom="5pt" text-align="left" font-family="Calibri, Verdana, Arial" font-size="9pt">
                                            <fo:inline font-family="Calibri, Verdana, Arial" font-size="10pt" font-weight="bold" color="#376092">Exercise Time:</fo:inline>&#xa0;
                                            <fo:inline font-family="Calibri, Verdana, Arial" font-size="9pt" font-weight="bold">
                                                <xsl:value-of select="$TotalTime"/>
                                            </fo:inline> Minutes
                                        </fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                            </xsl:if>
                            <xsl:if test="$TotalDistance &gt; 0">
                                <fo:table-row>
                                    <fo:table-cell>
                                        <fo:block margin-bottom="5pt" text-align="left" font-family="Calibri, Verdana, Arial" font-size="9pt">
                                            <fo:inline font-family="Calibri, Verdana, Arial" font-size="10pt" font-weight="bold" color="#376092"> Total Distance:</fo:inline>&#xa0;
                                            <fo:inline font-family="Calibri, Verdana, Arial" font-size="9pt" font-weight="bold">
                                                <xsl:value-of select="$TotalDistance"/>
                                            </fo:inline> Miles
                                        </fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                            </xsl:if>
                            <xsl:if test="$TotalCalories &gt; 0">
                                <fo:table-row>
                                    <fo:table-cell>
                                        <fo:block margin-bottom="5pt" text-align="left" font-family="Calibri, Verdana, Arial" font-size="9pt">
                                            <fo:inline font-family="Calibri, Verdana, Arial" font-size="10pt" font-weight="bold" color="#376092">Total Calories:</fo:inline>&#xa0;
                                            <fo:inline font-family="Calibri, Verdana, Arial" font-size="9pt" font-weight="bold">
                                                <xsl:value-of select="$TotalCalories"/>
                                            </fo:inline> Calories
                                        </fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                                <xsl:if test="$WorkoutNotesCount &gt; 0">
                                    <fo:table-row>

                                        <fo:table-cell background-color="#F2F2F2">
                                            <fo:block font-family="Calibri, Verdana, Arial" color="Gray" font-size="8pt" font-weight="bold">
                                                Comments :
                                            </fo:block>
                                            <xsl:for-each select="AjaxRequest/Workout/WorkoutNotes/Note">
                                                <fo:block margin-top="9pt" font-family="Calibri, Verdana, Arial" font-size="8pt" >
                                                    * <xsl:value-of select="Note"/>
                                                </fo:block>
                                            </xsl:for-each>
                                        </fo:table-cell>

                                    </fo:table-row>
                                </xsl:if>
                            </xsl:if>
                            <fo:table-row>
                                <fo:table-cell>
                                    <fo:block></fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-body>
                    </fo:table>
                    <fo:block margin-top="5pt">
                        <fo:leader  leader-length="6.3in" 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="2mm" font-family="Calibri, Verdana, Arial" font-size="10pt" font-weight="bold">
                        Exercise Summary
                    </fo:block>

                    <fo:table>
                        <fo:table-column column-width="2.5cm"/>
                        <fo:table-column column-width="7.1cm"/>
                        <fo:table-column column-width="6.4cm"/>
                        <xsl:for-each select="AjaxRequest/Workout/WorkoutDetail[generate-id() = generate-id(key('distinctSeqNum', ./SeqNum))]">
                            <xsl:variable name="ProgramExerciseID" select="ProgramExerciseID"></xsl:variable>
                            <xsl:variable name="ItemID" select="ItemID"></xsl:variable>
                            <xsl:variable name="NotesCount" select="count(../ExerciseNotes/Note[Item2ID = $ItemID])"></xsl:variable>



                            <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="#376092">
                                            <xsl:value-of select="ExerciseName"/>
                                        </fo:block>
                                        <!--<fo:block text-align="left" font-family="Calibri, Verdana, Arial" font-size="10pt">
                      <xsl:value-of select="Description"/>
                    </fo:block>-->
                                    </fo:table-cell>
                                    <fo:table-cell>
                                        <xsl:for-each select="../WorkoutDetail[ProgramExerciseID = $ProgramExerciseID]">
                                            <fo:block font-family="Calibri, Verdana, Arial" font-size="10pt" color="#376092">

                                                <fo:inline color="black" font-weight="bold" letter-spacing="5pt">
                                                    <xsl:value-of select="position()"/>
                                                </fo:inline>

                                                <xsl:if test="RepsYN = 1">
                                                    <fo:inline >
                                                        <xsl:value-of select ="RepsNum"></xsl:value-of>&#xa0;Reps&#xa0;
                                                    </fo:inline>
                                                </xsl:if>
                                                <xsl:if test="LevelYN  = 1">
                                                    <xsl:value-of select ="LevelNum"></xsl:value-of>&#xa0;Level&#xa0;
                                                </xsl:if>
                                                <xsl:if test="TimeYN = 1">
                                                    <xsl:value-of select ="TimeActValue"></xsl:value-of>&#xa0;<xsl:value-of select="TimeMeasueValue"/>&#xa0;
                                                </xsl:if>
                                                <xsl:if test="DistanceYN = 1">
                                                    <xsl:value-of select ="DistanceActValue"></xsl:value-of>&#xa0;<xsl:value-of select="DistanceMeasureValue"/>&#xa0;
                                                </xsl:if>
                                                <xsl:if test="WeightYN = 1">
                                                    <xsl:value-of select ="WeightActValue"></xsl:value-of>&#xa0;<xsl:value-of select="WeightMeasureValue"/>&#xa0;
                                                </xsl:if>
                                                <xsl:if test="CaloriesYN = 1">
                                                    <xsl:value-of select ="CaloriesActValue"></xsl:value-of>&#xa0;Cals&#xa0;
                                                </xsl:if>
                                            </fo:block>
                                        </xsl:for-each>

                                        <fo:block text-align="left" font-family="Calibri, Verdana, Arial" font-size="10pt">
                                        </fo:block>
                                    </fo:table-cell>
                                    <xsl:if test="$NotesCount &gt; 0">
                                        <fo:table-cell background-color="#F2F2F2">
                                            <fo:block font-family="Calibri, Verdana, Arial" color="Gray" font-size="8pt" font-weight="bold">
                                                Comments :
                                            </fo:block>

                                            <xsl:for-each select="../ExerciseNotes/Note[Item2ID = $ItemID]">
                                                <!--<fo:block-container background-color="#F2F2F2" margin-top="9pt" block-progression-dimension="13mm" inline-progression-dimension="60mm"
                                       font-family="Calibri, Verdana, Arial" font-size="8pt" border="solid Gray 0.2pt">-->
                                                <fo:block margin-top="9pt" font-family="Calibri, Verdana, Arial" font-size="8pt" >
                                                    * <xsl:value-of select="Note"/>
                                                </fo:block>
                                                <!--</fo:block-container>-->
                                            </xsl:for-each>
                                        </fo:table-cell>
                                    </xsl:if>
                                </fo:table-row>
                                <fo:table-row>
                                    <fo:table-cell number-columns-spanned="3">
                                        <fo:block margin-top="5pt">
                                            <fo:leader  leader-length="6.3in" leader-pattern="rule" baseline-shift="left" alignment-baseline="middle"
                                                        rule-thickness="0.5pt" color="gray"/>
                                        </fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                            </fo:table-body>
                        </xsl:for-each>
                    </fo:table>


                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
</xsl:stylesheet>