{% macro _change_type_confirmation(type) %}
    {% trans -%}
        Changing the event type to {{ type }} will change the look of the event page
        and may disable some features not available for the new event type.
    {%- endtrans %}
{% endmacro %}


{% macro _render_change_event_type(event, event_types) %}
    <button class="i-button borderless icon-wrench arrow js-dropdown hide-if-locked" data-toggle="dropdown">
        {%- trans %}Change type{% endtrans -%}
    </button>
    <ul class="i-dropdown">
        {% for type, title in event_types %}
            <li>
                {% if type == event.type_.name %}
                    <a class="disabled" title="{% trans %}This is the event's current type.{% endtrans %}">
                        {{ title }}
                    </a>
                {% else %}
                    <a data-href="{{ url_for('event_management.change_type', event) }}"
                       data-method="POST"
                       data-params="{{ {'type': type} | tojson | forceescape }}"
                       data-title="{% trans %}Confirm type change{% endtrans %}"
                       data-confirm="{{ _change_type_confirmation(title) }}"
                       data-ajax>
                        {{ title }}
                    </a>
                {% endif %}
            </li>
        {% endfor %}
    </ul>
{% endmacro %}


{% macro _render_lock_event(event) %}
    {% if not event.can_lock(session.user) %}
        {% if event.is_locked %}
            <button class="i-button borderless icon-lock-center disabled"
                    title="{% trans %}Only the event creator and category managers may unlock an event.{% endtrans %}">
                {%- trans %}Unlock{% endtrans -%}
            </button>
        {% else %}
            <button class="i-button borderless icon-lock-center disabled"
                    title="{% trans %}Only the event creator and category managers may lock an event.{% endtrans %}">
                {%- trans %}Lock{% endtrans -%}
            </button>
        {% endif %}
    {% else %}
        {% if event.is_locked %}
            <button class="i-button borderless icon-lock-center warning"
                    title="{% trans %}Unlock event{% endtrans %}"
                    data-qtip-style="warning"
                    data-href="{{ url_for('event_management.unlock', event) }}"
                    data-method="POST"
                    data-reload-after
                    data-ajax>
                {%- trans %}Unlock{% endtrans -%}
            </button>
        {% else %}
            <button class="i-button borderless icon-lock-center warning"
                    title="{% trans %}Lock event{% endtrans %}"
                    data-qtip-style="warning"
                    data-href="{{ url_for('event_management.lock', event) }}"
                    data-reload-after
                    data-ajax-dialog>
                {%- trans %}Lock{% endtrans -%}
            </button>
        {% endif %}
    {% endif %}
{% endmacro %}


{% macro _render_manage_series(event) %}
    <span id="event-action-manage-series-container"
          data-event-id="{{ event.id }}"
          data-category-id="{{ event.category_id }}"
          data-timezone="{{ event.timezone }}"
          {% if event.series %}data-series-id="{{ event.series.id }}"{% endif %}></span>
{% endmacro %}


{% macro _render_move_event(event) %}
    {% if not event.is_unlisted %}
        <span class="hide-if-locked"
              id="event-action-move-container"
              data-event-id="{{ event.id }}"
              data-category-id="{{ event.category.id }}"
              {{ 'data-pending-request' if event.pending_move_request }}></span>
    {% endif %}
{% endmacro %}


{% macro _render_delete_event(event) %}
    <button class="i-button borderless icon-remove danger hide-if-locked"
            title="{% trans %}Delete event{% endtrans %}"
            data-qtip-style="danger"
            data-href="{{ url_for('event_management.delete', event) }}"
            data-ajax-dialog>
        {%- trans %}Delete{% endtrans -%}
    </button>
{% endmacro %}


{% macro _render_import_event(event) %}
    <button class="i-button borderless icon-import hide-if-locked"
            title="{% trans %}Import from event{% endtrans %}"
            data-title="{% trans %}Import from other event{% endtrans %}"
            data-href="{{ url_for('event_management.import', event) }}"
            data-ajax-dialog>
        {%- trans %}Import{% endtrans -%}
    </button>
{% endmacro %}


{% macro render_action_menu(event, event_types) %}
    {% set clones = event.clones|rejectattr('is_deleted')|list %}

    <div class="action-menu">
        <div class="toolbar">
            {% if event.can_manage(session.user) %}
                {% if event.is_unlisted %}
                    <span class="hide-if-locked group"
                          id="event-action-publish-container"
                          data-event-id="{{ event.id }}"
                          {{ 'data-pending-request' if event.pending_move_request }}></span>
                   </a>
                {% endif %}

                {% block event_clone_actions scoped %}
                    <div class="group">
                        <button class="i-button icon-copy" title="{% trans %}Clone event{% endtrans %}"
                                data-href="{{ url_for('event_management.clone', event) }}"
                                data-title="{% trans %}Clone Event{% endtrans %}"
                                data-ajax-dialog>
                            {% trans %}Clone{% endtrans %}
                        </button>
                        {% if clones %}
                            <a id="event-action-menu-clones-target" class="i-button label clickable arrow">
                                {{- clones|length -}}
                            </a>
                        {% endif %}
                    </div>
                {% endblock %}

                <button id="event-action-menu-actions-target" class="i-button icon-settings i-button arrow"
                   title="{% trans %}Event actions{% endtrans %}"></button>

                <div id="event-action-menu-actions">
                    <div class="action-menu-buttons {{ 'event-locked' if event.is_locked }}">
                        {% block event_actions scoped %}
                            {{ _render_move_event(event) }}
                            {{ _render_import_event(event) }}
                            {% if not event.is_unlisted %}{{ _render_manage_series(event) }}{% endif %}
                            {{ _render_lock_event(event) }}
                            {{ _render_delete_event(event) }}
                            {{ _render_change_event_type(event, event_types) }}
                        {% endblock %}
                    </div>
                </div>

                <div id="event-action-menu-clones">
                    {% trans %}Events cloned from the current event:{% endtrans %}
                    <ul>
                        {% for clone in clones %}
                            <li>
                                <a href="{{ url_for('event_management.settings', clone) }}"
                                   title="{{ clone.title }}" data-qtip-position="left">
                                    {{ clone.start_dt | format_date(timezone=clone.tzinfo) }}
                                </a>
                            </li>
                        {% endfor %}
                    </ul>
                </div>
            {% endif %}
        </div>
    </div>

    <script>
        setupEventManagementActionMenu();
    </script>
{% endmacro %}
