<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">
    <xsl:variable name="number-of-logos" select="count(//logos/logo)"/>
    <xsl:template match="certificate">
        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master master-name="order" page-width="210mm" page-height="297mm">
                    <fo:region-body margin="13mm"/>
                    <fo:region-before extent="2mm"/>
                    <fo:region-after extent="0mm"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="order">
                <fo:flow flow-name="xsl-region-body">
                    <fo:block font-size="9pt" font-family="Helvetica">
                        <fo:block margin="5mm" border="2mm groove green">
                            <fo:table width="100%">
                                <fo:table-column column-width="100%"/>
                                <fo:table-body>
                                    <fo:table-row>
                                        <fo:table-cell>
                                            <fo:block margin-bottom="6mm">
                                                <xsl:apply-templates select="head"/>
                                            </fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                    <fo:table-row height="40mm">
                                        <fo:table-cell text-align="center">
                                            <fo:block margin-left="40mm" margin-right="40mm"
                                                margin-bottom="15mm" text-align="center" space-after="10mm">
                                                <xsl:apply-templates select="body"/>
                                            </fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                    <fo:table-row>
                                        <fo:table-cell>
                                            <fo:block margin-left="20mm" margin-right="20mm" margin-bottom="15mm" text-align="center">
                                                <xsl:apply-templates select="signatures"/>
                                            </fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                    <fo:table-row>
                                        <fo:table-cell>
                                            <fo:block>
                                                <xsl:apply-templates select="issuer"/>
                                            </fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                </fo:table-body>
                            </fo:table>
                        </fo:block>
                    </fo:block>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
    <xsl:template match="head">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="logos">
        <fo:block space-after="25mm" padding-top="20mm" margin-left="20mm" margin-right="20mm" padding-bottom="5mm" border-bottom="0.5mm solid green">
            <fo:table>
                <xsl:for-each select="logo">
                    <fo:table-column column-width="{100 div $number-of-logos}%"/>
                </xsl:for-each>
                <fo:table-body>
                    <fo:table-row height="20mm">
                        <xsl:for-each select="logo">
                            <fo:table-cell width="{100 div $number-of-logos}%" height="20mm" text-align="center">
                                <fo:block width="100%" height="20mm">
                                    <fo:external-graphic content-height="20mm" src="url('{@src}')"/>
                                </fo:block>
                            </fo:table-cell>
                        </xsl:for-each>
                    </fo:table-row>
                </fo:table-body>
            </fo:table>
        </fo:block>
    </xsl:template>
    <xsl:template match="title">
        <fo:block font-size="36pt" font-style="italic" text-align="center" space-before="10mm" space-after="5mm" font-family="Times">
            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>
    <xsl:template match="body">
        <fo:table>
            <fo:table-column column-width="60mm"/>
            <fo:table-body>
                <fo:table-row height="40mm">
                    <fo:table-cell>
                        <fo:block padding="3mm" font-size="12pt">
                            <xsl:apply-templates select="prolog"/>
                        </fo:block>
                        <fo:block padding="3mm" font-size="24pt" border="1mm groove green" font-style="italic" font-family="Times">
                            <xsl:apply-templates select="//receiver/name"/>
                        </fo:block>
                        <fo:block padding="3mm" font-size="12pt">
                            <xsl:apply-templates select="epilog"/>
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
            </fo:table-body>
        </fo:table>
    </xsl:template>
    <xsl:template match="receiver/name">
        <fo:block text-align="center" font-weight="bold" space-before="2mm" space-after="2mm">
            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>
    <xsl:template match="signatures">
        <fo:table width="100%" space-after="32mm">
            <fo:table-column column-width="30%"/>
            <fo:table-column column-width="60%"/>
            <fo:table-column column-width="30%"/>
            <fo:table-body>
                <fo:table-row>
                    <fo:table-cell text-align="left">
                        <fo:block font-size="12pt">
                            <xsl:value-of select="signer-title"/>
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell/>
                    <fo:table-cell text-align="right" font-size="12pt">
                        <fo:block>
                            <xsl:value-of select="signer-name"/>
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
                <fo:table-row>
                    <fo:table-cell/>
                    <fo:table-cell padding-top="8mm">
                        <fo:block font-size="14pt" text-align="center"> Date: <xsl:apply-templates select="date"/>
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell/>
                </fo:table-row>
            </fo:table-body>
        </fo:table>
    </xsl:template>
    <xsl:template match="date">
        <xsl:param name="separator" select="'/'"/>
        <xsl:value-of select="concat(day, $separator, month, $separator, year)"/>
    </xsl:template>
    <xsl:template match="issuer">
        <fo:block font-size="17pt" font-weight="bold" text-align="center" margin-left="15mm"
            margin-right="15mm" border-top="0.5mm solid green">
            <xsl:apply-templates select="organization-name"/>
        </fo:block>
        <fo:block font-size="12pt" font-weight="bold" text-align="center">
            <xsl:apply-templates select="organization-details"/>
        </fo:block>
    </xsl:template>
</xsl:stylesheet>
