{# Renders field for bootstrap 3 standards.

    Params:
        field - WTForm field
        kwargs - pass any arguments you want in order to put them into the html attributes.
        There are few exceptions: for - for_, class - class_, class__ - class_

    Example usage:
        {{ horz_form.field(form.email, placeholder='Input email', type='email') }}
#}

{% macro div_form_group(field) -%}
    <div class="form-group {{ class_ or '' }}">
        {{ caller(**kwargs) }}
    </div>
{%- endmacro %}


{% macro identity(value) %}{{ value }}{% endmacro %}

{% macro field(field, value_macro=identity, label_visible=true) -%}
    {% if field.type != 'HiddenField' and field.type !='CSRFTokenField' %}
        {% call div_form_group(field, **kwargs) %}
            {% if label_visible %}
                {{ field.label(class_='control-label') }}
            {% endif %}
            <div class="labeled-group">
                {{ field_widget(field, value_macro=value_macro, **kwargs) }}
            </div>
            {% if field.description %}
              <div class="description">
                <button type="button" class="btn" data-toggle="popover"
                        data-content="{{ field.description }}" data-placement="top">
                  ?
                </button>
              </div>
            {% endif %}
        {% endcall %}
    {% endif %}
{%- endmacro %}

{% macro field_widget(field, value_macro=identity) %}
    {{ '' if kwargs }} {# use kwargs so it is accepted #}
    <p id="{{ field.id }}" class="form-control-static">
        {%- if field.type == 'SelectField' -%}
            {{ value_macro(field.selected_choice_label) }}
        {%- else -%}
            {{ value_macro(field.data) }}
        {%- endif -%}
    </p>
{% endmacro %}

{# Renders radio field
    Params:
        field - WTForm field (there are no check, but you should put here only BooleanField.
        kwargs - pass any arguments you want in order to put them into the html attributes.
        There are few exceptions: for - for_, class - class_, class__ - class_

    Example usage:
        {{ horiz_form.radio_field(form.answers) }}
 #}
{% macro radio_field(field, label_visible=true) -%}
    {% call div_form_group(field, **kwargs) %}
        {% if label_visible %}
            {{ field.label(class_='control-label') }}
        {% endif %}
        <div class="labeled-group">
        {% for value, label, checked in field.iter_choices() %}
            {% if checked %}
                <p id="{{ field.id }}" class="form-control-static">{{ label }}</p>
            {% endif %}
        {% endfor %}
        </div>
        {% if field.description %}
          <div class="description">
            <button type="button" class="btn" data-toggle="popover"
                    data-content="{{ field.description }}" data-placement="top">
              ?
            </button>
          </div>
        {% endif %}
    {% endcall %}
{%- endmacro %}

{% macro submit_group(action_text='Done', btn_class='btn btn-primary', cancel_url='') -%}
    <div class="form-group">
        <div class="unlabeled-group">
          <a href="{{cancel_url}}" class="{{ btn_class }}">{{ action_text }}</a>
        </div>
    </div>
{%- endmacro %}

{% macro render_field(f) -%}
    {% if f.type == 'RadioField' %}
        {{ radio_field(f) }}
    {% else %}
        {{ field(f) }}
    {% endif %}
{%- endmacro %}



{# Renders WTForm in bootstrap way. There are two ways to call function:
     - as macros: it will render all field forms using cycle to iterate over them
     - as call: it will insert form fields as you specify:
     e.g. {% call macros.render_form(form, action_url=url_for('login_view'), action_text='Login',
                                        class_='login-form') %}
                {{ macros.render_field(form.email, placeholder='Input email', type='email') }}
                {{ macros.render_field(form.password, placeholder='Input password', type='password') }}
                {{ macros.render_checkbox_field(form.remember_me, type='checkbox') }}
            {% endcall %}

     Params:
        form - WTForm class
        action_url - url where to submit this form
        action_text - text of submit button
        class_ - sets a class for form
    #}
{% macro form(
    form,
    field_names=None,
    action_url='',
    action_text='Done',
    class_='form-horizontal',
    btn_class='btn btn-primary',
    cancel_url='',
    dirty_check=false
) -%}
    <div class="{{ class_ }}">
        {% if caller %}
            {{ caller() }}
        {% elif field_names %}
            {{ fields(form, field_names) }}
        {% else %}
            {% for f in form %}
                {{ render_field(f) }}
            {% endfor %}
        {% endif %}
        {{ submit_group(action_text=action_text, btn_class=btn_class, cancel_url=cancel_url) }}
    </div>
{%- endmacro %}

{% macro fields(form, field_names) -%}
    {% for field_name in field_names %}
        {{ render_field(form[field_name]) }}
    {% endfor %}
{%- endmacro %}

{% macro section(heading, form, field_names) -%}
    <h2>{{heading}}</h2>
    {% if caller %}
        {{ caller() }}
    {% else %}
        {{ fields(form, field_names) }}
    {% endif %}
{%- endmacro %}
