{% from 'components/timestamp.html' import timestamp %}
{% from 'components/buttons.html' import button %}
{% from 'components/links.html' import fancy_link %}

{% macro post_card(post) %}
  {% set src_ns = namespace(found=false) %}
  {% set src_ns.src = post.file.path if post.file.path and post.file.path|lower|regex_match("\.(gif|jpe?g|jpe|png|webp)$") %}
  {% if post.service == "fansly" or post.service == "candfans" or post.service == "boosty" or post.service == "gumroad"%}
    {% for file in post.attachments %}
      {% if not src_ns.src and file.path and file.path|lower|regex_match("\.(gif|jpe?g|jpe|png|webp)$") %}
        {% set src_ns.src = file.path %}
      {% endif %}
    {% endfor %}
  {% endif %}
  {% set post_link = g.freesites.kemono.post.link(post.service, post.user, post.id) %}
  <article
    class="post-card {{ 'post-card--preview' if src_ns }}"
    data-id="{{ post.id }}"
    data-service="{{ post.service }}"
    data-user="{{ post.user }}"
  >
    <a href="{{ post_link }}">
      <header class="post-card__header">
       {% if post.title and post.title != "DM" %}
            {{ post.title }}
          {% elif post.content|length < 50 %}
            {{ post.content }}
          {% else %}
            {{ post.content[:50] + "..." }}
          {% endif %}
      </header>
      {% if src_ns.src %}
        <div class="post-card__image-container">
            <img
              class="post-card__image"
              src="{{ g.thumbnails_prepend }}/thumbnail/data{{ src_ns.src }}"
            >
          </div>
      {% endif %}
      <footer class="post-card__footer">
       <div>
        <div>
        {% if post.published %}
            {{ timestamp(post.published) }}
        {% endif %}
        <div>
          {% if post.attachments|length %}
            {{ post.attachments|length }} {{ 'attachment' if post.attachments|length == 1 else 'attachments' }}
          {% else %}
            No attachments
          {% endif %}
        </div>
          </div>
            <img src="/static/small_icons/{{ post.service }}.png">
          </div>
      </footer>
    </a>
  </article>
{% endmacro %}

{% macro post_fav_card(post) %}
  {% set src_ns = namespace(found=false) %}
  {% set src_ns.src = post.file.path if post.file.path and post.file.path|lower|regex_match("\.(gif|jpe?g|jpe|png|webp)$") %}
  {% if post.service == "fansly" or post.service == "candfans" or post.service == "boosty" or post.service == "gumroad"%}
    {% for file in post.attachments %}
      {% if not src_ns.src and file.path and file.path|lower|regex_match("\.(gif|jpe?g|jpe|png|webp)$") %}
        {% set src_ns.src = file.path %}
      {% endif %}
    {% endfor %}
  {% endif %}
  {% set post_link = g.freesites.kemono.post.link(post.service, post.user, post.id) %}
  <article
    class="post-card {{ 'post-card--preview' if src_ns }}"
    data-id="{{ post.id }}"
    data-service="{{ post.service }}"
    data-user="{{ post.user }}"
  >
    <a href="{{ post_link }}">
      <header class="post-card__header">
       {% if post.title and post.title != "DM" %}
            {{ post.title }}
          {% elif post.content|length < 50 %}
            {{ post.content }}
          {% else %}
            {{ post.content[:50] + "..." }}
          {% endif %}
      </header>
      {% if src_ns.src %}
        <div class="post-card__image-container">
            <img
              class="post-card__image"
              src="{{ g.thumbnails_prepend }}/thumbnail/data{{ src_ns.src }}"
            >
          </div>
      {% endif %}
      <footer class="post-card__footer">
       <div>
        <div>
        {% if post.published %}
            {{ timestamp(post.published) }}
        {% endif %}
        <div>
          {% if post.attachments|length %}
            {{ post.attachments|length }} {{ 'attachment' if post.attachments|length == 1 else 'attachments' }}
          {% else %}
            No attachments
          {% endif %}
          <br>
          {{ post.fav_count| int }} {{ "favorites" if post.fav_count > 1 else "favorite" }}
        </div>
          </div>
            <img src="/static/small_icons/{{ post.service }}.png">
          </div>
      </footer>
    </a>
  </article>
{% endmacro %}
