{% import 'macros/form.html' as form %}

{% macro multiselect(name, id='', label='', options='', selected='', error='', classes=[], is_required=false) %}
  {% set classes = (classes|list) %}
  {% do classes.append('control-select') %}

  {%- set extra_html = caller() if caller -%}
  {% call form.input_block(id or name, label or name, error, classes, extra_html=extra_html, is_required=is_required) %}
    <select id="{{ id or name }}" name="{{ name }}" multiple>
      {% for option in options %}
        <option value="{{ option.value }}"{% if option.value in selected %} selected{% endif %}>{{ option.text or option.value }}</option>
      {% endfor %}
    </select>
  {% endcall %}
{% endmacro %}

{% macro checkbox_group(name, label='', group='', info='', checked=false, error="", classes=[], attrs={}, is_required=false) %}
  {%- set extra_html = caller() if caller -%}
  <div class="control-group checkbox-group{{ " " ~ classes | join(" ") }}{% if error %} error{% endif %}">
    <label class="control-label">{{group}}</label>
    <div class="controls">
      <label class="checkbox" for="{{ name }}">
        <input id="{{ id or name }}" type="checkbox" name="{{ name }}" value="True" {{ "checked " if checked }} data-toggle="collapse" data-target="#chbxgrp-{{name}}" />
        {{ _("Enable") }}
        {% if error and error is iterable %}<strong class="error-inline">{{ error|join(', ') }}</strong>{% endif %}
      </label>
      {{ form.info(info) }}
      <br/>
      <div id="chbxgrp-{{name}}" class="collapse {% if checked %} in {% endif %}">
        {{ extra_html }}
      </div>
    </div>
  </div>
{% endmacro %}


{{ form.checkbox('repeat_map', label=_('Allow map to repeat'), value=True, checked=data.repeat_map, error=errors.repeat_map) }}

{% call checkbox_group('enable_plot_map',
          group=_('Plot map'),
          info=_('Plot maps provide a single marker for each entry in the dataset.'),
          checked=True if is_new else data.enable_plot_map,
          error=errors.enable_plot_map) %}
    {{ form.input('plot_point_radius', label=_('Point radius'), value=(data.plot_point_radius or defaults['versioned_tilemap.style.plot.point_radius']), error=errors.plot_point_radius) }}
    {{ form.input('plot_point_colour', label=_('Point colour'), value=(data.plot_point_colour or defaults['versioned_tilemap.style.plot.point_colour']), error=errors.plot_point_colour) }}
    {{ form.input('plot_border_width', label=_('Point border width'), value=(data.plot_border_width or defaults['versioned_tilemap.style.plot.border_width']), error=errors.plot_border_width) }}
    {{ form.input('plot_border_colour', label=_('Point border colour'), value=(data.plot_border_colour or defaults['versioned_tilemap.style.plot.border_colour']), error=errors.plot_border_colour) }}
{% endcall %}

{% call checkbox_group('enable_grid_map',
          group=_('Grid map'),
          info=_('At any zoom level a grid maps plot a grid, providing hover and colour coded information about the number of records in that grid tile.'),
          checked=False if is_new else data.enable_grid_map,
          error=errors.enable_grid_map) %}
    {{ form.input('gridded_grid_resolution', label=_('Grid resolution'), value=(data.gridded_grid_resolution or defaults['versioned_tilemap.style.gridded.grid_resolution']), error=errors.gridded_grid_resolution) }}
    {{ form.input('gridded_cold_colour', label=_('Low record count colour'), value=(data.gridded_cold_colour or defaults['versioned_tilemap.style.gridded.cold_colour']), error=errors.gridded_cold_colour) }}
    {{ form.input('gridded_hot_colour', label=_('High record count colour'), value=(data.gridded_hot_colour or defaults['versioned_tilemap.style.gridded.hot_colour']), error=errors.gridded_hot_colour) }}
    {{ form.input('gridded_range_size', label=_('High record count colour'), value=(data.gridded_range_size or defaults['versioned_tilemap.style.gridded.range_size']), error=errors.gridded_range_size) }}
{% endcall %}

{% call checkbox_group('enable_heat_map',
          group=_('Heat map'),
          info=_('Heat maps use colour gradients to show the distribution of the records.'),
          checked=False if is_new else data.enable_heat_map,
          error=errors.enable_heat_map) %}
    {{ form.input('heatmap_point_radius', label=_('Point radius'), value=(data.heatmap_point_radius or defaults['versioned_tilemap.style.heatmap.point_radius']), error=errors.heatmap_point_radius) }}
    {{ form.input('heatmap_cold_colour', label=_('Low record count colour'), value=(data.heatmap_cold_colour or defaults['versioned_tilemap.style.heatmap.cold_colour']), error=errors.heatmap_cold_colour) }}
    {{ form.input('heatmap_hot_colour', label=_('High record count colour'), value=(data.heatmap_hot_colour or defaults['versioned_tilemap.style.heatmap.hot_colour']), error=errors.heatmap_hot_colour) }}
    {{ form.input('heatmap_heat_intensity', label=_('Intensity'), value=(data.heatmap_intensity or defaults['versioned_tilemap.style.heatmap.intensity']), error=errors.heatmap_intensity) }}
{% endcall %}

{% call checkbox_group('enable_utf_grid',
          info=_('If enabled, information will be shown when you hover (plot or grid maps) or click (plot map only) on markers on the map'),
          group=_('Marker information'),
          checked=True if is_new else data.enable_utf_grid,
          error=errors.enable_utf_grid) %}
    {{ form.select('utf_grid_title', label=_('Title'), options=map_fields, selected=data.utf_grid_title, error=errors.utf_grid_title) }}
    {{ multiselect('utf_grid_fields', label=_('Fields'), options=map_fields, selected=data.utf_grid_fields, error=errors.utf_grid_fields) }}
{% endcall %}

{% call form.select('overlapping_records_view', label=_('Overlapping records'), options=available_views, selected=data.overlapping_records_view, error=errors.overlapping_records_view) %}
  {{ form.info(_('Select the view that is used to display overlapping records at one location'), inline=True) }}
{% endcall %}
