{% macro render_field(
field,
label_visible=true,
icon=none,
container=true,
noindicator=false,
indicator_icon=none,
indicator_text=none,
suffix_text=none,
display=true
) -%}
{% set field_visible = field.type != 'HiddenField' and field.type !='CSRFTokenField' %}
{% if container and field_visible %}
    <div class="field {{ kwargs.pop('class_', '') }}
        {%- if field.errors %} error{% endif -%}
        {%- if field.render_kw and "disabled" in field.render_kw %} disabled{% endif -%}"
        {% if not display %}style="display: none"{% endif %}
    >
{% endif %}

{% if field_visible and label_visible %}
    {{ field.label() }}
    {% if field.description %}
        <div>{{ field.description }}</div>
    {% endif %}
{% endif %}


{% set lock_indicator = field.render_kw and ("readonly" in field.render_kw or "disabled" in field.render_kw) %}
{% set required_indicator = "required" in field.flags %}
{% set corner_indicator = not noindicator and (indicator_icon or lock_indicator or required_indicator) %}
{% set labeled = suffix_text or corner_indicator %}
{% if field_visible %}
    <div class="ui
        {% if suffix_text %}right{% endif %}
        {% if corner_indicator %}corner{% endif %}
        {% if labeled %}labeled{% endif %}
        {% if icon %}left icon {% endif %}
        {% if field.type not in ("BooleanField", "RadioField") %}input{% endif %}
    ">
{% endif %}
{% if icon %}<i class="{{ icon }} icon"></i>{% endif %}

{% if field.type not in ("SelectField", "SelectMultipleField") %}
    {{ field(**kwargs) }}
{% elif field.type == "SelectMultipleField" and field.render_kw and "readonly" in field.render_kw %}
    {{ field(class_="ui fluid dropdown multiple read-only", **kwargs) }}
{% elif field.type == "SelectMultipleField" %}
    {{ field(class_="ui fluid dropdown multiple", **kwargs) }}
{% elif field.type == "SelectField" and field.render_kw and "readonly" in field.render_kw %}
    {{ field(class_="ui fluid dropdown read-only", **kwargs) }}
{% elif field.type == "SelectField" %}
    {{ field(class_="ui fluid dropdown", **kwargs) }}
{% endif %}

{% if suffix_text %}
    <div class="ui basic label">
        {{ suffix_text }}{% if corner_indicator %}&emsp;{% endif %}
    </div>
{% endif %}

{% if not noindicator %}
    {% if indicator_icon %}
        <div class="ui corner label"{% if indicator_text %} title="{{ indicator_text }}"{% endif %}>
            <i class="{{ indicator_icon }} icon"></i>
        </div>
    {% elif lock_indicator %}
        <div class="ui corner label" title="{{ _("This field is not editable") }}">
            <i class="lock icon"></i>
        </div>
    {% elif required_indicator %}
        <div class="ui corner label" title="{{ _("This field is required") }}">
            <i class="asterisk icon"></i>
        </div>
    {% endif %}
{% endif %}
{% if field_visible %}
    </div>
{% endif %}

{% if field.errors %}
    {% for error in field.errors %}
        <div class="ui pointing red basic label">
            <p>{{ error }}</p>
        </div>
    {% endfor %}
{% endif %}

{% if container and field_visible %}
    </div>
{% endif %}

{%- endmacro %}

{% macro render_fields(form) %}
    {{ form.hidden_tag() if form.hidden_tag }}
    {% if caller %}
        {{ caller() }}
    {% else %}
        {% for field in form %}
            {% if field.type == "BooleanField" %}
                {{ render_checkbox(field) }}
            {% else %}
                {{ render_field(field) }}
            {% endif %}
        {% endfor %}
    {% endif %}
{% endmacro %}

{% macro render_checkbox(field) -%}
    <div class="field">
        <div class="ui toggle checkbox">
            {{ field(**kwargs) }}
            {{ field.label() }}
        </div>
    </div>
{%- endmacro %}

{% macro render_form(
form,
action_text=none,
class_='',
btn_class='ui right floated primary button',
action=none,
id=none) -%}
<form method="POST"
    id="{{ id or form.__class__.__name__|lower }}"
    action="{{ action or form.action }}"
    role="form"
    enctype="multipart/form-data"
    class="ui form {{ class_ }}"
>
    {% if caller %}
        {{ caller() }}
    {% else %}
        {{ render_fields(form) }}
    {% endif %}
    {% if action_text %}
        <button type="submit" class="{{ btn_class }}">{{ action_text }}</button>
    {% endif %}
</form>
{%- endmacro %}
