{% load i18n static %}
{% load forum_permission_tags %}

{% get_permission 'can_add_post' topic request.user as user_can_add_post %}
{% get_permission 'can_subscribe_to_topic' topic request.user as user_can_subscribe_to_topic %}
{% get_permission 'can_unsubscribe_from_topic' topic request.user as user_can_unsubscribe_from_topic %}
{% get_permission 'can_lock_topics' topic.forum request.user as user_can_lock_topics %}
{% get_permission 'can_move_topics' topic.forum request.user as user_can_move_topics %}
{% get_permission 'can_delete_topics' topic.forum request.user as user_can_delete_topics %}
{% get_permission 'can_update_topics_to_normal_topics' topic.forum request.user as user_can_change_to_normal_topics %}
{% get_permission 'can_update_topics_to_sticky_topics' topic.forum request.user as user_can_change_to_sticky_topics %}
{% get_permission 'can_update_topics_to_announces' topic.forum request.user as user_can_change_to_announces %}

{% if user_can_add_post %}
  <a href="{% url 'forum_conversation:post_create' forum.slug forum.pk topic.slug topic.pk %}"
     class="py-3 px-4 text-sm uppercase rounded rounded-lg border border-orange text-orange"><img
    class="inline-block mr-2" src="{% static 'img/icons/icon_button-talk-bubble.svg' %}"/>{% trans "Post reply" %}</a>
{% endif %}
{% if user_can_subscribe_to_topic %}
  <a href="{% url 'forum_member:topic_subscribe' topic.pk %}"
     class="py-3 px-4 text-sm uppercase rounded rounded-lg border border-slate text-orange"><img
    class="inline-block mr-2" src="{% static 'img/icons/icon_button-checkmark.svg' %}"/>{% trans "Subscribe" %}</a>
{% elif user_can_unsubscribe_from_topic %}
  <a href="{% url 'forum_member:topic_unsubscribe' topic.pk %}" class="py-3 px-4 text-sm uppercase rounded rounded-lg border border-slate text-orange"><img
    class="inline-block mr-2" src="{% static 'img/icons/icon_button-checkmark.svg' %}"/>{% trans "Unsubscribe" %}</a>
{% endif %}
{% if user_can_lock_topics and not topic.is_locked or user_can_move_topics or user_can_delete_topics %}
  <div class="inline-block relative" x-data="{ menuOpen: false }">
    <button id="id_dropdown_moderation_menu_button" class="py-3 px-4 rounded rounded-lg border border-slate text-orange"
            type="button"
            data-toggle="dropdown"
            aria-haspopup="true"
            aria-expanded="false"
            @click="menuOpen = !menuOpen"
    >
      <img src="{% static 'img/icons/icon_button-gavel.svg' %}" class="inline-block mr-2" /><i class="fas fa-caret-down"></i>
    </button>

    <!-- Moderator menu -->
    <div x-show="menuOpen"
         class="absolute inset-x-0 top-14 p-3 space-y-2 bg-black rounded border border-slate text-orange w-[200px]"
         aria-labelledby="id_dropdown_moderation_menu_button"
         @click.away="menuOpen = !menuOpen"
    >
      {% if user_can_lock_topics and not topic.is_locked %}
        <a href="{% url 'forum_moderation:topic_lock' slug=topic.slug pk=topic.pk %}"
           class="block">{% trans "Lock topic" %}</a>{% endif %}
      {% if user_can_lock_topics and topic.is_locked %}
        <a href="{% url 'forum_moderation:topic_unlock' slug=topic.slug pk=topic.pk %}"
           class="block">{% trans "Unlock topic" %}</a>{% endif %}
      {% if user_can_delete_topics %}<a href="{% url 'forum_moderation:topic_delete' slug=topic.slug pk=topic.pk %}"
                                        class="block">{% trans "Delete topic" %}</a>{% endif %}
      {% if user_can_move_topics %}<a href="{% url 'forum_moderation:topic_move' slug=topic.slug pk=topic.pk %}"
                                      class="block">{% trans "Move topic" %}</a>{% endif %}
      {% if topic.is_topic %}
        {% if user_can_change_to_sticky_topics %}
          <a href="{% url 'forum_moderation:topic_update_to_sticky' slug=topic.slug pk=topic.pk %}"
             class="block">{% trans "Change to sticky topic" %}</a>{% endif %}
        {% if user_can_change_to_announces %}
          <a href="{% url 'forum_moderation:topic_update_to_announce' slug=topic.slug pk=topic.pk %}"
             class="block">{% trans "Change to announce" %}</a>{% endif %}
      {% elif topic.is_sticky %}
        {% if user_can_change_to_normal_topics %}
          <a href="{% url 'forum_moderation:topic_update_to_post' slug=topic.slug pk=topic.pk %}"
             class="block">{% trans "Change to standard topic" %}</a>{% endif %}
        {% if user_can_change_to_announces %}
          <a href="{% url 'forum_moderation:topic_update_to_announce' slug=topic.slug pk=topic.pk %}"
             class="block">{% trans "Change to announce" %}</a>{% endif %}
      {% elif topic.is_announce %}
        {% if user_can_change_to_normal_topics %}
          <a href="{% url 'forum_moderation:topic_update_to_post' slug=topic.slug pk=topic.pk %}"
             class="block">{% trans "Change to standard topic" %}</a>{% endif %}
        {% if user_can_change_to_sticky_topics %}
          <a href="{% url 'forum_moderation:topic_update_to_sticky' slug=topic.slug pk=topic.pk %}"
             class="block">{% trans "Change to sticky topic" %}</a>{% endif %}
      {% endif %}
    </div>
  </div>
{% endif %}
