{% extends "layouts/" + render_mode + ".html" %}
{% block title %}{{ nice_subject(result.data.metadata[result.thread_ids[-1]].subject) }}{% endblock %}
{% block content %}
{% if result and not result.errors %}
{% set start_mid     = result.thread_ids[-1] %}
{% set last_mid      = result.thread_ids.0 %}
{% set start_subject = result.data.metadata[start_mid].subject %}
{% set profiles      = mailpile('profiles').result %}

{% set loop_history  = [] %}
{% for mid in result.thread_ids|reverse %}
  {% set metadata = result.data.metadata.get(mid) %}
  {% set message = result.data.messages.get(mid) %}

  {# Render the composer #}
  {% if message and "editing_strings" in message %}
    {% set editing_strings = message.editing_strings %}
    {% set editing_addresses = result.data.addresses %}
    {% set attachments = message.attachments %}
    {% include("partials/compose.html") %}
    {% do loop_history.append("edit message") %}

  {# Render full message #}
  {% elif message %}
    {% include("partials/thread_message.html") %}{# Show: Message #}
    {% set show_reply = (mid != last_mid) %}
    {% do loop_history.append("display message") %}

  {# Message is a draft, show composer #}
  {% elif metadata.flags.draft %}
    {# FIXME: We should probably just tell the user this is a draft, make
              loading the composer a manual action UNLESS this is the last
              message in the the thread.
     #}
    {% set compose = mailpile('message/draft', 'all', '='+mid).result %}{# Gets Ephermal message #}
    {% if compose %}
      {% do loop_history.append("edit draft") %}
      {% set mid = compose.message_ids.0 %}
      {% if "editing_strings" in compose.data.messages[mid] %}
      {% set editing_strings = compose.data.messages[mid].editing_strings %}
      {% set editing_addresses = compose.data.addresses %}
      {% set attachments = compose.data.messages[mid].attachments %}
      {% include("partials/compose.html") %}
      {% endif %}
    {% else %}
      {{_("Could Not Render Composer")}}
    {% endif %}
  {% else %}{# Just render snippet view #}
    <div id="message-{{ mid }}" class="thread-snippet {% for tid in metadata.tag_tids %}{% if result.data.tags[tid].slug == "new" %}{{result.data.tags[tid].slug}}{% endif %}{% endfor %}" data-mid="{{ mid }}">
      {% set from = metadata.from %}
      {% include("partials/thread_metadata.html") %}
      {% if metadata.crypto.encryption in ("lockedkey", "missingkey", "error", "mixed-lockedkey", "mixed-missingkey", "mixed-error") %}
      <div class="message-part-text"><em>{{_("Unable to see message preview")}}</em></div>
      {% else %}
      <div class="message-part-text">{{ metadata.body.snippet }}</div>
      {% endif %}
    </div>
    {% do loop_history.append("display snippet") %}
  {% endif %}
{% endfor %}{# End for loop #}

{% if loop_history|last not in ("edit draft", "edit message") %}
  {# Give the user the option to reply to the last message in the thread unless we already did! #}
  {% set compose = mailpile('message/reply', 'all', 'ephemeral', '='+last_mid).result %}{# Gets Ephermal message #}
  {% if compose %}
    {% set mid = compose.message_ids.0 %}
    {% set editing_strings = compose.data.messages[mid].editing_strings %}
    {% set editing_addresses = compose.data.addresses %}
    {% set attachments = compose.data.messages[mid].attachments %}
    {% include("partials/compose.html") %}
  {% else %}
    {{_("Could Not Render Composer")}}
  {% endif %}
{% endif %}

<div id="thread-people" class="hide" data-modal_title="{{_("People in Conversation")}}">
  <ul>
    {% for aid in result.data.addresses %}{% set person = result.data.addresses[aid] %}
    <li>
      <a class="contact-conversation-avatar" href="{{ contact_url(person) }}"><img src="{{ show_avatar(person) }}"></a>
      <a class="contact-conversation-name" href="{{ contact_url(person) }}"> <h4>{{person.fn}}</h4></a>
      <a class="contact-conversation-address" href=""><span class="icon-compose"></span> {{person.address}}</a>
    </li>
    {% endfor %}
  </ul>
</div>
<div id="thread-tags" class="hide" data-modal_title="{{_("Tags in Conversation")}}">
  <ul>
    {% for tid in result.data.tags %}{% set tag = result.data.tags[tid] %}
    {% if tag.type in ['tag', 'inbox', 'sent', 'spam', 'trash'] %}
    <li>
      <a class="" href="{{ tag.url }}"><span class="icon-tag"></span> <h4>{{ tag.name }}</h4></a>
    </li>
    {% endif %}
    {% endfor %}
  </ul>
</div>

<script>
$(document).ready(function() {
  Mailpile.Message.init();
});
</script>

{% else %}{# No Result, Show Error #}
  {% include("partials/error_message_missing.html") %}
{% endif  %}{# if result #}
{% endblock %}
