{% macro forum_category_listing(forums, title, colour, id, icon) %}
    {% from _self import forum_category_entry %}
    {% from 'macros.twig' import container_title %}

    <div class="container forum__categories"
        {% if colour is not null %}style="{{ colour|html_colour('--accent-colour') }}"{% endif %}
        {% if id|length > 0 %}id="{{ id }}"{% endif %}>
        {{ container_title('<span class="' ~ icon|default('fas fa-folder fa-fw') ~ '"></span> ' ~ title) }}

        {% if forums|length > 0 %}
            <div class="forum__categories__list">
                {% for forum in forums %}
                    {{ forum_category_entry(forum) }}
                {% endfor %}
            </div>
        {% else %}
            <div class="forum__categories__empty">
                This category is empty.
            </div>
        {% endif %}
    </div>
{% endmacro %}

{% macro forum_header(title, breadcrumbs, omit_last_breadcrumb, title_url, actions) %}
    <div class="container forum__header">
        {% if breadcrumbs is iterable and breadcrumbs|length > 0 %}
            <div class="forum__header__breadcrumbs">
                {% for name, url in breadcrumbs %}
                    {% if url != breadcrumbs|first %}
                        <div class="forum__header__breadcrumb__separator">
                            <i class="fas fa-chevron-right"></i>
                        </div>
                    {% endif %}

                    {% if not (omit_last_breadcrumb|default(false) and url == breadcrumbs|last) %}
                        <a href="{{ url }}" class="forum__header__breadcrumb">{{ name }}</a>
                    {% endif %}
                {% endfor %}
            </div>
        {% endif %}

        {% if title|length > 0 %}
            {% if title_url|length > 0 %}
                <a class="forum__header__title" href="{{ title_url }}">
                    {{ title }}
                </a>
            {% else %}
                <div class="forum__header__title forum__header__title--fill">
                    {{ title }}
                </div>
            {% endif %}
        {% endif %}

        {% if actions is iterable and actions|length > 0 %}
            <div class="forum__header__actions">
                {% for action in actions %}
                    {% if action.display is not defined or action.display %}
                        <a class="forum__header__action{% if action.class is defined %}{{ action.class }}{% endif %}" href="{{ action.url }}">
                            {{ action.html|raw }}
                        </a>
                    {% endif %}
                {% endfor %}
            </div>
        {% endif %}
    </div>
{% endmacro %}

{% macro forum_category_tools(info, perms, pagination_info) %}
    {% from 'macros.twig' import pagination %}

    {% set is_locked = info.forum_archived != 0 %}
    {% set can_topic = not is_locked and perms|perms_check(constant('MSZ_FORUM_PERM_CREATE_TOPIC')) %}
    {% set pag = pagination(pagination_info, url('forum-category'), null, {'f': info.forum_id}) %}

    {% if can_topic or pag|trim|length > 0 %}
        <div class="container forum__actions">
            <div class="forum__actions__buttons">
                {% if can_topic %}
                    <a href="{{ url('forum-topic-new', {'forum': info.forum_id}) }}" class="input__button forum__actions__button">New Topic</a>
                {% endif %}
            </div>

            <div class="forum__actions__pagination">
                {{ pag }}
            </div>
        </div>
    {% endif %}
{% endmacro %}

{% macro forum_topic_tools(info, pagination_info, can_reply) %}
    {% from 'macros.twig' import pagination %}

    {% set pag = pagination(pagination_info, url('forum-topic'), null, {'t': info.topic_id}, 'page') %}

    {% if can_reply or pag|trim|length > 0 %}
        <div class="container forum__actions">
            <div class="forum__actions__buttons">
                {% if can_reply %}
                    <a href="{{ url('forum-reply-new', {'topic': info.topic_id}) }}" class="input__button">Reply</a>
                {% endif %}
            </div>

            <div class="forum__actions__pagination">
                {{ pag }}
            </div>
        </div>
    {% endif %}
{% endmacro %}

{% macro forum_category_entry(forum, forum_unread, forum_icon) %}
    {% from 'macros.twig' import avatar %}
    {% set forum_unread = forum_unread|default(forum.forum_unread|default(false)) ? 'unread' : 'read' %}

    {% if forum_icon is empty %}
        {% if forum.forum_icon is defined and forum.forum_icon is not empty %}
            {% set forum_icon = forum.forum_icon %}
        {% elseif forum.forum_archived is defined and forum.forum_archived %}
            {% set forum_icon = 'fas fa-archive fa-fw' %}
        {% elseif forum.forum_type is defined and forum.forum_type != constant('MSZ_FORUM_TYPE_DISCUSSION') %}
            {% if forum.forum_type == constant('MSZ_FORUM_TYPE_LINK') %}
                {% set forum_icon = 'fas fa-link fa-fw' %}
            {% elseif forum.forum_type == constant('MSZ_FORUM_TYPE_CATEGORY') %}
                {% set forum_icon = 'fas fa-folder fa-fw' %}
            {% endif %}
        {% else %}
            {% set forum_icon = 'fas fa-comments fa-fw' %}
        {% endif %}
    {% endif %}

    <div class="forum__category">
        <a href="{{ url('forum-category', {'forum': forum.forum_id}) }}" class="forum__category__link"></a>

        <div class="forum__category__container">
            <div class="forum__category__icon forum__category__icon--{{ forum_unread }}">
                <span class="{{ forum_icon }}"></span>
            </div>

            <div class="forum__category__details">
                <div class="forum__category__title">
                    {{ forum.forum_name }}
                </div>

                <div class="forum__category__description">
                    {{ forum.forum_description|nl2br }}
                </div>

                {% if forum.forum_subforums is defined and forum.forum_subforums|length > 0 %}
                    <div class="forum__category__subforums">
                        {% for subforum in forum.forum_subforums %}
                            <a href="{{ url('forum-category', {'forum': subforum.forum_id}) }}"
                                class="forum__category__subforum{% if subforum.forum_unread %} forum__category__subforum--unread{% endif %}">
                                {{ subforum.forum_name }}
                            </a>
                        {% endfor %}
                    </div>
                {% endif %}
            </div>

            {% if forum.forum_type == constant('MSZ_FORUM_TYPE_LINK') %}
                {% if forum.forum_link_clicks is not null %}
                    <div class="forum__category__stats">
                        <div class="forum__category__stat" title="Clicks">{{ forum.forum_link_clicks|number_format }}</div>
                    </div>
                {% endif %}
            {% elseif forum_may_have_children(forum.forum_type) %}
                <div class="forum__category__stats">
                    <div class="forum__category__stat" title="Topics">{{ forum.forum_count_topics|number_format }}</div>
                    <div class="forum__category__stat" title="Posts">{{ forum.forum_count_posts|number_format }}</div>
                </div>
            {% endif %}

            {% if forum_may_have_topics(forum.forum_type) or forum.forum_link_clicks is not null %}
                <div class="forum__category__activity{% if forum.forum_link_clicks is not null %} forum__category__activity--empty{% endif %}">
                    {% if forum.forum_type != constant('MSZ_FORUM_TYPE_LINK') %}
                        {% if forum.recent_topic_id is not defined %}
                            <div class="forum__category__activity__none">
                                There are no posts in this forum yet.
                            </div>
                        {% else %}
                            <div class="forum__category__activity__details">
                                <a class="forum__category__activity__post"
                                    href="{{ url('forum-post', {'post': forum.recent_post_id, 'post_fragment': 'p' ~ forum.recent_post_id}) }}">
                                    {{ forum.recent_topic_title }}
                                </a>

                                <div class="forum__category__activity__info">
                                    {% if forum.recent_post_user_id is not null %}
                                        <a href="{{ url('user-profile', {'user': forum.recent_post_user_id}) }}" class="forum__category__username"
                                            style="{{ forum.recent_post_user_colour|html_colour }}">{{ forum.recent_post_username }}</a>
                                    {% endif %}
                                    <time datetime="{{ forum.recent_post_created|date('c') }}" title="{{ forum.recent_post_created|date('r') }}">{{ forum.recent_post_created|time_format }}</time>
                                </div>
                            </div>

                            {% if forum.recent_post_user_id is not null %}
                                <a href="{{ url('user-profile', {'user': forum.recent_post_user_id}) }}" class="avatar forum__category__avatar">
                                    {{ avatar(forum.recent_post_user_id, 40, forum.recent_post_username) }}
                                </a>
                            {% endif %}
                        {% endif %}
                    {% endif %}
                </div>
            {% endif %}
        </div>
    </div>
{% endmacro %}

{% macro forum_topic_notice(icon, body) %}
    <div class="container forum__status">
        <div class="forum__status__icon">
            <div class="forum__status__icon__background"></div>
            <i class="fas fa-{{ icon }}"></i>
        </div>
        <div class="forum__status__text">
            {{ body|raw }}
        </div>
    </div>
{% endmacro %}

{% macro forum_topic_redirect(redirect) %}
    {% from _self import forum_topic_notice %}
    {% if redirect is not empty %}
        {% set body %}
            This topic redirects to <span class="forum__status__emphasis"><a href="{{ redirect.topic_redir_url }}" class="link">{{ redirect.topic_redir_url }}</a></span>.
        {% endset %}
        {{ forum_topic_notice('share', body) }}
    {% endif %}
{% endmacro %}

{% macro forum_topic_locked(locked, archived) %}
    {% from _self import forum_topic_notice %}
    {% if locked is not null or archived %}
        {% set body %}
            {% if archived %}
                This topic has been <span class="forum__status__emphasis">archived</span>.
            {% else %}
                This topic was locked
                <time class="forum__status__emphasis"
                    datetime="{{ locked|date('c') }}"
                    title="{{ locked|date('r') }}">{{ locked|time_format }}</time>.
            {% endif %}
        {% endset %}
        {{ forum_topic_notice(archived ? 'archive' : 'lock', body) }}
    {% endif %}
{% endmacro %}

{% macro forum_topic_listing(topics, title) %}
    {% from _self import forum_topic_entry %}
    {% from 'macros.twig' import container_title %}

    <div class="container forum__topics">
        {{ container_title('<i class="fas fa-comments fa-fw"></i> ' ~ title|default('Topics')) }}

        <div class="forum__topics__list">
            {% if topics|length > 0 %}
                {% for topic in topics %}
                    {{ forum_topic_entry(topic) }}
                {% endfor %}
            {% else %}
                <div class="forum__topics__empty">
                    There are no topics in this forum.
                </div>
            {% endif %}
        </div>
    </div>
{% endmacro %}

{% macro forum_topic_entry(topic, topic_icon, topic_unread) %}
    {% from 'macros.twig' import avatar %}
    {% set topic_unread = topic_unread|default(topic.topic_unread|default(false)) %}
    {% set topic_important = topic.topic_type == constant('MSZ_TOPIC_TYPE_STICKY') or topic.topic_type == constant('MSZ_TOPIC_TYPE_ANNOUNCEMENT') or topic.topic_type == constant('MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT') %}

    {% if topic_icon is null %}
        {% if topic.topic_deleted is defined and topic.topic_deleted is not null %}
            {% set topic_icon = 'fas fa-trash-alt' %}
        {% elseif topic.topic_type is defined and topic.topic_type != constant('MSZ_TOPIC_TYPE_DISCUSSION') %}
            {% if topic.topic_type == constant('MSZ_TOPIC_TYPE_ANNOUNCEMENT') or topic.topic_type == constant('MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT') %}
                {% set topic_icon = 'fas fa-bullhorn' %}
            {% elseif topic.topic_type == constant('MSZ_TOPIC_TYPE_STICKY') %}
                {% set topic_icon = 'fas fa-thumbtack' %}
            {% endif %}
        {% elseif topic.topic_locked is defined and topic.topic_locked is not null %}
            {% set topic_icon = 'fas fa-lock' %}
        {% else %}
            {% set topic_icon = (topic_unread ? 'fas' : 'far') ~ ' fa-comment' %}
        {% endif %}
    {% endif %}

    <div class="forum__topic{% if topic.topic_deleted is not null %} forum__topic--deleted{% elseif topic.topic_locked is not null and not topic_important %} forum__topic--locked{% endif %}">
        <a href="{{ url('forum-topic', {'topic': topic.topic_id}) }}" class="forum__topic__link"></a>

        <div class="forum__topic__container">
            <div class="forum__topic__icon forum__topic__icon--{{ topic_unread ? 'unread' : 'read' }}">
                <i class="{{ topic_icon }} fa-fw"></i>

                {% if topic.topic_participated %}
                    <div class="forum__topic__icon__participated" title="You have posted in this topic"></div>
                {% endif %}
            </div>

            <div class="forum__topic__details">
                <div class="forum__topic__title">
                    <span class="forum__topic__title__inner">
                        {{ topic.topic_title }}
                    </span>
                </div>

                <div class="forum__topic__info">
                    {% if topic.author_id is not null %}
                        by <a
                            href="{{ url('user-profile', {'user': topic.author_id}) }}"
                            class="forum__topic__username"
                            style="{{ topic.author_colour|html_colour }}">{{ topic.author_name }}</a>,

                    {% endif %}
                    <time datetime="{{ topic.topic_created|date('c') }}" title="{{ topic.topic_created|date('r') }}">{{ topic.topic_created|time_format }}</time>
                </div>

                {% if topic.topic_pages|default(0) > 1 %}
                    <div class="forum__topic__pagination">
                        {% set topic_pages_start_end = min(3, topic.topic_pages) %}
                        {% for i in 1..topic_pages_start_end %}
                            <a href="{{ url('forum-topic', {'topic': topic.topic_id, 'page': i}) }}" class="forum__topic__pagination__item">
                                {{ i }}
                            </a>
                        {% endfor %}
                        {% if topic.topic_pages > 3 %}
                            {% if topic.topic_pages > 6 %}
                                <div class="forum__topic__pagination__separator">
                                    <i class="fas fa-ellipsis-h"></i>
                                </div>
                            {% endif %}

                            {% set topic_pages_end_start = max(4, min(topic.topic_pages, topic.topic_pages - 2)) %}
                            {% for i in topic_pages_end_start..topic.topic_pages %}
                                <a href="{{ url('forum-topic', {'topic': topic.topic_id, 'page': i}) }}" class="forum__topic__pagination__item">
                                    {{ i }}
                                </a>
                            {% endfor %}
                        {% endif %}
                    </div>
                {% endif %}
            </div>

            <div class="forum__topic__stats">
                <div class="forum__topic__stat" title="Posts">{{ topic.topic_count_posts|number_format }}</div>
                <div class="forum__topic__stat" title="Views">{{ topic.topic_count_views|number_format }}</div>
            </div>

            <div class="forum__topic__activity">
                <div class="forum__topic__activity__details">
                    {% if topic.respondent_id is not null %}
                        <a href="{{ url('user-profile', {'user': topic.respondent_id}) }}" class="forum__topic__username"
                            style="{{ topic.respondent_colour|html_colour }}">{{ topic.respondent_name }}</a>
                    {% endif %}

                    <a class="forum__topic__activity__post"
                        href="{{ url('forum-post', {'post': topic.response_id, 'post_fragment': 'p' ~ topic.response_id}) }}">
                        <time datetime="{{ topic.response_created|date('c') }}"
                            title="{{ topic.response_created|date('r') }}">{{ topic.response_created|time_format }}</time>
                    </a>
                </div>

                {% if topic.respondent_id is not null %}
                    <a href="{{ url('user-profile', {'user': topic.respondent_id}) }}" class="forum__topic__avatar">
                        {{ avatar(topic.respondent_id, 30, topic.respondent_name) }}
                    </a>
                {% endif %}
            </div>
        </div>
    </div>
{% endmacro %}

{% macro forum_post_listing(posts, user_id, perms) %}
    {% from _self import forum_post_entry %}

    {% for post in posts %}
        {{ forum_post_entry(post, user_id, perms) }}
    {% endfor %}
{% endmacro %}

{% macro forum_post_entry(post, user_id, perms) %}
    {% from 'macros.twig' import avatar %}
    {% set is_deleted   = post.post_deleted is not null %}
    {% set can_post     = perms|perms_check(constant('MSZ_FORUM_PERM_CREATE_POST')) %}
    {% set can_edit     = perms|perms_check(constant('MSZ_FORUM_PERM_EDIT_ANY_POST')) or (
        user_id == post.poster_id
        and perms|perms_check(constant('MSZ_FORUM_PERM_EDIT_POST'))
    ) %}
    {% set can_delete   = not post.is_opening_post and (
        perms|perms_check(constant('MSZ_FORUM_PERM_DELETE_ANY_POST')) or (
            user_id == post.poster_id
            and perms|perms_check(constant('MSZ_FORUM_PERM_DELETE_POST'))
            and post.post_created|date('U') > ''|date('U') - constant('MSZ_FORUM_POST_DELETE_LIMIT')
        )
    ) %}

    <div class="container forum__post{% if is_deleted %} forum__post--deleted{% endif %}" id="p{{ post.post_id }}" style="{{ post.poster_colour|html_colour('--accent-colour') }}">
        <div class="forum__post__info">
            <div class="forum__post__info__background"></div>
            <div class="forum__post__info__content">
                {% if post.poster_id is not null %}
                    <a class="forum__post__avatar" href="{{ url('user-profile', {'user': post.poster_id}) }}">
                        {{ avatar(post.poster_id, 120, post.poster_name) }}
                    </a>

                    <a class="forum__post__username" href="{{ url('user-profile', {'user': post.poster_id}) }}">{{ post.poster_name }}</a>

                    {% if post.poster_title|length > 0 %}
                        <div class="forum__post__usertitle">{{ post.poster_title }}</div>
                    {% endif %}

                    <div class="forum__post__icons">
                        <div class="flag flag--{{ post.poster_country|lower }}" title="{{ post.poster_country|country_name }}"></div>
                        <div class="forum__post__posts-count">{{ post.poster_post_count|number_format }} posts</div>
                    </div>

                    {% if post.is_original_poster %}
                        <div class="forum__post__badge forum__post__badge--original-poster">
                            <div class="forum__post__badge__desktop">Original Poster</div>
                            <div class="forum__post__badge__mobile">OP</div>
                        </div>
                    {% endif %}

                    <div class="forum__post__joined">
                        joined <time datetime="{{ post.poster_joined|date('c') }}" title="{{ post.poster_joined|date('r') }}">{{ post.poster_joined|time_format }}</time>
                    </div>
                {% else %}
                    <div class="forum__post__username">Deleted User</div>
                {% endif %}
            </div>
        </div>

        <div class="forum__post__content">
            {% set post_link = url(post.is_opening_post ? 'forum-topic' : 'forum-post', {'topic': post.topic_id, 'post': post.post_id, 'post_fragment': 'p%d'|format(post.post_id)}) %}

            <div class="forum__post__details">
                <a class="forum__post__datetime" href="{{ post_link }}">
                    <time datetime="{{ post.post_created|date('c') }}" title="{{ post.post_created|date('r') }}">{{ post.post_created|time_format }}</time>
                    {% if post.post_edited is not null %}
                        (edited <time datetime="{{ post.post_edited|date('c') }}" title="{{ post.post_edited|date('r') }}">{{ post.post_edited|time_format }}</time>)
                    {% endif %}
                </a>

                <a class="forum__post__id" href="{{ post_link }}">
                    #{{ post.post_id }}
                </a>
            </div>

            <div class="forum__post__text{% if post.post_parse == constant('\\Misuzu\\Parsers\\Parser::MARKDOWN') %} markdown{% endif %}">
                {{ post.post_text|escape|parse_text(post.post_parse)|raw }}
            </div>

            {% if can_post or can_edit or can_delete %}
                <div class="forum__post__actions">
                    {% if is_deleted %}
                        <a href="{{ url('forum-post-restore', {'post': post.post_id}) }}" class="forum__post__action forum__post__action--restore"><i class="fas fa-magic fa-fw"></i> Restore</a>
                        <a href="{{ url('forum-post-nuke', {'post': post.post_id}) }}" class="forum__post__action forum__post__action--nuke"><i class="fas fa-radiation-alt fa-fw"></i> Permanently Delete</a>
                    {% else %}
                        {# if can_post %}
                            <a href="{{ url('forum-post-quote', {'post': post.post_id}) }}" class="forum__post__action forum__post__action--quote"><i class="fas fa-quote-left fa-fw"></i> Quote</a>
                        {% endif #}
                        {% if can_edit %}
                            <a href="{{ url('forum-post-edit', {'post': post.post_id}) }}" class="forum__post__action forum__post__action--edit"><i class="fas fa-edit fa-fw"></i> Edit</a>
                        {% endif %}
                        {% if can_delete %}
                            <a href="{{ url('forum-post-delete', {'post': post.post_id}) }}" class="forum__post__action forum__post__action--delete"><i class="far fa-trash-alt fa-fw"></i> Delete</a>
                        {% endif %}
                    {% endif %}
                </div>
            {% endif %}

            {% if post.post_display_signature and post.poster_signature_content|length > 0 %}
                <div class="forum__post__signature{% if post.poster_signature_parser == constant('\\Misuzu\\Parsers\\Parser::MARKDOWN') %} markdown{% endif %}">
                    {{ post.poster_signature_content|escape|parse_text(post.poster_signature_parser)|raw }}
                </div>
            {% endif %}
        </div>
    </div>
{% endmacro %}
