{%- set fileAttachments = attachments|filter(a => not (a.mimeType starts with "image/" or a.mimeType starts with "video/")) -%}
{%- set imageAttachments = attachments|filter(a => a.mimeType starts with "image/" or a.mimeType starts with "video/") -%}

{%- set canReply =
    not post.deleted
    and not topic.isLocked
    and currentUser is not null
    and currentUser.hasPermission(permission("CREATE_OWN_POST"))
-%}

{%- set canEdit =
    not post.deleted
    and not topic.isLocked
    and currentUser is not null
    and (
        (
            postAuthor is not null
            and postAuthor.id == currentUser.id
            and postAuthor.hasPermission(permission("EDIT_OWN_POST"))
        )
        or currentUser.hasPermission(permission("EDIT_OTHER_POST"))
    ) -%}

{%- set canDelete =
    not post.deleted
    and currentUser is not null
    and (
        (
            postAuthor is not null
            and postAuthor.id == currentUser.id
            and postAuthor.hasPermission(permission("DELETE_OWN_POST"))
        )
        or currentUser.hasPermission(permission("DELETE_OTHER_POST"))
    ) -%}

{%- set canViewAttachments = currentUser is not null -%}

{%- set your_are_the_author =
    currentUser is not null
    and postAuthor is not null
    and currentUser.id == postAuthor.id
-%}

{%- set is_op =
    topicAuthor is not null
    and postAuthor is not null
    and postAuthor.id == topicAuthor.id
-%}

{% if post.deleted %}
    <div class="media" id="post-{{ post.id }}">
        <div class="media-left hidden-sm hidden-xs">
            <div class="media-object" style="width:64px"></div>
        </div>
        <div class="media-body">
            <div class="well icon-well text-warning">
                <span class="fa fa-exclamation-triangle text-warning" aria-hidden="true"></span>
                <em>{{ __("This post has been deleted") }}</em>
            </div>
        </div>
    </div>
{% else %}
    <div class="media" id="post-{{ post.id }}" data-text="{{ post.content }}" style="overflow: visible;">
        {% if not hide_pfp %}
            <div class="media-left hidden-sm hidden-xs">
                {% if postAuthor %}
                    {% if hide_actions %}
                        <img class="media-object" alt="{{ __("Profile picture") }}" src="?_action=profilepicture&amp;user={{ postAuthor.id|url_encode }}" width="64" height="64">
                    {% else %}
                        <a href="?_action=viewuser&amp;user={{ postAuthor.id|url_encode }}">
                            <img class="media-object" alt="{{ __("Profile picture") }}" src="?_action=profilepicture&amp;user={{ postAuthor.id|url_encode }}" width="64" height="64">
                        </a>
                    {% endif %}
                {% else %}
                    <div class="media-object" style="width:64px;height:64px"></div>
                {% endif %}
            </div>
        {% endif %}
        <div class="media-body" style="overflow: visible;">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div class="panel-title h3">
                        {% if not hide_actions %}
                            <div class="pull-right">
                                <a href="#post-{{ post.id }}" class="btn btn-default"><span class="fa fa-link" aria-hidden="true"></span><span class="sr-only">{{ __("Permalink") }}</span></a>
                                {% if canReply %}
                                    <button data-post-id="{{ post.id }}" class="btn btn-default js-only _reply-post"><span class="fa fa-comment" aria-hidden="true"></span><span class="sr-only">{{ __("Reply to post") }}</span></button>
                                {% endif %}
                                {% if canEdit %}
                                    <button data-post-id="{{ post.id }}" class="btn btn-default js-only _edit-post"><span class="fa fa-pencil" aria-hidden="true"></span><span class="sr-only">{{ __("Edit post") }}</span></button>
                                {% endif %}
                                {% if canDelete %}
                                    <form action="?_action=deletepost" method="post" class="seamless-inline">
                                        <input type="hidden" name="post" value="{{ post.id }}">
                                        <button type="submit" class="btn btn-danger"><span class="fa fa-trash" aria-hidden="true"></span><span class="sr-only">{{ __("Delete post") }}</span></button>
                                    </form>
                                {% endif %}
                            </div>
                        {% endif %}
                        {% if postAuthor %}
                            {% if hide_actions %}
                                {{ postAuthor.displayName }}
                            {% else %}
                                <a href="?_action=viewuser&amp;user={{ postAuthor.id|url_encode }}">{{ postAuthor.displayName }}</a>
                                {% if is_op %}
                                    <span title="{{ __("Created this topic") }}" class="text-info fa fa-user-circle"></span>
                                {% endif %}
                            {% endif %}
                            {% if your_are_the_author %}
                                <span class="text-normal label label-primary">{{ __("You") }}</span>
                            {% endif %}
                        {% else %}
                            <em class="text-muted">{{ __("(deleted)") }}</em>
                        {% endif %}
                    </div>
                    <span class="_time">{{ post.postDate.format("c") }}</span>
                    {% if post.edited %}
                        <em class="text-muted">{{ __("(edited)") }}</em>
                    {% endif %}
                </div>
                <div class="panel-body">
                    <div class="post-content">{{ renderPost(post.content) }}</div>
                    {% if imageAttachments|length > 0 %}
                        <div class="post-images clearfix">
                            {% for attachment in imageAttachments %}
                                {% if hide_actions %}
                                    <span class="image-attachment" title="{{ attachment.name }}">
                                        <img class="image-attachment-image" src="?_action=thumb&amp;attachment={{ attachment.id|url_encode }}" alt="" width="100">
                                    </span>
                                {% else %}
                                    <a class="
                                        image-attachment
                                        attachment
                                        {{ attachment.mimeType starts with "video/" ? "video-attachment" }}
                                    " href="?_action=attachment&amp;attachment={{ attachment.id|url_encode }}" title="{{ attachment.name }}" data-attachment-id="{{ attachment.id }}">
                                        <img class="image-attachment-image" src="?_action=thumb&amp;attachment={{ attachment.id|url_encode }}" alt="" width="100">
                                        {% if not canViewAttachments %}
                                            <span class="attachment-lock fa fa-ban" aria-hidden="true"></span>
                                        {% elseif attachment.mimeType starts with "video/" %}
                                            <span class="video-player-icon fa fa-play-circle" aria-hidden="true"></span>
                                        {% endif %}
                                    </a>
                                {% endif %}
                            {% endfor %}
                        </div>
                    {% endif %}
                </div>
                {% if fileAttachments|length > 0 %}
                    <div class="panel-footer">
                        <div class="btn-group">
                            {% for attachment in fileAttachments %}
                                {% if hide_actions %}
                                    <button class="btn btn-default">{{ attachment.name }}</button>
                                {% else %}
                                    <a class="btn btn-default attachment" href="?_action=attachment&amp;attachment={{ attachment.id|url_encode }}">
                                        {% if not canViewAttachments %}
                                            <span class="fa fa-ban" aria-hidden="true"></span>
                                        {% endif %}
                                        {{ attachment.name }}
                                    </a>
                                {% endif %}
                            {% endfor %}
                        </div>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>
{% endif %}