{% macro table_header() -%}
 <thead>
  <tr>
   <th>Region</th>
   <th>Type</th>
   <th>From</th>
   <th>To</th>
   <th>Similarity Confidence</th>
   <th colspan="2">Most similar known cluster</th>
  </tr>
 </thead>
{%- endmacro %}

{% macro region_button(region, element_class, css) -%}
    <{{element_class}} class="regbutton {{css}} {{region.anchor_id}}">
{%- endmacro %}

{% macro region_row(region, cycle, record_index, options, css) -%}
   <tr class="linked-row {{cycle}}" data-anchor="#{{region.anchor_id}}">
     {{region_button(region, "td", css)}}
      <a href="#{{region.anchor_id}}">Region&nbsp;{% if record_index %}{{record_index}}.{% endif %}{{region.get_region_number()}}</a>
     </td>
     <td>
       {% set join = joiner(",") %}
       {% for subtype in region.products -%}
         {{join()}}<a class="external-link" href="{{options.urls.docs_baseurl}}glossary/#{{subtype.lower()}}" target="_blank">{{subtype}}</a>
       {%- endfor %}
     </td>
     <td class="digits">{{"{:,d}".format(region.start + 1)}}</td>
     <td class="digits table-split-left">{{"{:,d}".format(region.end)}}</td>
     {% if region.most_related_area.identifier -%}
      {% set similarity = region.most_related_area.similarity_percentage %}
      {% if similarity > 15 %}
       {% set url = region.most_related_area.url %}
       {% if similarity > 75 %}
         {% set colour = "rgb(178, 208, 178)" %}
         {% set label = "High" %}
       {% elif similarity > 50 %}
         {% set colour = "rgb(251, 214, 192)" %}
         {% set label = "Medium" %}
       {% else %}
         {% set colour = "rgb(249, 178, 178)" %}
         {% set label = "Low" %}
       {% endif %}
       <td class="similarity-text" style="background: {{colour}}">{{label}}</td>
       {% if url -%}
       <td><a class="external-link" href="{{region.most_related_area.url}}" target="_blank">{{region.most_related_area.description}}</a></td>
       {%- else %}
       <td>{{region.most_related_area.description}}</td>
       {%- endif %}
       <td>{{region.most_related_area.product}}</td>
      {% else %}
       <td class="similarity-text"></td>
       <td colspan="2"></td>
      {% endif %}
     {% else %}
      <td colspan="3"></td>
     {% endif %}
   </tr>
{%- endmacro %}

{% macro single_record_table(record, multi_record, options, get_region_css) -%}
<table class="region-table">
 {{ table_header() }}
 <tbody>
 {% for region in record.regions -%}
  {{ region_row(region, loop.cycle('odd', 'even'), (multi_record and record.record_index) or 0, options, get_region_css(region)) }}
 {% endfor %}
 </tbody>
</table>
{%- endmacro %}

{% macro compact_records_table(records, options, get_region_css) -%}
<table class="region-table">
 {{ table_header() }}
 <tbody>
 {% set style = cycler('odd', 'even') %}
 {% for record in records %}
  {% for region in record.regions -%}
   {{ region_row(region, style.next(), record.record_index, options, get_region_css(region)) }}
  {% endfor %}
 {% endfor %}
 </tbody>
</table>
{%- endmacro %}
