{% from 'message_box.html' import message_box %}

{% macro _hidden_events_block(class, category, events_by_month, format_event_date, is_recent, happening_now,
                             before=none, after=none, message_first=true) %}
    {% set message=caller() %}
    {% macro render_message() %}
        {% call message_box('info', icon=false) %}
            <div class="content smaller">
                <span class="js-show-message">
                    {{ message }}
                    <button class="js-toggle-list toggle-hidden-events">
                        {% trans %}Show{% endtrans %}
                    </button>
                    <span class="icon-spinner js-spinner"></span>
                </span>
                <span class="js-hide-message">
                    {{ message }}
                    <button class="js-toggle-list toggle-hidden-events">
                        {% trans %}Hide{% endtrans %}
                    </button>
                </span>
            </div>
        {% endcall %}
    {% endmacro %}

    {% macro render_list() %}
        <div class="events"
             data-event-list-url="{{ url_for('.event_list', category) }}"
             {% if after %}data-event-list-after="{{ after }}"{% endif %}
             {% if before %}data-event-list-before="{{ before }}"{% endif %}>
            {%- if events_by_month -%}
                {{ event_list_block(events_by_month, format_event_date, is_recent, happening_now) }}
            {%- endif -%}
        </div>
    {% endmacro %}

    <div class="{{ class }}">
        {% if message_first %}
            {{ render_message() }}
            {{ render_list() }}
        {% else %}
            {{ render_list() }}
            {{ render_message() }}
        {% endif %}
    </div>
{% endmacro %}

{% macro event_list(events_by_month, format_event_date, is_recent, happening_now, category, future_event_count,
                    past_event_count, future_threshold, past_threshold, future_events_by_month=[],
                    past_events_by_month=[], show_future_events=false, show_past_events=false,
                    has_hidden_events=false, is_flat=false, request_params={}) %}
    {#- request_params parameter provided for template extensions -#}
    {% if has_hidden_events %}
        {% call message_box('highlight', icon=true) %}
            <div class="content smaller">
                <span class="js-show-message">
                    {% trans %}Some events in the list below have been hidden.{% endtrans %}
                </span>
            </div>
        {% endcall %}
    {% endif %}

    <div class="event-list"
         data-show-future-events-url="{{ url_for('.show_future_events', category) }}"
         data-show-past-events-url="{{ url_for('.show_past_events', category) }}">
        {% if future_event_count %}
            {% call _hidden_events_block('future-events', category, future_events_by_month, format_event_date,
                                        is_recent, happening_now, after=future_threshold) %}
                {% trans count=future_event_count -%}
                    There is one event in the future.
                {%- pluralize -%}
                    There are {{ count }} events in the future.
                {%- endtrans %}
            {% endcall %}
        {% endif %}
        <div>
            {{ event_list_block(events_by_month, format_event_date, is_recent, happening_now) }}
        </div>
        {% if past_event_count %}
            {% call _hidden_events_block('past-events', category, past_events_by_month, format_event_date, is_recent,
                                        happening_now, before=past_threshold, message_first=false) %}
                {% trans count=past_event_count -%}
                    There is one event in the past.
                {%- pluralize -%}
                    There are {{ count }} events in the past.
                {%- endtrans %}
            {% endcall %}
        {% endif %}
    </div>
    <script>
        setupCategoryDisplayEventList({{ (show_future_events and not future_events_by_month) | tojson }},
                                      {{ (show_past_events and not past_events_by_month) | tojson }},
                                      {{ is_flat | tojson }},
                                      {{ request_params | tojson }});
    </script>
{% endmacro %}

{% macro event_list_block(events_by_month, format_event_date, is_recent, happening_now) %}
    {% for month in events_by_month %}
        <h3 class="{% if month.is_current %}current-month{% endif %}">
            <span>{{ month.name }}</span>
        </h3>
        <ul>
            {% for event in month.events %}
                {% set is_lecture = (event.type == 'lecture') %}
                <li>
                    <span class="event-icons">
                        <ind-with-tooltip>
                            <a class="icon-calendar" href="{{ url_for('events.export_event_ical', event) }}" aria-label="{% trans title=event.title|e %}Export {{ title }} to calendar{% endtrans %}">
                                <span data-tip-content>{% trans %}Export{% endtrans %}</span>
                            </a>
                        </ind-with-tooltip>
                        {% if session.user and event in session.user.favorite_events %}
                            <ind-with-tooltip>
                                <i class="icon-star"></i>
                                <span data-tip-content>{% trans %}You have favorited this event.{% endtrans %}</span>
                            </ind-with-tooltip>
                        {% endif %}
                    </span>
                    <span class="list-name">
                        <span class="date {% if happening_now(event) %}today{% endif %}">
                            {{ format_event_date(event) }}
                        </span>
                        <div class="event-title">
                            <a href="{{ event.url }}">
                                {{ event.get_verbose_title(show_speakers=is_lecture, show_series_pos=is_lecture) | striptags }}
                            </a>
                            <span class="protected">
                                {% if event.visibility == 0 %}
                                    <span class="ui label mini grey"
                                          title="{% trans %}This event is hidden.{% endtrans %}">
                                        {%- trans %}hidden{% endtrans -%}
                                    </span>
                                {% endif %}
                                {% if event.is_self_protected %}
                                    <ind-with-tooltip>
                                        <span class="icon-shield" data-type="restricted"></span>
                                        <span data-tip-content>{% trans %}Protected{% endtrans %}</span>
                                    </ind-with-tooltip>
                                {% endif %}

                                {% if is_recent(event.created_dt) %}
                                    <span class="ui label mini blue"
                                          title="{% trans %}This event is new.{% endtrans %}">
                                        {%- trans %}new{% endtrans -%}
                                    </span>
                                {% endif %}
                                {{ template_hook('event-status-labels', event=event) }}
                            </span>
                            {% if event.label %}
                                <span class="ui label basic mini {{ event.label.color }}"
                                      title="{{ event.label_message }}">
                                    {{- event.label.title -}}
                                </span>
                            {% endif %}
                        </div>
                    </span>
                </li>
            {% endfor %}
        </ul>
    {% endfor %}
{% endmacro %}
