{% extends 'messages/master.twig' %}
{% from 'macros.twig' import avatar, container_title %}
{% from '_layout/input.twig' import input_hidden, input_text, input_select %}

{% set title = 'Viewing message' %}
{% set canonical_url = url('messages-view', { message: message.info.id }) %}

{% block messages_content %}
    <div class="messages-columns">
        <div class="messages-columns-sidebar">
            <div class="messages-sidebar">
                <div class="messages-sidebar-button">
                    <a class="input__button" href="{{ url('messages-index') }}">Back</a>
                </div>

                <div class="container messages-sidebar-section messages-actions">
                    {{ container_title('<i class="fas fa-comments fa-fw"></i> Actions') }}

                    <button class="messages-actions-item js-messages-actions-mark-read"{% if not message.info.isSent %} hidden{% endif %} data-state="active" data-inactive-str="Mark as read" data-inactive-ico="fas fa-envelope-open fa-fw" data-active-str="Mark as unread" data-active-ico="fas fa-envelope">
                        <div class="messages-actions-item-icon js-messages-button-icon"><i></i></div>
                        <div class="messages-actions-item-label js-messages-button-label"></div>
                    </button>
                    <button class="messages-actions-item js-messages-actions-move-trash" data-state="{{ message.info.isDeleted ? 'active' : 'inactive' }}" data-inactive-str="Move to Trash" data-inactive-ico="fas fa-trash-alt fa-fw" data-active-str="Restore item" data-active-ico="fas fa-trash-restore-alt">
                        <div class="messages-actions-item-icon js-messages-button-icon"><i></i></div>
                        <div class="messages-actions-item-label js-messages-button-label"></div>
                    </button>
                    <button class="messages-actions-item js-messages-actions-nuke"{% if not message.info.isDeleted %} hidden{% endif %}>
                        <div class="messages-actions-item-icon"><i class="fas fa-radiation-alt"></i></div>
                        <div class="messages-actions-item-label">Permanently delete</div>
                    </button>
                </div>
            </div>
        </div>
        <div class="messages-columns-content">
            <div class="messages-thread js-messages-thread">
                {% if reply_to is defined and reply_to is not empty %}
                    <article class="{{ html_classes('container', 'messages-message', 'messages-message-snippet', 'js-messages-message', {'messages-message-deleted': reply_to.info.isDeleted, 'messages-message-draft': not reply_to.info.isSent}) }}" tabindex="0" data-msg-id="{{ reply_to.info.id }}" data-msg-url="{{ url('messages-view', {message: reply_to.info.id}) }}" data-msg-type="snip" data-msg-sent="{{ reply_to.info.isSent ? 'sent' : 'draft' }}" data-msg-read="{{ reply_to.info.isRead ? 'read' : 'unread' }}">
                        <div class="messages-message-header">
                            <div class="messages-message-sender-avatar">
                                {{ avatar(reply_to.author_info.id|default(0), 80) }}
                            </div>
                            <div class="messages-message-details">
                                <div class="messages-message-header-columns">
                                    <div class="messages-message-sender-name" style="--user-colour: {{ reply_to.author_colour }};">
                                        <a href="{{ url('user-profile', {user: reply_to.author_info.id|default(0)}) }}" class="messages-message-overflow">{{ reply_to.author_info.name|default('Deleted User') }}</a>
                                    </div>
                                    <div class="messages-message-details-spacing"></div>
                                    <div class="messages-message-datetime">
                                        <time datetime="{{ reply_to.info.displayTime|date('c') }}" title="{{ reply_to.info.displayTime|date('r') }}">{{ reply_to.info.displayTime|time_format }}</time>
                                    </div>
                                </div>
                                <div class="messages-message-addressee">
                                    <div class="messages-message-addressee-to">To: </div>
                                    <div class="messages-message-addressee-user" style="--user-colour: {{ reply_to.recipient_colour }};">
                                        <a href="{{ url('user-profile', {user: reply_to.recipient_info.id|default(0)}) }}" class="messages-message-overflow">{{ reply_to.recipient_info.name|default('Deleted User') }}</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="messages-message-subject">
                            <h1>{{ reply_to.info.title }}</h1>
                        </div>
                        <div class="messages-message-snippet-body">
                            <p>{{ reply_to.info.body }}</p>
                        </div>
                    </article>
                {% endif %}

                <article class="{{ html_classes('container', 'messages-message', 'js-messages-message', {'messages-message-deleted': message.info.isDeleted, 'messages-message-draft': not message.info.isSent}) }}" data-msg-id="{{ message.info.id }}" data-msg-url="{{ url('messages-view', {message: message.info.id}) }}" data-msg-type="full" data-msg-sent="{{ message.info.isSent ? 'sent' : 'draft' }}" data-msg-read="{{ message.info.isRead ? 'read' : 'unread' }}" data-msg-deleted="{{ message.info.isDeleted ? 'yes' : 'no' }}">
                    <div class="messages-message-header">
                        <div class="messages-message-sender-avatar">
                            {{ avatar(message.author_info.id|default(0), 80) }}
                        </div>
                        <div class="messages-message-details">
                            <div class="messages-message-header-columns">
                                <div class="messages-message-sender-name" style="--user-colour: {{ message.author_colour }};">
                                    <a href="{{ url('user-profile', {user: message.author_info.id|default(0)}) }}" class="messages-message-overflow">{{ message.author_info.name|default('Deleted User') }}</a>
                                </div>
                                <div class="messages-message-details-spacing"></div>
                                <div class="messages-message-datetime">
                                    <time datetime="{{ message.info.displayTime|date('c') }}" title="{{ message.info.displayTime|date('r') }}">{{ message.info.displayTime|time_format }}</time>
                                </div>
                            </div>
                            <div class="messages-message-addressee">
                                <div class="messages-message-addressee-to">To: </div>
                                <div class="messages-message-addressee-user" style="--user-colour: {{ message.recipient_colour }};">
                                    <a href="{{ url('user-profile', {user: message.recipient_info.id|default(0)}) }}" class="messages-message-overflow">{{ message.recipient_info.name|default('Deleted User') }}</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="messages-message-subject">
                        <h1>{{ message.info.title }}</h1>
                    </div>
                    <div class="messages-message-body{% if message.info.isBodyMarkdown %} markdown{% endif %}">{{ message.info.body|escape|parse_text(message.info.parser)|raw }}</div>
                </article>

                {% if can_send_messages %}
                    {% set has_draft_info = draft_info is defined and draft_info is not null %}
                    {% set reply_field_is_draft = false %}
                    {% if not has_draft_info and not message.info.isSent %}
                        {% set has_draft_info = true %}
                        {% set reply_field_is_draft = true %}
                        {% set draft_info = message.info %}
                    {% endif %}

                    {% set msg_author_id = message.info.authorId|default(0) %}
                    {% set msg_recipient_id = message.info.recipientId|default(0) %}

                    {% if has_draft_info or (msg_author_id > 0 and msg_recipient_id > 0) %}
                        <div class="container messages-reply js-messages-reply"{% if (reply_field_is_draft ? draft_info.isDeleted : message.info.isDeleted) %} hidden{% endif %}>
                            {{ container_title(has_draft_info ? '<i class="fas fa-edit"></i> Edit' : '<i class="fas fa-reply"></i> Reply') }}

                            <form class="messages-reply-form js-messages-reply-form">
                                {% if has_draft_info %}
                                    {{ input_hidden('message', draft_info.id) }}
                                {% else %}
                                    {{ input_hidden('reply', message.info.id) }}
                                    {{ input_hidden('recipient', self_info.id == msg_author_id ? msg_recipient_id : msg_author_id) }}
                                {% endif %}
                                <div class="messages-reply-subject">
                                    {{ input_text('title', 'messages-reply-subject-input', draft_info.title|default('%s%s'|format((message.info.title|slice(0, 4) == 'Re: ' ? '' : 'Re: '), message.info.title)), 'text', 'Subject', true) }}
                                </div>
                                <div class="messages-reply-body">
                                    <textarea name="body" placeholder="Write your reply here...&#10;Press Ctrl+Enter to send your reply&#10;Press Ctrl+Shift+Enter to save a draft" class="input__textarea messages-reply-body-input js-messages-reply-body">{{ draft_info.body|default('') }}</textarea>
                                </div>
                                <div class="messages-reply-actions js-messages-reply-actions" hidden></div>
                                <div class="messages-reply-options">
                                    <div class="messages-reply-settings">
                                        {{ input_select('parser', constant('\\Misuzu\\Parsers\\Parser::NAMES'), draft_info.parser|default('1'), null, null, null, 'js-messages-reply-parser') }}
                                    </div>
                                    <div class="messages-reply-buttons">
                                        <button class="input__button js-messages-reply-save" name="draft" value="1">Save draft</button>
                                        <button class="input__button js-messages-reply-send" name="draft" value="0">Reply</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    {% endif %}
                {% endif %}

                {% if replies_for is defined and replies_for is iterable %}
                    {% for reply_for in replies_for %}
                        <article class="{{ html_classes('container', 'messages-message', 'messages-message-snippet', 'js-messages-message', {'messages-message-deleted': reply_for.info.isDeleted, 'messages-message-draft': not reply_for.info.isSent}) }}" tabindex="0" data-msg-id="{{ reply_for.info.id }}" data-msg-url="{{ url('messages-view', {message: reply_for.info.id}) }}" data-msg-type="snip" data-msg-sent="{{ reply_for.info.isSent ? 'sent' : 'draft' }}" data-msg-read="{{ reply_for.info.isRead ? 'read' : 'unread' }}">
                            <div class="messages-message-header">
                                <div class="messages-message-sender-avatar">
                                    {{ avatar(reply_for.author_info.id|default(0), 80) }}
                                </div>
                                <div class="messages-message-details">
                                    <div class="messages-message-header-columns">
                                        <div class="messages-message-sender-name" style="--user-colour: {{ reply_for.author_colour }};">
                                            <a href="{{ url('user-profile', {user: reply_for.author_info.id|default(0)}) }}" class="messages-message-overflow">{{ reply_for.author_info.name|default('Deleted User') }}</a>
                                        </div>
                                        <div class="messages-message-details-spacing"></div>
                                        <div class="messages-message-datetime">
                                            <time datetime="{{ reply_for.info.displayTime|date('c') }}" title="{{ reply_for.info.displayTime|date('r') }}">{{ reply_for.info.displayTime|time_format }}</time>
                                        </div>
                                    </div>
                                    <div class="messages-message-addressee">
                                        <div class="messages-message-addressee-to">To: </div>
                                        <div class="messages-message-addressee-user" style="--user-colour: {{ reply_for.recipient_colour }};">
                                            <a href="{{ url('user-profile', {user: reply_for.recipient_info.id|default(0) }) }}" class="messages-message-overflow">{{ reply_for.recipient_info.name|default('Deleted User') }}</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="messages-message-subject">
                                <h1>{{ reply_for.info.title }}</h1>
                            </div>
                            <div class="messages-message-snippet-body">
                                <p>{{ reply_for.info.body }}</p>
                            </div>
                        </article>
                    {% endfor %}
                {% endif %}
            </div>
        </div>
    </div>
{% endblock %}
