{% load i18n %}
{% load hk_generic %}
{% load decorate %}
{% load tz %}

{% is_message_new email.date as is_new %}
<div class="email{% if unfolded %} email-first{% endif %}{% if is_new %} unread{% endif %}">

    <div id="{{email.message_id_hash}}" class="email-header">
        <div class="gravatar-wrapper d-flex">
            <div class="gravatar circle">
                {% gravatar email.sender.address 120 %}
            </div>
            <div class="email-author d-flex">
                <h2 class="name">
                    {% if email.sender.mailman_id %}
                    <a href="{% url 'hk_public_user_profile' user_id=email.sender.mailman_id %}"
                       title="{% blocktrans with name=email.sender_name|default:email.sender.address|escapeemail|escape %}See the profile for {{ name }}{% endblocktrans %}"
                       >{{email.sender_name|default:email.sender.address|escapeemail}}</a>
                    {% else %}
                       {{email.sender_name|default:email.sender.address|escapeemail}}
                    {% endif %}
                </h2>
            </div>
        </div>
        <div class="email-date right">
            {% if is_new %}
            <i class="fa fa-envelope" title="{% trans 'Unread' %}"></i>
            {% endif %}
            {% ifchanged %}
            <span class="date d-none d-sm-inline">
                {{email.date|date:"j M"}}
                {% if unfolded %}
                    {{email.date|date:"Y"}}
                {% endif %}
            </span>
            <span class="date d-sm-none">
                {{email.date|date:"j M"}}
                {% if unfolded %}
                '{{email.date|date:"y"}}
                {% endif %}
            </span>
            {% endifchanged %}
            <div class="time">
                <span title="{% trans "Sender's time:" %} {% localtime off %}{{email|date_with_senders_timezone|date:'DATETIME_FORMAT'}}{% endlocaltime %}">{{email.date|time:"TIME_FORMAT"}}</span>
            </div>

        </div>
        {% if email.changed_subject %}
        <div class="subject">
          {% trans 'New subject:' %} {{ email.changed_subject }}
        </div>
        {% endif %}
    </div> <!-- /email-header: gravatar, author-info, date, peramlink, changed_subject -->
    <div class="email-body {% if email.display_fixed %}fixed{% endif %}">
      {{ email|render:mlist }}
    </div>

    {% if unfolded and email.attachments.count %}
    <div class="attachments">
        <p class="attachments">{% trans "Attachments:" %}</p>
        <ul class="attachments-list list-unstyled">
        {% for attachment in email.attachments.all %}
            <li><a href="{% url 'hk_message_attachment' mlist_fqdn=mlist.name message_id_hash=email.message_id_hash counter=attachment.counter filename=attachment.name %}">{{attachment.name}}</a>
                ({{attachment.content_type}} &mdash; {{attachment.size|filesizeformat}})
            </li>
        {% endfor %}
        </ul>
    </div>
    {% endif %}

    <div class="email-info">
      <div class="likeform-wrapper right">
        <div class="messagelink pull-right">
          <button class="toggle-font btn btn-sm"
                title="{% trans "Display in fixed font" %}"
                data-bs-toggle="tooltip" data-placement="bottom">
                <i class="fa fa-font"></i>
          </button>
          <a href="{% url 'hk_message_index' mlist_fqdn=mlist.name message_id_hash=email.message_id_hash %}"
             title="{% trans "Permalink for this message" %}"
             data-bs-toggle="tooltip" data-placement="bottom"><i class="fa fa-link"></i></a>
        </div>
        {% include "hyperkitty/fragments/like_form.html" with message_id_hash=email.message_id_hash object=email mlist=mlist %}
        </div>

        <!-- Reply link -->
        {% if posting_enabled %}
        {% if user.is_authenticated %}
        <a class="reply" href="#">
            <i class="fa fa-reply"></i>
            {% trans "Reply" %}
        </a>
        {% else %}
        <a class="reply reply-mailto" title="{% trans "Reply" %}" href="#">
            <i class="fa fa-reply"></i>
            {% trans "Reply" %}
        </a>
        {% endif %}
        {% endif  %}

        <!-- Attachments -->
        {% if not unfolded and email.attachments.count %}
        <div class="attachments dropdown">
            <a class="attachments" data-bs-toggle="dropdown" href="#">
                {% blocktrans count attachments=email.attachments.count %}
                {{ email.attachments.count }} attachment
                {% plural %}
                {{ email.attachments.count }} attachments
                {% endblocktrans %}
                <span class="caret"></span>
                <!-- <i class="fa fa-caret-right"></i> -->
            </a>
            <ul class="attachments-list list-unstyled dropdown-menu">
            {% for attachment in email.attachments.all %}
                <li><a href="{% url 'hk_message_attachment' mlist_fqdn=mlist.name message_id_hash=email.message_id_hash counter=attachment.counter filename=attachment.name %}"
                        title="{{attachment.content_type|escape}}, {{attachment.size|filesizeformat}}"
                        >{{attachment.name}}
                    </a>
                </li>
            {% endfor %}
            </ul>
        </div>
        {% endif %}
        <!-- Reply form -->
        {% if not user.is_authenticated and posting_enabled %}
        <div class="reply-form-unauthenticated">
            <a class="btn btn-sm btn-primary" href="{% url LOGIN_URL %}?next={{next|default:request.path|urlencode}}#">
                {% trans "Sign in to reply online" %}
            </a>
            <a class="btn btn-sm btn-secondary reply-mailto"
                       href="mailto:{{ mlist.name }}?Subject={{ email.subject|reply_subject|urlencode }}&amp;In-Reply-To=&lt;{{ email.message_id|urlencode }}&gt;"
                       >{% trans 'Use email software' %}</a></li>
        </div>
        {% endif %}

        {% if user.is_authenticated and posting_enabled %}
        <div class="reply-form dropdown">
            <form method="post" enctype="multipart/form-data"
                  action="{% url 'hk_message_reply' mlist_fqdn=mlist.name message_id_hash=email.message_id_hash %}">
                {% csrf_token %}
                <ul class="list-inline reply-tools">
                    <li><a class="btn btn-sm btn-secondary quote" href="#">{% trans "Quote" %}</a></li>
                    <li class="checkbox"><label>{{ reply_form.newthread }} {% trans "Create new thread" %}</label></li>
                    <li><a class="btn btn-sm btn-secondary reply-mailto"
                       href="mailto:{{ mlist.name }}?Subject={{ email.subject|reply_subject|urlencode }}&amp;In-Reply-To=&lt;{{ email.message_id|urlencode }}&gt;"
                       >{% trans 'Use email software' %}</a></li>
                </ul>
                {{ reply_form.non_field_errors }}

                <p class="new-subject">{{ reply_form.subject }} {{ reply_form.subject.errors}}</p>
                <p class="message-text">{{ reply_form.message }} {{ reply_form.message.errors}}</p>

                {% include "hyperkitty/fragments/send_as.html" with form=reply_form %}

                <p class="buttons">
                    <button type="submit" class="submit btn btn-primary">{% trans "Send" %}</button>
                    {% trans "or" %} <a class="cancel" href="#">{% trans "cancel" %}</a>
                </p>
            </form>
        </div>
        {% endif %}

    </div>

</div>
