{% from 'components/timestamp.html' import timestamp %}
{% from 'components/links.html' import fancy_link %}
{% from 'components/fancy_image.html' import fancy_image, background_image %}

{% macro dm_card(
  dm,
  is_private=false,
  is_global=false,
  artist={},
  class_name=none
) %}
  {% set service = g.paysites[dm.service] %}
  {% set creator_page_url = '/' ~ dm.service ~ '/user/' ~ dm.user %}
  {% set remote_creator_page_url = service.user.profile(artist or { "id" : dm.user}) %}

  <article
    class="dm-card {{ class_name if class_name }}"
    {% if is_private %}
      data-id="{{ dm.hash }}"
    {% endif %}
  >
    {% if is_global %}
        <header class="dm-card__header">
{#          {% call fancy_link(url=creator_page_url, class_name="dm-card__icon") %}#}
{#            {{ fancy_image( g.icons_prepend ~ '/icons/' ~ artist.service ~ '/' ~ artist.id) }}#}
{#          {% endcall %}#}

          {% call fancy_link(url=creator_page_url, class_name='dms__user-link') %}
            <span class="dm-card__user">{{ artist.name or dm.user }}</span>
          {% endcall %}
          {% call fancy_link(url=remote_creator_page_url, class_name='dms__remote-user-link') %}
            <span class="dm-card__service">({{ service.title }})</span>
          {% endcall %}
        </header>
    {% endif %}

    {% if is_private %}
      <header class="dm-card__header">
          {% call fancy_link(url=creator_page_url, class_name='dms__user-link') %}
            <span class="dm-card__user">{{ artist.name or dm.user }}</span>
          {% endcall %}

          {% call fancy_link(url=remote_creator_page_url, class_name='dms__remote-user-link') %}
            <span class="dm-card__service">({{ service.title }})</span>
          {% endcall %}
      </header>
    {% endif %}

    <section class="dm-card__body" tabindex="0">
      {# writing it like this so there wouldn't be whitespaces/newlines in the output #}
      <div class="dm-card__content"><pre>{{ dm.content|sanitize_html|safe }}</pre></div>
    </section>

    <footer class="dm-card__footer">
      {% if dm.published %}
        <div class="dm-card__added">
          Published: {{ ( dm.published|simple_datetime|string)[:7] }}
        </div>
      {% elif dm.user_id %} {# this is to detect if its not DM#}
        <div class="dm-card__added">
          Added: {{ ( dm.added|simple_datetime|string)[:7] }}
        </div>
      {% else %}
        <div class="dm-card__added">
          Added: {{ dm.added|simple_datetime }}
        </div>
      {% endif %}
    </footer>
  </article>
{% endmacro %}
