<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tei="http://www.tei-c.org/ns/1.0"
    xmlns:functx="http://www.functx.com" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:uuid="java:java.util.UUID" exclude-result-prefixes="xs" version="2.0"
    xpath-default-namespace="http://sub.uni-goettingen.de/edxml#">

    <xsl:function name="functx:substring-before-if-contains" as="xs:string?">
        <xsl:param name="arg" as="xs:string?"/>
        <xsl:param name="delim" as="xs:string"/>

        <xsl:sequence select="
                if (contains($arg, $delim))
                then
                    substring-before($arg, $delim)
                else
                    $arg
                "/>

    </xsl:function>

    <xsl:template match="facsimile">
        <div class="facsimile section"
            id="{if(@xml:id) then (@xml:id) else ('facs_'||generate-id(.))}">
            <xsl:apply-templates/>
        </div>
    </xsl:template>

    <xsl:template match="surface">
        <div class="surface" id="{if(@xml:id) then (@xml:id) else ('surf_'||generate-id(.))}">
            <xsl:apply-templates/>
        </div>
    </xsl:template>

    <xsl:template match="column">
        <xsl:variable name="nr" select="
                if (@n) then
                    (@n)
                else
                    (count(preceding::column) + 1)"/>
        <xsl:variable name="id" select="
                if (@xml:id) then
                    (@xml:id)
                else
                    ('colu_' || generate-id(.))"/>
        <div class="column" id="{$id}" data-n="{$nr}">
            <xsl:apply-templates/>
        </div>
    </xsl:template>

    <!--<xsl:template match="line[not(@n)] | surface//note">
        <div class="note" id="{if(@xml:id) then (@xml:id) else ('line_'||generate-id(.))}">
            <xsl:apply-templates/>
        </div>
    </xsl:template>-->

    <xsl:template match="line[@n]">
        <xsl:variable name="nr" select="@n"/>
        <xsl:variable name="auto-nr" select="
                if ($nr) then
                    ($nr)
                else
                    (count(preceding::line) + 1)"/>
        <xsl:variable name="id" select="
                if (@xml:id) then
                    (@xml:id)
                else
                    ('line_' || generate-id(.))"/>
        <xsl:variable name="targetIds" select=".//@xml:id"/>
        <xsl:variable name="allFollowingNotes"
            select="./ancestor::surface/following-sibling::notes/note"/>

        <xsl:variable name="correspondingNotes">
            <xsl:for-each select="$allFollowingNotes">
                <xsl:if
                    test="substring-after(functx:substring-before-if-contains(@target, ' '), '#') = $targetIds">
                    <xsl:copy-of select="."/>
                </xsl:if>
            </xsl:for-each>
        </xsl:variable>
        <div class="line-container">
            <div class="line" id="{$id}">
                <span>
                    <xsl:choose>
                        <xsl:when test="@n">
                            <xsl:attribute name="class">line-nr explicit</xsl:attribute>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:attribute name="class">line-nr calculated</xsl:attribute>
                        </xsl:otherwise>
                    </xsl:choose>
                    <xsl:value-of select="$auto-nr"/>
                </span>
                <span class="line-body">
                    <xsl:apply-templates/>
                </span>
                <xsl:if test="$correspondingNotes != ''">
                    <span>
                        <xsl:variable name="noteIconId" select="uuid:randomUUID()"/>
                        <label for="{$noteIconId}" class="notes-icon"/>
                        <input type="checkbox" id="{$noteIconId}"/>
                    </span>
                </xsl:if>
            </div>
            <xsl:if test="$correspondingNotes != ''">
                <div class="notes-container">
                    <ul>
                        <xsl:for-each select="$correspondingNotes">
                            <xsl:apply-templates/>
                        </xsl:for-each>
                    </ul>

                </div>
            </xsl:if>
        </div>
    </xsl:template>

    <!-- LINE-PARTS -->
    <xsl:template match="g[not(@cert)]">
        <span class="g" id="{if(@xml:id) then (@xml:id) else ('part_'||generate-id(.))}">
            <xsl:apply-templates/>
        </span>
    </xsl:template>

    <xsl:template match="g[@cert]">
        <span class="g" data-cert="{@cert}"
            id="{if(@xml:id) then (@xml:id) else ('part_'||generate-id(.))}">
            <xsl:apply-templates/>
        </span>
    </xsl:template>

    <xsl:template match="metamark[contains(@rend, 'Line') or contains(@rend, 'line')]">
        <xsl:variable name="rend" select="@rend"/>
        <hr class="metamark-line {$rend}" data-rend="{$rend}"/>
    </xsl:template>

    <xsl:template match="line//part">
        <span class="part" id="{if(@xml:id) then (@xml:id) else ('part_'||generate-id(.))}">
            <xsl:apply-templates/>
        </span>
    </xsl:template>

    <xsl:template match="line//seg">
        <span class="seg" id="{if(@xml:id) then (@xml:id) else ('part_'||generate-id(.))}">
            <xsl:apply-templates/>
        </span>
    </xsl:template>

    <!-- notes -->

    <xsl:template match="facsimile/notes"/>

    <xsl:template match="note">
        <li class="note-item">
            <xsl:variable name="noteItemId" select="uuid:randomUUID()"/>
            <label for="{$noteItemId}">
                <xsl:value-of select="@type"/>
            </label>
            <input id="{$noteItemId}" type="checkbox"/>
            <span>
                <xsl:apply-templates/>
            </span>

        </li>
    </xsl:template>

</xsl:stylesheet>