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

    {% if forums.info is defined %}
        {% set title = forums.info.name|default('Forums') %}
        {% set icon = forums.info.iconForDisplay|default('fas fa-folder fa-fw') %}
        {% set colour = forums.colour|default(null) %}
        {% set id = forums.info.id is defined ? 'f' ~ forums.info.id : '' %}
        {% set forums = forums.children %}
    {% elseif forums.children is defined %}
        {% set title = 'Forums' %}
        {% set icon = 'fas fa-folder fa-fw' %}
        {% set colour = null %}
        {% set id = '' %}
        {% set forums = forums.children %}
    {% else %}
        {% set title = title|default('Forums') %}
        {% set icon = icon|default('fas fa-folder fa-fw') %}
        {% set colour = colour|default(null) %}
        {% set id = id|default('') %}
    {% endif %}

    <div class="container forum__categories" {% if id|length > 0 %}id="{{ id }}"{% endif %}
        {% if colour is not null %}style="--accent-colour: {{ colour }}"{% endif %}>
        {{ container_title('<span class="' ~ icon ~ '"></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">
                <a href="{{ url('forum-index') }}" class="forum__header__breadcrumb">Forums</a>
                {% for category in breadcrumbs|reverse %}
                    <div class="forum__header__breadcrumb__separator">
                        <i class="fas fa-chevron-right"></i>
                    </div>
                    {% if not (omit_last_breadcrumb|default(false) and category == breadcrumbs|first) %}
                        <a href="{{ category.hasParent ? url('forum-category', {'forum': category.id}) : url('forum-category-root', {'forum': 'f' ~ category.id}) }}" class="forum__header__breadcrumb">{{ category.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 %}

    {% if info.forum_id is defined %}
        {% set forum_id = info.forum_id %}
        {% set is_archived = info.forum_archived != 0 %}
    {% else %}
        {% set forum_id = info.id %}
        {% set is_archived = info.isArchived %}
    {% endif %}

    {% set is_locked = is_archived %}
    {% set can_topic = not is_locked and perms.can_create_topic %}
    {% set pag = pagination(pagination_info, 'forum-category', {'forum': 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': 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, 'forum-topic', {'topic': info.id}) %}

    {% 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.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 %}

    {% if forum.info is defined %}
        {% set forum_id = forum.info.id %}
        {% set forum_name = forum.info.name %}
        {% set forum_desc = forum.info.description|default('') %}
        {% set forum_is_link = forum.info.isLink %}
        {% set forum_may_have_children = forum.info.mayHaveChildren %}
        {% set forum_link_clicks = forum.info.linkClicks %}
        {% set forum_count_topics = forum.info.topicsCount %}
        {% set forum_count_posts = forum.info.postsCount %}
        {% set forum_show_activity = forum.info.mayHaveTopics or forum.info.hasLinkClicks %}
        {% set forum_unread = forum.unread %}
        {% set forum_colour = forum.colour %}

        {% set forum_has_recent_post = forum.lastPost is defined %}
        {% set children = forum.children %}

        {% if forum_has_recent_post %}
            {% set forum_recent_post_id = forum.lastPost.info.id %}
            {% set forum_recent_topic_title = forum.lastPost.topicInfo.title %}
            {% set forum_recent_post_created = forum.lastPost.info.createdTime %}

            {% set forum_has_recent_post_user = forum_has_recent_post and forum.lastPost.user is defined %}
            {% if forum_has_recent_post_user %}
                {% set forum_recent_post_user_id = forum.lastPost.user.id %}
                {% set forum_recent_post_user_name = forum.lastPost.user.name %}
                {% set forum_recent_post_user_colour = '--user-colour: ' ~ forum.lastPost.colour %}
            {% endif %}
        {% endif %}

        {% if forum_icon is empty %}
            {% set forum_icon = forum.info.iconForDisplay %}
        {% endif %}
    {% else %}
        {% set forum_id = null %}
        {% set forum_name = 'Forums' %}
        {% set forum_desc = null %}
        {% set forum_is_link = false %}
        {% set forum_may_have_children = true %}
        {% set forum_count_topics = 0 %}
        {% set forum_count_posts = 0 %}
        {% set forum_show_activity = false %}
        {% set forum_unread = false %}
        {% set forum_colour = null %}
        {% set forum_has_recent_post = false %}
        {% set children = forum %}
    {% endif %}

    <div class="forum__category"{% if forum_colour is not null %} style="--accent-colour: {{ forum_colour }}"{% endif %}>
        <a href="{{ url('forum-category', {'forum': forum_id}) }}" class="forum__category__link"></a>

        <div class="forum__category__container">
            <div class="forum__category__icon forum__category__icon--{{ forum_unread ? 'unread' : 'read' }}">
                <span class="{{ forum_icon }}"></span>
            </div>

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

                {% if forum_desc is not null %}
                    <div class="forum__category__description">
                        {{ forum_desc|nl2br }}
                    </div>
                {% endif %}

                {% if children|length > 0 %}
                    <div class="forum__category__subforums">
                        {% for child in children %}
                            {% if child.info is defined %}
                                {% set child_id = child.info.id %}
                                {% set child_name = child.info.name %}
                                {% set child_unread = child.unread %}
                            {% else %}
                                {% set child_id = child.forum_id %}
                                {% set child_name = child.forum_name %}
                                {% set child_unread = child.forum_unread %}
                            {% endif %}

                            <a href="{{ url('forum-category', {'forum': child_id}) }}"
                                class="forum__category__subforum{% if child_unread %} forum__category__subforum--unread{% endif %}">
                                {{ child_name }}
                            </a>
                        {% endfor %}
                    </div>
                {% endif %}
            </div>

            {% if forum_is_link %}
                {% if forum_link_clicks is not null %}
                    <div class="forum__category__stats">
                        <div class="forum__category__stat" title="Clicks">{{ forum_link_clicks|number_format }}</div>
                    </div>
                {% endif %}
            {% elseif forum_may_have_children %}
                <div class="forum__category__stats">
                    <div class="forum__category__stat" title="Topics">{{ forum_count_topics|number_format }}</div>
                    <div class="forum__category__stat" title="Posts">{{ forum_count_posts|number_format }}</div>
                </div>
            {% endif %}

            {% if forum_show_activity %}
                <div class="forum__category__activity{% if forum_link_clicks is not null %} forum__category__activity--empty{% endif %}">
                    {% if not forum_is_link %}
                        {% if forum_has_recent_post %}
                            <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_has_recent_post_user %}
                                        <a href="{{ url('user-profile', {'user': forum_recent_post_user_id}) }}" class="forum__category__username"
                                            style="{{ forum_recent_post_user_colour }}">{{ forum_recent_post_user_name }}</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_has_recent_post_user %}
                                <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_user_name) }}
                                </a>
                            {% endif %}
                        {% else %}
                            <div class="forum__category__activity__none">
                                There are no posts in this forum yet.
                            </div>
                        {% 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.linkTarget }}" class="link">{{ redirect.linkTarget }}</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_id = topic.info.id %}
    {% set topic_title = topic.info.title %}
    {% set topic_participated = topic.participated %}
    {% set topic_count_posts = topic.info.postsCount %}
    {% set topic_count_views = topic.info.viewsCount %}
    {% set topic_created = topic.info.createdTime %}
    {% set topic_locked = topic.info.isLocked %}
    {% set topic_deleted = topic.info.isDeleted %}
    {% set topic_pages = (topic.info.postsCount / 10)|round(0, 'ceil') %}

    {% set has_topic_author = topic.user is defined %}
    {% if has_topic_author %}
        {% set topic_author_id = topic.user.id %}
        {% set topic_author_name = topic.user.name %}
        {% set topic_author_colour = '--user-colour: ' ~ topic.colour %}
    {% endif %}

    {% set has_reply = topic.lastPost is defined and topic.lastPost is not null %}
    {% if has_reply %}
        {% set reply_id = topic.lastPost.info.id %}
        {% set reply_created = topic.lastPost.info.createdTime %}

        {% set has_reply_author = topic.lastPost.user is defined %}
        {% if has_reply_author %}
            {% set reply_author_id = topic.lastPost.user.id %}
            {% set reply_author_name = topic.lastPost.user.name %}
            {% set reply_author_colour = '--user-colour: ' ~ topic.lastPost.colour %}
        {% endif %}
    {% endif %}

    {% set topic_unread = topic.unread %}
    {% set topic_important = topic.info.isImportant %}

    {% if topic_icon is null %}
        {% set topic_icon = topic.info.iconForDisplay(topic.unread) %}
    {% endif %}

    <div class="forum__topic{% if topic_deleted %} forum__topic--deleted{% elseif topic_locked and not topic_important %} forum__topic--locked{% endif %}">
        <a href="{{ url('forum-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_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_title }}
                    </span>
                </div>

                <div class="forum__topic__info">
                    {% if has_topic_author %}
                        by <a href="{{ url('user-profile', {'user': topic_author_id}) }}" class="forum__topic__username" style="{{ topic_author_colour }}">{{ topic_author_name }}</a>,
                    {% endif %}
                    <time datetime="{{ topic_created|date('c') }}" title="{{ topic_created|date('r') }}">{{ topic_created|time_format }}</time>
                </div>

                {% if topic_pages|default(0) > 1 %}
                    <div class="forum__topic__pagination">
                        {% set topic_pages_start_end = min(3, topic_pages) %}
                        {% for i in 1..topic_pages_start_end %}
                            <a href="{{ url('forum-topic', {'topic': topic_id, 'page': i}) }}" class="forum__topic__pagination__item">
                                {{ i }}
                            </a>
                        {% endfor %}
                        {% if topic_pages > 3 %}
                            {% if 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_pages, topic_pages - 2)) %}
                            {% for i in topic_pages_end_start..topic_pages %}
                                <a href="{{ url('forum-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_count_posts|number_format }}</div>
                <div class="forum__topic__stat" title="Views">{{ topic_count_views|number_format }}</div>
            </div>

            <div class="forum__topic__activity">
                <div class="forum__topic__activity__details">
                    {% if has_reply %}
                        {% if has_reply_author %}
                            <a href="{{ url('user-profile', {'user': reply_author_id}) }}" class="forum__topic__username" style="{{ reply_author_colour }}">{{ reply_author_name }}</a>
                        {% endif %}
                        <a class="forum__topic__activity__post" href="{{ url('forum-post', {'post': reply_id, 'post_fragment': 'p' ~ reply_id}) }}">
                            <time datetime="{{ reply_created|date('c') }}" title="{{ reply_created|date('r') }}">{{ reply_created|time_format }}</time>
                        </a>
                    {% endif %}
                </div>

                {% if has_reply and has_reply_author %}
                    <a href="{{ url('user-profile', {'user': reply_author_id}) }}" class="forum__topic__avatar">
                        {{ avatar(reply_author_id, 30, reply_author_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 post_id = post.info.id %}
    {% set post_created = post.info.createdTime %}
    {% set post_edited = post.info.editedTime %}
    {% set post_is_deleted = post.info.isDeleted %}
    {% set post_is_op = post.isOriginalPost %}
    {% set post_body = post.info.body|escape|parse_text(post.info.parser) %}
    {% set post_is_markdown = post.info.isBodyMarkdown %}
    {% set post_show_signature = post.info.shouldDisplaySignature %}
    {% set post_can_be_deleted = post.info.canBeDeleted %}
    {% set topic_id = post.info.topicId %}

    {% set has_author = post.user is defined %}
    {% if has_author %}
        {% set author_id = post.user.id %}
        {% set author_name = post.user.name %}
        {% set author_title = post.user.title %}
        {% set author_colour = '--accent-colour:' ~ post.colour %}
        {% set author_country = post.user.countryCode %}
        {% set author_created = post.user.createdTime %}
        {% set author_posts_count = post.postsCount %}
        {% set author_is_op = post.isOriginalPoster %}
        {% set signature_body = post.user.signatureContent|default('')|escape|parse_text(post.user.signatureParser) %}
        {% set signature_is_markdown = post.user.isSignatureBodyMarkdown %}
    {% endif %}

    {% set viewer_is_author = has_author and user_id == author_id %}
    {% set can_edit = perms.can_edit_any_post|default(false) or (viewer_is_author and perms.can_edit_post|default(false)) %}
    {% set can_delete = not post_is_op and (perms.can_delete_any_post|default(false) or (viewer_is_author and perms.can_delete_post|default(false) and post_can_be_deleted)) %}

    <div class="container forum__post{% if post_is_deleted %} forum__post--deleted{% endif %}" id="p{{ post_id }}"{% if author_colour is defined%} style="{{ author_colour }}"{% endif %}>
        <div class="forum__post__info">
            <div class="forum__post__info__background"></div>
            <div class="forum__post__info__content">
                {% if has_author %}
                    <a class="forum__post__avatar" href="{{ url('user-profile', {'user': author_id}) }}">
                        {{ avatar(author_id, 120, author_name) }}
                    </a>

                    <a class="forum__post__username" href="{{ url('user-profile', {'user': author_id}) }}">{{ author_name }}</a>

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

                    <div class="forum__post__icons">
                        <div class="flag flag--{{ author_country|lower }}" title="{{ author_country|country_name }}"></div>
                        {% if author_posts_count is not null %}<div class="forum__post__posts-count">{{ author_posts_count|number_format }} posts</div>{% endif %}
                    </div>

                    {% if author_is_op %}
                        <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="{{ author_created|date('c') }}" title="{{ author_created|date('r') }}">{{ author_created|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_op ? 'forum-topic' : 'forum-post', {'topic': topic_id, 'post': post_id, 'post_fragment': 'p%d'|format(post_id)}) %}

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

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

            <div class="forum__post__text{% if post_is_markdown %} markdown{% endif %}">
                {{ post_body|raw }}
            </div>

            {% if perms.can_create_post|default(false) or can_edit or can_delete %}
                <div class="forum__post__actions">
                    {% if post_is_deleted %}
                        <a href="{{ url('forum-post-restore', {'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_id}) }}" class="forum__post__action forum__post__action--nuke"><i class="fas fa-radiation-alt fa-fw"></i> Permanently Delete</a>
                    {% else %}
                        {# if perms.can_create_post|default(false) %}
                            <a href="{{ url('forum-post-quote', {'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_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_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_show_signature and signature_body is defined and signature_body|length > 0 %}
                <div class="forum__post__signature{% if signature_is_markdown %} markdown{% endif %}">
                    {{ signature_body|raw }}
                </div>
            {% endif %}
        </div>
    </div>
{% endmacro %}
