{#
  Copyright (C) 2020 CERN.

  Invenio RDM Records is free software; you can redistribute it and/or modify
  it under the terms of the MIT License; see LICENSE file for more details.
#}

{% macro show_detail(title, value) %}
<div class="ui relaxed grid">
  <div class="two column row">
    <div class="four wide column">
      <p><b>{{ title }}</b></p>
    </div>
    <div class="twelve wide column">
      {{ value }}
    </div>
  </div>
</div>
{%- endmacro %}

{% macro show_title_detail(title, language, value) %}
<div class="ui relaxed grid">
  <div class="three column row">
    <div class="six wide column">
      <p><b>{{ title }}</b> {{ '(' + language + ')' if language}}</p>
    </div>
    <div class="ten wide column">
      {{ value }}
    </div>
  </div>
</div>
{%- endmacro %}

{% macro show_sanitized_detail(title, value) %}
<div class="ui relaxed grid">
  <div class="two column row">
    <div class="four wide column">
      <p><b>{{ title }}</b></p>
    </div>
    <div class="twelve wide column">
      {{ value | sanitize_html()| safe }}
    </div>
  </div>
</div>
{%- endmacro %}

{% macro list_sizes(sizes) %}
  {% for size in sizes %}
    {{ size }}{{ ", " if not loop.last }}
  {% endfor %}
{% endmacro %}

{% macro list_formats(formats) %}
  {% for format in formats %}
    {{ format }}{{ ", " if not loop.last }}
  {% endfor %}
{% endmacro %}

{% macro show_add_titles(add_titles) %}
  {% for add_title in add_titles %}
    {% if add_title.lang %}
    {{ show_title_detail(add_title.type.title_l10n, add_title.lang.title_l10n, add_title.title)}}
  {% else %}
    {{ show_title_detail(add_title.type.title_l10n,None, add_title.title)}}
  {% endif %}
{% endfor %}
{% endmacro %}

{% macro show_add_descriptions(add_descriptions) %}
  {% for add_description in add_descriptions %}
    {{
      show_sanitized_detail(add_description.type.title_l10n, add_description.description)
    }}
  {% endfor %}
{% endmacro %}

{% macro show_dates(dates) %}
  {% for date in dates %}
    {{ show_detail(date.type.title_l10n, date.date) }}
  {% endfor %}
{% endmacro %}

{% macro show_funding(funding) %}
  {% for fund in funding %}
    {{ show_detail('Award', fund.award.title + ' ( ' + fund.award.number + ' )' ) if fund.award }}
    {{ show_detail('Funder', fund.funder.name) if fund.funder }}
  {% endfor %}
{% endmacro %}

{% macro show_references(references) %}
  {% for reference in references %}
    <p>{{ reference.reference + ' ( ' + reference.scheme + ' - ' +  reference.identifier + ' )'}}</p>
  {% endfor %}
{% endmacro %}

{% macro _identifiers_for_group(related_identifiers) %}
  {% for identifier in related_identifiers %}
    <div class="ui relaxed grid">
      <div class="two column row">
        <div class="eight wide column">
          <p>{{ identifier.identifier + ' ( ' + identifier.scheme | get_scheme_label + ' )' }}</p>
        </div>
        <div class="eight wide column">
          {% if identifier.resource_type is defined %}
            {{ identifier.resource_type.title_l10n }}
          {% endif %}
        </div>
      </div>
    </div>
  {% endfor %}
{% endmacro %}


{% macro show_related_identifiers(related_identifiers) %}
  {%- for group in related_identifiers | groupby('relation_type.title_l10n')%}
    {{ show_detail(group.grouper, _identifiers_for_group(group.list))}}
  {%- endfor %}
{% endmacro %}


{% macro show_alternate_identifiers(identifiers) %}
  {% for alt_id in identifiers %}
    {# upper() is in keeping with the displayed schemes in RDMDEpositForm.js #}
    {{ show_detail(alt_id.scheme | get_scheme_label, alt_id.identifier) }}
  {% endfor %}
{% endmacro %}


{% macro list_languages(languages) %}
  {% for lang in languages %}
    {{ lang.title_l10n }}{{ ", " if not loop.last }}
  {% endfor %}
{% endmacro %}
