{% macro dispatch(var) %}
{% if var.__class__.__name__ == "GridType" %}
<label for="{{ var.id }}">
    <input name="{{ var.id }}" id="{{ var.id }}" type="checkbox" value="on"/>
    Download this variable
</label>

{{ attributes(var.attributes) }}

<fieldset>
    {% for name in var.maps %}
    <div>
        <label for="{{ var.id }}[{{ loop.index0 }}]">{{ name }}</label>
        (start:step:stop):
        <input
            id="{{ var.id }}[{{ loop.index0 }}]"
            type="text"
            placeholder="0:1:{{ var[name].shape[0]-1 }}"/>
    </div>
    {% endfor %}
</fieldset>

{% elif var.__class__.__name__ == "BaseType" %}
<label for="{{ var.id }}">
    <input name="{{ var.id }}" id="{{ var.id }}" type="checkbox" value="on"/>
    Download this variable
</label>

{{ attributes(var.attributes) }}

{% if var.shape %}
<fieldset>
    <div>
        {% for shp in var.shape %}
        <label for="{{ var.id }}[{{ loop.index0 }}]">{{ var.name }}[{{ loop.index0 }}]</label>
        {% if shp > 0 %}
        (start:step:stop):
        <input
            id="{{ var.id }}[{{ loop.index0 }}]"
            type="text"
            placeholder="0:1:{{ shp - 1 }}">
        {% else %}
        <input id="{{ var.id }}[{{ loop.index0 }}]" type="text" value="0" readonly/>
        {% endif %}
        {% endfor %}
    </div>
</fieldset>
{% endif %}

{% elif var.__class__.__name__ == "SequenceType" %}
{{ attributes(var.attributes) }}

<fieldset>
    Filter where:
    <select name="var1_{{ var.id }}" id="var1_{{ var.id }}">
        <option value="--" selected>--</option>
        {% for child in var.children() %}
        <option value="{{ child.id }}">{{ child.name }}</option>
        {% endfor %}
    </select>
    <select name="op_{{ var.id }}" id="op_{{ var.id }}">
        <option value="%3D" selected>=</option>
        <option value="%21%3D">≠</option>
        <option value="%3C">&lt;</option>
        <option value="%3C%3D">≤</option>
        <option value="%3E">&gt;</option>
        <option value="%3E%3D">≥</option>
        <option value="%3D%7E">≈</option>
    </select>
    <input type="text" name="var2_{{ var.id }}" id="var2_{{ var.id }}" value=""/>
</fieldset>

{% for child in var.children() %}
{{ dispatch(child) }}
{% endfor %}

{% else %}
{{ attributes(var.attributes) }}

<fieldset>
{% for child in var.children() %}
{{ dispatch(child) }}
{% endfor %}
</fieldset>
{% endif %}

{% endmacro %}

{% macro attributes(d) %}
<dl>
    {% for k, v in d.items() %}
    <dt>{{ k }}</dt>
    {% if v is mapping %}
    <dd>
    <div>
    {{ attributes(v) }}
    </div>
    </dd>
    {% elif v is sequence and not v is string %}
    <dd>{{ v|join(", ") }}</dd>
    {% else %}
    <dd>{{ v }}</dd>
    {% endif %}
    {% endfor %}
</dl>
{% endmacro %}
