{% extends "layouts/" + render_mode + ".html" %}
{% block title %}{{ nice_subject(result.data.metadata[result.thread_ids[-1]]) }}{% endblock %}
{% block content %}
{% if result %}
{% 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 class="thread-snippet" id="snippet-{{ mid }}" data-mid="{{ mid }}">
      {% set from = metadata.from %}
      {% include("partials/thread_metadata.html") %}
      {% if metadata.crypto.encryption in ("missingkey", "error", "mixed-missingkey", "mixed-error") %}
      <div class="thread-item-text"><em>{{_("Unable to see message preview")}}</em></div>
      {% else %}
      <div class="thread-item-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 == 'tag' %}
    <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 #}
  {% set error_title = "message_missing" %}
  {% include("partials/errors_content.html") %}
{% endif  %}{# if result #}
{% endblock %}
