<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="documentTemplate">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="documentType" minOccurs="0" />
                <xs:element ref="form"/>
                <xs:element ref="template"/>
            </xs:sequence>
            <xs:attribute name="headerId" type="xs:string" />
            <xs:attribute name="subsequentHeaderId" type="xs:string" />
            <xs:attribute name="footerId" type="xs:string" />
        </xs:complexType>
    </xs:element>

    <xs:element name="documentType">
        <xs:complexType>
            <xs:attribute name="conceptId" type="xs:integer" use="required" />
            <xs:attribute name="description" type="xs:string" use="required" />
            <xs:attribute name="descriptionId" type="xs:integer" />
        </xs:complexType>
    </xs:element>

    <!--   FORM INPUTS -->
    <xs:attributeGroup name="conditionAttributes">
        <xs:attribute name="else" type="xs:string"/>
        <xs:attribute name="if" type="xs:string"/>
    </xs:attributeGroup>

    <xs:attributeGroup name="topLevelFormElementAttributes">
        <xs:attributeGroup ref="conditionAttributes" />
        <xs:attribute name="required" type="xs:boolean"/>
        <xs:attribute name="name" use="required">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="[A-Za-z0-9_-]+" />
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="label"/>
    </xs:attributeGroup>

    <xs:complexType name="topLevelFormElementType">
        <xs:attributeGroup ref="topLevelFormElementAttributes" />
    </xs:complexType>


    <xs:element name="form">
        <xs:complexType>
            <xs:all>
                <xs:element ref="title" />
                <xs:element ref="banner" minOccurs="0" />
                <xs:element ref="heading" minOccurs="0" />
                <xs:element ref="section" minOccurs="0"/>
            </xs:all>
        </xs:complexType>
    </xs:element>

    <xs:element name="title" type="xs:string"/>

    <xs:complexType name="layoutElementContainerType">
        <xs:sequence>
            <!--  Permit any of these tags in any order in any number     -->
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element ref="heading" />
                <xs:element ref="section"/>
                <xs:element ref="checkbox"/>
                <xs:element ref="checkbox-group"/>
                <xs:element ref="radio-buttons"/>
                <xs:element ref="select"/>
                <xs:element ref="text-input"/>
                <xs:element ref="textarea"/>
                <xs:element ref="quantity"/>
                <xs:element ref="date"/>
                <xs:element ref="time"/>
                <xs:element ref="drawing"/>
                <xs:element ref="banner"/>
            </xs:choice>
        </xs:sequence>
        <xs:attributeGroup ref="conditionAttributes" />
    </xs:complexType>

    <xs:complexType name="headingElementType">
        <xs:complexContent>
            <xs:extension base="layoutElementContainerType">
                <xs:attribute name="title" use="required"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:element name="heading" type="headingElementType"/>

    <xs:element name="section" type="layoutElementContainerType" />

    <xs:element name="checkbox">
        <xs:complexType>
            <xs:attributeGroup ref="topLevelFormElementAttributes" />
            <xs:attribute name="text" use="required"/>
            <xs:attribute name="value" use="required" type="xs:string"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="checkbox-group">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" ref="checkbox-option"/>
            </xs:sequence>
            <xs:attributeGroup ref="topLevelFormElementAttributes" />
            <xs:attribute name="twoColumns" type="xs:boolean"/>
            <!-- Options ticked before anyone touches the form, comma separated. -->
            <xs:attribute name="value" type="xs:string"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="checkbox-option">
        <xs:complexType>
            <xs:attribute name="label" use="required" type="xs:string"/>
            <xs:attribute name="value" use="required" type="xs:string"/>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="radioButtonsType">
        <xs:sequence>
            <xs:element maxOccurs="unbounded" ref="radio-option"/>
        </xs:sequence>
        <xs:attributeGroup ref="topLevelFormElementAttributes" />
        <!-- Lay the options out in a row rather than stacked, for a short choice like Yes/No. -->
        <xs:attribute name="horizontal" type="xs:boolean"/>
        <!-- The option selected before anyone touches the form. -->
        <xs:attribute name="value" type="xs:string"/>
    </xs:complexType>
    <xs:element name="radio-buttons" type="radioButtonsType" />

    <xs:element name="radio-option">
        <xs:complexType >
            <xs:attribute name="label" use="required" type="xs:string"/>
            <xs:attribute name="value" use="required" type="xs:string"/>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="selectType">
        <xs:sequence>
            <xs:element maxOccurs="unbounded" ref="select-option"/>
        </xs:sequence>
        <xs:attributeGroup ref="topLevelFormElementAttributes" />
    </xs:complexType>
    <xs:element name="select" type="selectType" />


    <xs:element name="select-option">
        <xs:complexType>
            <xs:attribute name="label" use="required"/>
            <xs:attribute name="value" use="required" type="xs:string"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="text-input">
        <xs:complexType>
            <xs:attributeGroup ref="topLevelFormElementAttributes" />
            <xs:attribute name="value" type="xs:string"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="textarea">
        <xs:complexType>
            <xs:attributeGroup ref="topLevelFormElementAttributes" />
            <xs:attribute name="value" type="xs:string"/>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="quantityType">
        <xs:complexContent>
            <xs:extension base="topLevelFormElementType">
                <xs:attribute name="unit" use="required" type="xs:string"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:element name="quantity" type="quantityType" />

    <xs:complexType name="dateType">
        <xs:complexContent>
            <xs:extension base="topLevelFormElementType">
                <xs:attribute name="default" type="xs:string"/>
                <xs:attribute name="max-date" type="xs:string"/>
                <xs:attribute name="min-date" type="xs:string"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:element name="date" type="dateType" />

    <xs:element name="time" type="topLevelFormElementType" />

    <xs:complexType name="drawingType">
        <xs:complexContent>
            <xs:extension base="topLevelFormElementType">
                <xs:attribute name="backgroundImage" use="required" type="xs:string"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:element name="drawing" type="drawingType" />

    <xs:element name="banner">
        <xs:complexType>
            <xs:attribute name="type" type="xs:string" />
            <xs:attribute name="text" type="xs:string" />
        </xs:complexType>
    </xs:element>

    <!-- TEMPLATE  -->
    <xs:element name="template">
        <xs:complexType>
            <xs:all>
                <xs:element name="content" type="xs:string" />
            </xs:all>
            <xs:attribute name="mimetype" type="xs:string" use="required" />
        </xs:complexType>
    </xs:element>
</xs:schema>