{% from "schema_templates/schema_macros.jinja2" import badge %}

{% macro body(element, trail) %}
    <div class="breadcrumbs">
        {% for node in trail|split("-") %}
            {% if loop.first %}
                Root
            {% else %}
                <a href="#{{ node }}"
                   onclick="anchorLink('{{ node }}')">{{ node }}</a>
            {% endif %}
            {% if not loop.last %}
                <i class="bi-arrow-right-short"></i>
            {% endif %}
        {% endfor %}
    </div>
    {% if element.occurs[0] != 1 or element.occurs[1] != 1 %}
        <div class="row">
            <div class="col">
                {{ badge("secondary", element.occurs|occurs_text) }}
            </div>
        </div>
    {% endif %}
    <div class="row">
        <div class="col">
            {{ badge("secondary", "Type: " + element.type|get_name) }}
        </div>
    </div>
    <br/>
    <div class="row">
        <div class="col">
            <span>{{ element|get_desc|simple_md }}</span>
        </div>
    </div>
    {% if element.type.has_complex_content() %}
        {% for child in element.type.content|children %}
            {{ render(child, trail) }}
        {% endfor %}
    {% endif %}
{% endmacro %}

{% macro render(element, trail) %}
    <div class="row my-2">
        <div class="col">
            {% set trail=trail+"-"+element.name %}
            {% set html_id=trail %}
            <div class="accordion" id="accordion_{{ html_id }}">
                <div class="accordion-item">
                    <h2 class="accordion-header" id="heading_{{ html_id }}">
                        <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
                                data-bs-target="#{{ html_id }}" aria-expanded="false"
                                aria-controls="{{ html_id }}" onclick="setAnchor('#{{ html_id }}')">
                            <span class="property-name align-middle pb-1">{{ element.name|escape }}</span>
                            {% if element.occurs[0] == 1 and element.occurs[1] == 1 %}
                                {{ " " }}{{ badge("warning", "Required", "ms-2") }}
                            {% endif %}
                        </button>
                    </h2>
                    <div id="{{ trail }}" class="accordion-collapse collapse"
                         aria-labelledby="heading_{{ html_id }}"
                         data-parent="#accordion_{{ html_id }}">
                        <div class="accordion-body">
                            {% if element.parent != None and element.type.name == element.parent.parent.name %}
                                {{ badge("info", "<em>Recursive Reference</em>") }}
                            {% else %}
                                {{ body(element, trail) }}
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endmacro %}

<div class="row">
    <div class="col text-center">
        <h1>{{ page.meta['title'] }}</h1>
    </div>
</div>
<div class="row">
    <div class="col">
        {% for element in schema.elements.values() %}
            {% for child in element.type.content %}
                {{ render(child, element.name) }}
            {% endfor %}
        {% endfor %}
    </div>
</div>
