{% from 'events/_affiliation.html' import render_affiliation %}

{% macro _render_location_tip(item) -%}
    {% set show_url = item.room and item.room.map_url %}
    <div class="location-info-tip">
        {% if item.room_name -%}
            <h3>{{ item.room_name }}</h3>
        {% endif %}
        {% if item.venue_name -%}
            <h4>{{ item.venue_name }}</h4>
        {% endif %}
        {% if item.address %}
            <div class="location-address">
                {{- item.address }}
            </div>
        {%- endif %}
        {% if item.room and item.room.capacity %}
            <div class="icon-user">
                {{ item.room.capacity }}
            </div>
        {% endif %}
        {% if show_url %}
            <a href="{{ item.room.map_url }}" class="map-link">
                {% trans %}Show room on map{% endtrans %}
            </a>
        {% endif %}
    </div>
{%- endmacro %}

{% macro render_location(item, parent=none, class='item-location', header=false) -%}
    {% set show_venue = not parent or (parent.venue_name != item.venue_name) %}
    {% set show_room = item.room_name != '' %}
    {% block render_location scoped %}
        {% if show_venue or show_room %}
            <span class="location-info {{ class }}"
                data-qtip-html=".qtip-hidden-content"
                data-qtip-opts='{"hide": {"fixed": true, "delay": 500}}'>
                <i class="icon-location {{ 'header-data' if header }}"></i>
                <span class="text">
                    {%- if show_venue and show_room -%}
                        {{ item.room_name }}{% if item.venue_name %} ({{ item.venue_name }}){% endif %}
                    {%- elif show_venue -%}
                        {{ item.venue_name }}
                    {%- else -%}
                        {{ item.room_name }}
                    {%- endif -%}
                </span>
            </span>
            <span class="qtip-hidden-content">
                {{ _render_location_tip(item) }}
            </span>
        {% endif %}
    {% endblock %}
{%- endmacro %}

{% macro render_user_data(user_data, show_title=true) -%}
    {% if user_data.title and show_title -%}
        <span class="speaker-title">
            {{- user_data.title -}}
        </span>
    {%- endif %}
    <span>{{ user_data.display_full_name }}</span>
    {%- if user_data.affiliation %}
        <span class="affiliation">
            <span class="text">({{ render_affiliation(user_data) }})</span>{#--#}
        </span>
    {%- endif %}
{%- endmacro %}

{% macro render_users(user_list, span_class='', title=true, separator=', ') -%}
    {%- for link in user_list -%}
        {%- if caller is defined -%}
            {{- caller(link) -}}
        {%- else -%}
            <span class="{{ span_class }}">
                {{- render_user_data(link, show_title=title) -}}
            </span>
        {%- endif -%}
        {%- if not loop.last -%}
            {{- separator -}}
        {%- endif -%}
    {%- endfor -%}
{%- endmacro %}

{% macro render_speakers(contribution) %}
    <section>
        <div class="header">
            <div class="header-row">
                <h3>
                    {%- trans count=contribution.speakers|length -%}
                        Speaker
                    {%- pluralize -%}
                        Speakers
                    {%- endtrans -%}
                </h3>
            </div>
        </div>
        <div class="speaker-list">
            {{ render_users(contribution.speakers|sort(attribute='display_order_key'),
                            span_class='speaker-item icon-user', separator='', title=contribution.event.show_titles) }}
        </div>
    </section>
{% endmacro %}

{% macro render_datetime(prop_name, dt, timezone, format='medium', show_weekday=false) -%}
    <time itemprop="{{ prop_name }}" datetime="{{ dt.isoformat() }}">
        {% if show_weekday %}
            {{ dt | format_datetime(format='EEEE', timezone=timezone) }}
        {% endif %}
        {{ dt | format_datetime(format=format, timezone=timezone) }}
    </time>
{%- endmacro %}

{% macro render_event_time(event, timezone) -%}
    <i class="icon-calendar header-data" title="{% trans %}Start/end date/time of the event{% endtrans %}"></i>
    <span class="event-time-data">
        {% if event.start_dt.astimezone(timezone).date() == event.end_dt.astimezone(timezone).date() %}
            {{ render_datetime('startDate', event.start_dt, timezone=timezone, show_weekday=true) }}
            &rarr;
            <time itemprop="endDate" datetime="{{ event.end_dt.isoformat() }}">
                {{ event.end_dt | format_time(format='medium', timezone=timezone) }}
            </time>
        {% else %}
            {%- set show_weekday = event.type == 'lecture' -%}
            {{ render_datetime('startDate', event.start_dt, timezone=timezone, show_weekday=show_weekday) }}
            &rarr;
            {{ render_datetime('endDate', event.end_dt, timezone=timezone, show_weekday=show_weekday) }}
        {% endif %}
        <span class="timezone">{{ timezone }}</span>
    </span>
{%- endmacro %}
