{% from 'core/macros.jinja' import user_profile_link_short_name %}

{% macro display_forum(forum, user, is_root=False) %}
  <div class="forum {% if is_root %}category{% endif %}">
    <div class="ib w_big">
      {% if not is_root %}
        <a class="ib w_big" href="{{ url('forum:view_forum', forum_id=forum.id) }}">
      {% else %}
        <div class="ib w_big">
      {% endif %}
      <div class="title">{{ forum.name }}</div>
      <p>{{ forum.description }}</p>
      {% if not is_root %}
        </a>
      {% else %}
        </div>
      {% endif %}
      {% if user.is_owner(forum) %}
        <div class="tools">
          <a class="ib" href="{{ url('forum:edit_forum', forum_id=forum.id) }}">{% trans %}Edit{% endtrans %}</a>
          <a class="ib" href="{{ url('forum:delete_forum', forum_id=forum.id) }}">{% trans %}Delete{% endtrans %}</a>
        </div>
      {% endif %}
    </div>
    {% if not is_root %}
      <div class="ib w_small">
        <p class="ib w_medium">
          {{ forum.topic_number }}
        </p>
        <div class="ib w_medium last_message" style="font-size: x-small; text-align: center">
          {% if forum.last_message %}
            {{ user_profile_link_short_name(forum.last_message.author) }} <br/>
            <a href="{{ forum.last_message.get_absolute_url() }}">
              <date>
                {{ forum.last_message.date|localtime|date(DATETIME_FORMAT) }}
                {{ forum.last_message.date|localtime|time(DATETIME_FORMAT) }}
              </date><br>
              <span>
                {{ forum.last_message.topic }}
              </span>
            </a>
          {% endif %}
        </div>
      </div>
    {% endif %}
  </div>
{% endmacro %}

{% macro display_topic(topic, user, first_unread=False) %}
  <div class="topic">
    <div class="ib w_medium">
      {% if first_unread %}
        <a class="ib w_big" href="{{ topic.get_first_unread_message(user).get_absolute_url() }}">
      {% else %}
        <a class="ib w_big" href="{{ url('forum:view_topic', topic_id=topic.id) }}">
      {% endif %}
      <div class="title">{{ topic.title or topic.messages.first().title }}</div>
      <p>{{ topic.description }}</p>
    </a>
    {% if user.can_edit(topic) %}
      <div class="ib tools" style="text-align: center;">
        <a href="{{ url('forum:edit_topic', topic_id=topic.id) }}">{% trans %}Edit{% endtrans %}</a>
      </div>
    {% endif %}
  </div>
  <div class="ib w_medium last_message">
    <div class="ib w_medium">
      <p class="ib w_medium" style="text-align: center;">
        {{ user_profile_link_short_name(topic.author) }}
      </p>
      <p class="ib w_medium" style="text-align: center;">
        {{ topic._message_number }}
      </p>
    </div>
    <p class="ib w_medium" style="text-align: center;">
      {% set last_msg = topic.last_message %}
      {% if last_msg %}
        {{ user_profile_link_short_name(last_msg.author) }} <br/>
        <a href="{{ last_msg.get_absolute_url() }}">
          <date>{{ last_msg.date|localtime|date(DATETIME_FORMAT) }} {{ last_msg.date|localtime|time(DATETIME_FORMAT) }}</date>
        </a>
      {% endif %}
    </p>
  </div>
  </div>
{% endmacro %}

{% macro display_breadcrumb(forum, topic="") %}
  <p>
    <a href="{{ url('forum:main') }}">{% trans %}Forum{% endtrans %}</a>
    {% for f in forum.get_parent_list()|reverse %}
      > <a href="{{ f.get_absolute_url() }}">{{ f }}</a>
    {% endfor %}
    > <a href="{{ forum.get_absolute_url() }}">{{ forum }}</a>
    {% if topic != "" %} > <a href="{{ topic.get_absolute_url() }}">{{ topic }}</a>{%- endif -%}
  </p>
{% endmacro %}

{% macro display_message(m, user, unread=False) %}
  {% set user_is_admin = m.topic.is_owned_by(user) %}
  <article id="msg_{{ m.id }}" class="message {% if unread %}unread{% endif %}">
    {% if user_is_admin or not m._deleted %}
      <div class="message-header">
        {% if m.author.avatar_pict %}
          <img src="{{ m.author.avatar_pict.get_download_url() }}" alt="{% trans %}Profile{% endtrans %}" id="picture" />
        {% elif m.author.profile_pict %}
          <img src="{{ m.author.profile_pict.get_download_url() }}" alt="{% trans %}Profile{% endtrans %}" id="picture" />
        {% else %}
          <img src="{{ static('core/img/unknown.jpg') }}" alt="{% trans %}Profile{% endtrans %}" id="picture" />
        {% endif %}
        <div class="message-metadata">
          <a href="{{ m.author.get_absolute_url() }}">
            <strong>{{ m.author.get_short_name() }}</strong>
          </a>
          <a href="{{ m.get_absolute_url() }}">
            {{ m.date|localtime|date(DATETIME_FORMAT) }}
            {{ m.date|localtime|time(DATETIME_FORMAT) }}
          </a>
        </div>
        <div class="message-options">
          <a href="{{ url('forum:new_message', topic_id=m.topic.id) }}?quote_id={{ m.id }}">
            <i class="fa fa-quote-right"></i>
          </a>
          {% if user.can_edit(m) %}
            <a href="{{ url('forum:edit_message', message_id=m.id) }}">
              <i class="fa fa-pencil edit-action"></i>
            </a>
          {% endif %}

          {% if user_is_admin and m._deleted %}
            <span>
              <a href="{{ url('forum:undelete_message', message_id=m.id) }}">
                {% trans %}Undelete{% endtrans %}
              </a>
            </span>
          {% endif %}
          {% if not m._deleted and (user_is_admin or user.id == m.author_id) %}
            <span>
              <a href="{{ url('forum:delete_message', message_id=m.id) }}">
                <i class="fa fa-trash-can delete-action"></i>
              </a>
            </span>
          {% endif %}
        </div>
      </div>
    {% endif %}  {# close `user.can_view(m) or user_is_admin` #}
    {% if user.can_view(m) %}
      <div
        class="message-content {%- if m.deleted -%}deleted{%- endif -%}"
        {%- if m.id == first_unread_message_id -%}id="first_unread"{%- endif -%}
      >
        {{ m.message|markdown }}
        {% if m.can_be_moderated_by(user) %}
          <ul class="msg_meta">
            {% for meta in m.metas.select_related('user').order_by('id') %}
              <li style="background: {% if m.author == meta.user %}#bfffbf{% else %}#ffffbf{% endif %}">
                {{ meta.get_action_display() }} {{ meta.user.get_short_name() }}
                {% trans %} at {% endtrans %}{{ meta.date|localtime|time(DATETIME_FORMAT) }}
                {% trans %} the {% endtrans %}{{ meta.date|localtime|date(DATETIME_FORMAT)}}
              </li>
            {% endfor %}
          </ul>
        {% endif %}
      </div>
      {% if m.author.forum_signature %}
        <div class="forum-signature">{{ m.author.forum_signature|markdown }}</div>
      {% endif %}
    {% else %}
      <div class="message-content delete">
        <p>{% trans %}Deleted or unreadable message.{% endtrans %}</p>
      </div>
    {% endif %}
  </article>
  {{ m.mark_as_read(user) or "" }}
{% endmacro %}

{% macro display_search_bar(request) %}
  <form class="search_bar" action="{{ url('forum:search') }}" method="GET">
    <input type="text" placeholder="{% trans %}Search{% endtrans %}" name="query" value="{{ request.GET.query|default('') }}"/>
    <input type="checkbox" class="search_check" name="order" value="date" {% if request.GET.order|default("") == "date" or (request.GET.order|default("") == "" and request.GET.query|default("") == "") -%}
             checked
           {%- endif -%}> {% trans %}Order by date{% endtrans %}<br>
    <input type="submit" class="search_bouton" value="{% trans %}Search{% endtrans %}"/>
  </form>
{% endmacro %}