{% macro fatcat_bits(entity, entity_type, expand="", editgroup=None) -%}

{% if entity.state == None and editgroup %}
  <div class="ui segment pink inverted attached">
  {% if editgroup.changelog_index %}
    <b>Accepted Edit Version</b>
    <p>This is the version of the entity as of a specific merged editgroup:
  {% elif editgroup.submitted %}
    <b>Submitted Edit Version</b>
    <p>This is a version of the entity that has been submitted for approval as part of an editgroup:
  {% else %}
    <b>Edit In Progress</b>
    <p>This is a version of the entity that has not yet been submitted for approval, part of an editgroup:
  {% endif %}
  <b><a href="/editgroup/{{ editgroup.editgroup_id }}" style="color: white; font-weight: bold;">{{ editgroup.editgroup_id }}</a></b>
  </div>
{% elif entity.state == None and entity.ident == None %}
<div class="ui segment pink inverted attached">
  <b>Revision</b>
  <p>This is a specific, static metadata record, not necessarily linked to any current entity in the catalog.
</div>
{% elif entity.state == "wip" %}
<div class="ui segment pink inverted attached">
  <b>Work In Progress</b>
  <p>This entity has not been "accepted" into the official database yet.
</div>
{% endif %}

<div class="ui segment attached">
  <b>Fatcat Bits</b>
  <p>
  {% if entity.state %}
    State is "{{ entity.state }}".
  {% endif %}
  {% if entity.state != "deleted" %}
    Revision:
    <br><small><code><a href="/{{ entity_type }}/rev/{{ entity.revision }}">{{ entity.revision }}</a></code></small>
  {% endif %}
  <br><a href="{% if config.FATCAT_DOMAIN == 'dev.fatcat.wiki' %}http://localhost:9411{% else %}https://api.{{ config.FATCAT_DOMAIN }}{% endif %}/v0/{{ entity_type }}/{{ entity.ident }}{% if expand %}?expand={{ expand}}{% endif %}">As JSON object via API</a>
</div>

<div class="two ui buttons bottom attached">
  <a href="{% if editgroup %}/editgroup/{{ editgroup.editgroup_id }}{% endif %}/{{ entity_type }}/{{ entity.ident }}/edit" class="ui blue button">Edit Metadata</a>
  <a href="/{{ entity_type }}/{{ entity.ident }}/history" class="ui button">View History</a>
</div>
{%- endmacro %}


{% macro extra_metadata(extra) -%}
<table class="ui definition single line fixed compact small unstackable table">
<tbody>
{% for (key, value) in extra.items()|sort %}
  {% if key in ("ia", "crossref", "kbart", "arxiv", "jstor", "pubmed") and value is mapping and value %}
    {% for (inner_key, inner_value) in value.items()|sort %}
      <tr><td class="three wide right aligned"><code>{{ key }}.{{ inner_key }}</code></td>
          <td class="seven wide"><code>{{ inner_value }}</code>
    {% endfor %}
  {% elif key in ("urls") and value and value is iterable and value is not string and value[0] is string %}
    <tr><td class="three wide right aligned"><code>{{ key }}</code></td>
        <td class="seven wide">
          <code>
          {% for u in value %}
            <a href="{{ u }}">{{ u }}</a><br>
          {% endfor %}
          </code>
  {% else %}
    <tr><td class="three wide right aligned"><code>{{ key }}</code></td>
        <td class="seven wide"><code>{% if value != None %}{{ value }}{% endif %}</code>
  {% endif %}
{% endfor %}
</tbody>
</table>
{%- endmacro %}


{% macro release_list(releases) -%}
<table class="ui very basic celled table">
<tbody>
{% for release in releases %}
  <tr><td class="collapsing center aligned">
        {% if release.release_date %}{{ release.release_date }}{% elif release.release_year %}{{ release.release_year }}{% else %}<i>unknown</i>{% endif %}
      <td class="">
        <b><a href="/release/{{ release.ident }}">{{ release.title }}</a></b>
        <br><small>{{ release.release_stage or "unknown status" }}
            | {{ release.release_type or "unknown type" }}
            {% if release.version %} | {{ release.version }}{% endif %}
            {% if release.license_slug %} | {{ release.license_slug }}{% endif %}
        </small>
        {% if release.ext_ids.doi %}
          <br><a href="https://doi.org/{{ release.ext_ids.doi }}" style="color:green;">doi:{{ release.ext_ids.doi }}</a>
        {% endif %}
{% endfor %}
</tbody>
</table>
{%- endmacro %}


{% macro url_list(urls, wayback_suffix="") -%}
<table class="ui very basic compact single line fixed table">
<tbody>
  {% for url in urls %}
    {% if url.rel == "wayback" %}
      {% set suffix = wayback_suffix %}
    {% else %}
      {% set suffix = "" %}
    {% endif %}
    {% set entity = release %}
    <tr><td class="two wide right aligned">{{ url.rel }}
        <td class="eight wide"><small><code><a href="{{ url.url }}{{ suffix }}">
        {% if url.url.count('/') >= 3 and url.rel != "dweb" %}
          {{ '/'.join(url.url.split('/')[0:2]) }}/<b>{{ ''.join(url.url.split('/')[2]) }}</b>/{{ '/'.join(url.url.split('/')[3:]) }}{{ suffix }}
        {% else %}
          {{ url.url }}{{ suffix }}
        {% endif %}
        </a></code></small>
  {% endfor %}
</tbody>
</table>
{%- endmacro %}

{% macro lookup_form(entity_type, key, example, lookup_key, lookup_value, lookup_error) -%}
<form class="ui form" role="search" action="/{{ entity_type }}/lookup" method="get">
  <div class="ui form">
    <div class="field {% if key == lookup_key %}{% if lookup_error == 400 %}error{% elif lookup_error == 404 %}warning{% endif %}{% endif %}">
      {% if key == lookup_key and lookup_error == 400 %}
        <div class="ui pointing below red label">
          Not correct syntax. Expected, eg, {{ example }}
        </div>
      {% endif %}
      <div class="ui action input big fluid">
        <input type="text" placeholder="{% if example %}eg, {{ example }}{% endif %}" name="{{ key }}" value="{% if key == lookup_key %}{{ lookup_value }}{% endif %}" aria-label="{{ key }} lookup">
        <button class="ui button">Go!</button>
      </div>
    </div>
  </div>
</form>
{%- endmacro %}