<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:cs="http://www.xfront.com/owl/ontologies/CoordinateSystem/#"
                xmlns:len="http://www.xfront.com/owl/ontologies/Length/#"
                xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                xmlns:exslt="http://exslt.org/math"
                extension-element-prefixes="exslt"
                version="2.0">
 
    <xsl:include href="Length-Include-File.xsl"/>

    <xsl:function name="cs:CoordinateSystem" as="element()">
        <xsl:param name="coordinateSystem" as="item()"/>

        <xsl:choose>
            <xsl:when test="local-name($coordinateSystem)='Polar-Coordinate-System'">
                <Cartesian-Coordinate-System xmlns="http://www.xfront.com/owl/ontologies/CoordinateSystem/#">
                    <xsl:variable name="canonical-r-length" select="len:Length($coordinateSystem/cs:r/len:Length)"/>
                    <xsl:variable name="canonical-theta-angle" select="cs:Angle($coordinateSystem/cs:theta/cs:Angle)"/>
                    <x>
                        <Length xmlns="http://www.xfront.com/owl/ontologies/Length/#">
                            <value>
                                <!-- x = r cos theta -->
                                <xsl:value-of select="$canonical-r-length/len:value * exslt:cos($canonical-theta-angle/cs:value)"/>
                            </value>
                            <units rdf:resource="http://www.xfront.com/owl/ontologies/Length/#Metre"/>
                        </Length>
                    </x>
                    <y>
                        <Length xmlns="http://www.xfront.com/owl/ontologies/Length/#">
                            <value>
                                <!-- y = r sin theta -->
                                <xsl:value-of select="$canonical-r-length/len:value * exslt:sin($canonical-theta-angle/cs:value)"/>
                            </value>
                            <units rdf:resource="http://www.xfront.com/owl/ontologies/Length/#Metre"/>
                        </Length>
                    </y>
                </Cartesian-Coordinate-System>
            </xsl:when>
            <xsl:when test="local-name($coordinateSystem)='Cartesian-Coordinate-System'">
                <Cartesian-Coordinate-System xmlns="http://www.xfront.com/owl/ontologies/CoordinateSystem/#">
                    <x>
                        <Length xmlns="http://www.xfront.com/owl/ontologies/Length/#">
                            <value>
                                <xsl:variable name="canonical-x-length" select="len:Length($coordinateSystem/cs:x/len:Length)"/>
                                <xsl:value-of select="$canonical-x-length/len:value"/>
                            </value>
                            <units rdf:resource="http://www.xfront.com/owl/ontologies/Length/#Metre"/>
                        </Length>
                    </x>
                    <y>
                        <Length xmlns="http://www.xfront.com/owl/ontologies/Length/#">
                            <value>
                                <xsl:variable name="canonical-y-length" select="len:Length($coordinateSystem/cs:y/len:Length)"/>
                                <xsl:value-of select="$canonical-y-length/len:value"/>
                            </value>
                            <units rdf:resource="http://www.xfront.com/owl/ontologies/Length/#Metre"/>
                        </Length>
                    </y>
                </Cartesian-Coordinate-System>
            </xsl:when>
            <xsl:otherwise>
                <xsl:sequence select="$coordinateSystem"/>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:function>

    <xsl:function name="cs:Angle" as="element()">
        <xsl:param name="angle" as="item()"/>

        <xsl:choose>
            <xsl:when test="$angle/cs:units/@rdf:resource!='http://www.xfront.com/owl/ontologies/CoordinateSystem/#Radian'">
                <!-- Implement later -->
            </xsl:when>
            <xsl:otherwise>
                <xsl:sequence select="$angle"/>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:function>


</xsl:stylesheet>
