{% import "widgets/user.html" as widget_user %}
{% import "widgets/attachments.html" as widget_attachments %}

{# Post actions: this widget expands to a context menu with actions controlling
   a post, supporting different types of posts. #}
{% macro post_actions(post) %}
    {# TODO (Guest edit): determine permissions in post_actions widget #}

    {% set auth = current_user.is_authenticated %}
    {% set can_edit    = auth and current_user.can_edit_post(post) %}
    {% set can_delete  = auth and current_user.can_delete_post(post) %}
    {% set can_punish  = auth and current_user.can_punish_post(post) %}
    {% set can_topcomm = auth and current_user.can_set_topcomment(post) %}
    {% set can_move    = auth and current_user.can_edit_post(post) and post.type == "comment" %}

    {% if post.type == "topic" %}
        {% set suffix = " le sujet" %}
    {% elif post.type == "program" %}
        {% set suffix = " le programme" %}
    {% endif %}

    {% if can_edit or can_move or can_delete or can_punish or can_topcomm %}
        <details>
            <summary><b>⋮</b></summary>
            <div class='context-menu'>
                {% if can_edit %}
                    <a href="{{ url_for('edit_post', postid=post.id, r=request.path) }}">Modifier{{ suffix }}</a>
                {% endif %}

                {% if can_move %}
                    <a href="{{ url_for('move_post', postid=post.id) }}">Déplacer</a>
                {% endif %}

                {% if can_punish %}
                    <a href="{{ url_for('delete_post', postid=post.id, penalty=False, csrf_token=csrf_token()) }}" onclick="return confirm('Le post sera supprimé.')">Supprimer{{ suffix }} (normal)</a>
                    <a href="{{ url_for('delete_post', postid=post.id, penalty=True, csrf_token=csrf_token()) }}" onclick="return confirm('Le post sera supprimé avec pénalité d\'XP.')">Supprimer{{ suffix }} (pénalité)</a>
                {% elif can_delete %}
                    <a href="{{ url_for('delete_post', postid=post.id, penalty=False, csrf_token=csrf_token()) }}" onclick="return confirm('Le post sera supprimé !')">Supprimer{{ suffix }}</a>
                {% endif %}

                {% if can_topcomm %}
                    <a href="{{ url_for('set_post_topcomment', postid=post.id, csrf_token=csrf_token()) }}">Utiliser comme en-tête</a>
                {% endif %}
            </div>
        </details>
    {% endif %}
{% endmacro %}

{# Thread widget: this widget expands to a table that shows a list of comments
   from a thread, along with message controls.

   comments: List of comments to render
   top_comment: Thread's top comment (will be elided if encountered) #}

{% macro thread(comments, top_comment, owner=None) %}
<table class="thread">
{% for c in comments %}
    {% if c != top_comment %}
    <tr id="{{ c.id }}">
        <td class="author">{{ widget_user.profile(c.author) }}</td>
        <td class="message">
            <div class="info">
                <div>Posté le <a href="{{ request.path }}#{{ c.id }}">{{ c.date_created | dyndate }}</a></div>
                {% if (c.date_modified - c.date_created).seconds > 120 %}
                    <div>Modifié le <a href="{{ request.path }}#{{ c.id }}">{{ c.date_modified | dyndate }}</a></div>
                {% endif %}
                {{ post_actions(c) }}
            </div>

            {{ c.text|md(c.id) }}
            {{ widget_attachments.attachments(c) }}

            {% if c.author.signature %}
              <hr class="signature">
              {{ c.author.signature|md }}
            {% endif %}
        </td>
    </tr>
    {% elif loop.index0 != 0 %}
    <tr class="topcomment-placeholder">
        <td></td>
        <td><div>Le commentaire à cet endroit est actuellement utilisé comme en-tête.</div></td>
    </tr>
    {% endif %}
{% endfor %}
</table>
{% endmacro %}

{# Thread leader widget: this widget expands to a single-message thread which
   can show more text when called. This is intended for programs and similar
   objects which display metadata before description and comments.

   leader: Posts's top comment (actual rendering is delegated to caller) #}

{% macro thread_leader(leader) %}
<table class="thread topcomment">
    {# Empty line to get normal background (instead of alternate one) #}
    <tr></tr>
    <tr id="{{ leader.id }}">
        <td class="author">{{ widget_user.profile(leader.author) }}</td>
        <td class="message">{{ caller() }}</td>
    </tr>
</table>
{% endmacro %}
