<div id="message-{{mid}}" class="pile-message {% 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 %}
  {%- set encryption_status = [] %}
  {%- set signature_status = [] %}
  {%- set special_text_parts = [] %}
  {%- macro msg_crypto(part, forcedisplay) %}
    {#-
     # Parts that do not have own crypto attribute inherit from message itself
     # Watch for changes to the "status" which is present in each encryption and
     # signature section - if either has changed, then we have moved from
     # one security context to the next, and need to let the user know.
     #}
    {%- set part_encryption = (part.crypto and part.crypto.encryption or message.crypto.encryption) %}
    {%- set part_signature = (part.crypto and part.crypto.signature or message.crypto.signature) %}
    {%- set encryption = show_text_part_encryption(part_encryption and part_encryption.status) %}
    {%- set signature = show_text_part_signature(part_signature and part_signature.status) %}

    {%- if encryption_status and (forcedisplay or part_encryption.status != encryption_status[-1]) %}
    <div class="part-crypto-status clearfix crypto-changed border-{{ encryption.color }}">
      <span class="message-inline-crypto-info {{encryption.color}}">&#x2198;</span>
    {%- elif signature_status and part_signature.status != signature_status[-1] %}
    <div class="part-crypto-status clearfix crypto-changed border-{{ signature.color }}">
      <span class="message-inline-crypto-info {{signature.color}}">&#x2198;</span>
    {%- else %}
    <div class="part-crypto-status clearfix border-{{ encryption.color }}">
    {%- endif %}

    {%- if part_encryption %}
      {%- if not encryption_status or forcedisplay or part_encryption.status != encryption_status[-1] %}
      <span class="message-inline-crypto-info inline-encryption-info"
            title="{{ encryption.text }}"
            data-crypto_color="{{ encryption.color }}"
            data-crypto_icon="{{ encryption.icon }}"
            data-crypto_message="{{ encryption.message }}"
            data-crypto_keyinfo="{{ part_encryption.keyinfo }}">
        <span class="icon {{ encryption.color }} {{ encryption.icon }}"></span>
        <span class="text {{ encryption.color }}">{{ encryption.text }}</span>
      </span>
      {%- endif %}
      {%- do encryption_status.append(part_encryption.status) %}
    {%- endif %}

    {%- if part_signature %}
      {%- if not signature_status or part_signature.status != signature_status[-1] %}
      <span class="message-inline-crypto-info inline-signature-info"
            title="{{ signature.text }}"
            data-crypto_color="{{ signature.color }}"
            data-crypto_icon="{{ signature.icon }}"
            data-crypto_message="{{ signature.message }}"
            data-crypto_keyinfo="{{ part_signature.keyinfo }}">
        <span class="icon {{ signature.color + ' ' + signature.icon }}"></span>
        <span class="text {{ signature.color }}">{{ signature.text }}</span>
      </span>
      {%- endif %}
      {%- do signature_status.append(part_signature.status) %}
    {%- endif %}
    </div>
  {%- endmacro %}
  <div class="pile-message-content">
  {%- if message.text_parts %}
    {%- set last_part = message.text_parts|length - 1 %}
    {%- for part in message.text_parts %}
    <div class="pile-message-text-part">
      {%- if part.data|trim %}
        {{- msg_crypto(part, False) }}
        {%- autoescape false %}
        {%- set part_text = part.data|nice_text|e|urlize|fix_urls(40, metadata.flags.spam) %}
        {%- if part.type in ("text", "pgpsignedtext") %}
      <div class="message-part-text">{{ part_text|to_br }}</div>
        {%- elif part.type in ("pgptext",) %}
          {%- if part.crypto.encryption.status in ("lockedkey", "missingkey", "error") %}
            {%- set failed_crypto = part.crypto.encryption %}
            {%- include("partials/thread_message_cryptofail.html") %}
          {%- else %}
      <div class="message-part-text">{{ part_text|to_br }}</div>
          {%- endif %}
        {%- elif part.type in ("pgpbegin", "pgpend") %}
      <div class="message-part-{{part.type}} hide">{{part.data}}</div>
        {%- elif part.type == "quote" %}
          {# If this is the 2nd-to-last part, and the LAST part is a quote
             or signature, we shall also hide. #}
          {%- if loop.index == (loop.length - 1) and
                 message.text_parts[last_part].type in ("signature", "quote") -%}
            {% do special_text_parts.append("quote") %}
      <div class="message-part-quote hide">{{ part_text|to_br }}</div>
          {# If this part is a quote at end of message, hide #}
          {%- elif loop.last %}
            {%- do special_text_parts.append("quote") %}
      <div class="message-part-quote hide">{{ part_text|to_br }}</div>
          {%- else %}
      <div class="message-part-quote">{{ part_text|to_br }}</div>
          {%- endif -%}
        {%- elif part.type == "signature" %}
          {%- do special_text_parts.append("signature") %}
      <div class="message-part-signature hide">{{ part_text|to_br }}</div>
        {%- else %}
      <div class="message-part-text"><em>{{_("Unknown Text Part")}}</em></div>
        {%- endif %}
        {%- endautoescape %}
      {%- endif %}
    </div>
    {%- endfor %}
    {%- for part in message.html_parts %}
    <div id="html-part-{{ mid }}-{{ loop.index }}"
         class="pile-message-html-part hide">
      {{ msg_crypto(part, loop.first) }}
      <div class="message-part-html">
        <i class="noframe">{{ _("HTML rendering requires Javascript, sorry!") }}</i>
      </div>
    </div>
    {%- endfor %}
  {%- else %}
    {%- if message.crypto.encryption.status in ("lockedkey", "missingkey", "error") %}
      {%- set failed_crypto = message.crypto.encryption %}
      {%- include("partials/thread_message_cryptofail.html") %}
    {%- elif not message.attachments %}
      <div class="message-part-text"><em>{{_("Message content is empty")}}</em></div>
    {%- endif %}
  {%- endif %}
  {%- if message.attachments %}
    <div class="pile-message-attachments">
      <ul class="pile-message-attachments horizontal clearfix">
    {%- for att in message.attachments %}
        <li class="left">
      {# FIXME: Too ugly, for now. { msg_crypto(att, False) } #}
      {%- set type = attachment_type(att.mimetype) %}
      {%- if (att.filename) %}
        {%- set filename = att.filename %}
      {%- else %}
        {%- set filename = _("Untitled") %}
      {%- endif %}
      {%- set is_image = att.mimetype in ["image/bmp", "image/gif", "image/jpg", "image/jpeg", "image/pjpeg", "image/svg+xml", "image/x-png", "image/png", "application/vnd.google-apps.photo"] %}
      {%- set is_pdf = (att.mimetype in ["application/pdf"]) %}
      {%- set file_parts = filename.split(".") %}
      {%- set file_parts_length = file_parts|length %}
      {#- if type == "keys" #}
        {#- do special_text_parts.append("pgp-key") #}
        {#- FIXME: We should omit these (and other boring attachments), but
                   instead add a mode that lets users easily download any part
                   they like. Like mutt! #}
      {#- else #}
        <a href="{{ U('/message/download/get/=', mid, '/part-', att.count, '/') }}"
        {%- if is_image %}
           class="attachment-image" target=_blank
        {%- else %}
           class="attachment"{% if is_pdf %} target="_blank"{% endif %}
        {%- endif %}
        {%- if att.crypto.encryption.description %}
           data-description="{{ att.crypto.encryption.description }}"
        {%- endif %}
           data-size="{{att.length|friendly_bytes}}"
           title="{{filename}}" type="{{att.mimetype}}">
        {%- if is_image %}
          <div class="preview" style="background-image: url('{{ U('/message/download/preview/=', mid, '/part-', att.count, '/') }}');"></div>
        {%- else %}
          <div class="preview">
            <span class="icon-mime" type="{{att.mimetype}}"></span>
          {%- if filename != _("Untitled") %}
            <span class="extension">
              {{- file_parts[file_parts_length - 1] -}}
            </span>
          {%- endif %}
          </div>
          <div class="filename">
          {%- if att.crypto.encryption.status == "decrypted" %}
            <span class="icon-lock-closed crypto-encrypted"></span>&nbsp;
          {%- elif att.crypto.encryption.status == "encrypted" %}
            <span class="icon-lock-closed crypto-error"></span>&nbsp;
          {%- endif %}
          {%- if file_parts_length > 2 or filename|length > 20 %}
              {{- att.filename[0:15] -}}...
          {%- elif file_parts[0] %}
              {{- file_parts[0] -}}
          {%- endif %}
          </div>
        {%- endif %}
        </a>
      {#- endif #}
        </li>
    {%- endfor %}
      </ul>
    </div>
  {%- endif %}
  </div>
  <div class="message-actions clearfix">
    <ul class="message-actions horizontal left">
      {%- if "quote" in special_text_parts or "signature" in special_text_parts %}
      <li class="action show-quotes">
        <a data-show="#message-{{mid}} .pile-message-content .hide"
           data-hide="#message-{{mid}} .show-quotes"
           class="do-show message-actions-quote"
           href="#">&middot;&middot;&middot;&middot;</a>
      </li>
      {%- endif %}
    </ul>
  </div>
  <p class="message-actions-padding">&nbsp;</p>
  <ul class="message-actions horizontal bottom left">
    <li class="action"><a class="message-action-reply-all" href="#"><span class="icon-reply-all"></span> {{_("Reply")}}</a></li>
    <li class="action"><a class="message-action-forward" href="#"><span class="icon-forward"></span> {{_("Forward")}}</a></li>
  </ul>
</div>
<script>
$(document).ready(function() {
  Mailpile.Message.AnalyzeMessageInline('{{mid}}');
  $('#message-{{mid}}').data('html', {{ message.html_parts|json|safe }});
  {%- if message.html_parts|length > 0 and
         from.get('html-policy', 'none') != 'none' %}
    Mailpile.Message.ShowHTML('{{mid}}', '{{from["html-policy"]}}');
  {%- endif %}
});
</script>
