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

{% macro with_default(content='') %}
    {% set content = (caller() if caller is defined else content) | trim %}
    {%- if content -%}
        {{ content }}
    {%- else -%}
        <span class="no-value">{% trans 'Short URL' %}None{% endtrans %}</span>
    {%- endif -%}
{% endmacro %}


{% macro section_id(name) -%}
    event-settings-{{ name|replace('_', '-') }}
{%- endmacro %}


{% macro render_section(event, name, icon, edit_endpoint, edit_title, with_container=true) %}
    {% set id = section_id(name) %}
    {% if with_container %}<div class="section" id="{{ id }}">{% endif %}
        <div class="icon {{ icon }}"></div>
        <div class="text">
            {{ caller() }}
            {{ template_hook('event-settings-section-extra', event=event, section=name) }}
        </div>
        <div class="toolbar">
            <button class="i-button borderless text-color icon-only icon-edit hide-if-locked"
                    title="{{ edit_title }}"
                    data-href="{{ url_for(edit_endpoint, event) }}"
                    data-title="{{ edit_title }}"
                    data-form-container="#{{ section_id(name) }} > .text"
                    data-update='{"settings_box": "#{{ id }}",
                                  "right_header": "#event-management-header-right"}'
                    data-ajax-form></button>
        </div>
    {% if with_container %}</div>{% endif %}
{% endmacro %}


{% macro render_section_data(event, with_container=true) %}
    {% call render_section(event, 'data', 'icon-home', '.edit_data', _('Edit basic event data'),
                           with_container=with_container) %}
        <dl class="i-data-list">
            <dt>{% trans %}Title{% endtrans %}</dt>
            <dd>{{ event.title }}</dd>

            <dt>{% trans %}Description{% endtrans %}</dt>
            <dd>{{ with_default(event.description|striptags)|truncate(200) }}</dd>

            <dt>{% trans %}Short URL{% endtrans %}</dt>
            <dd>
                {% call with_default() %}
                    {% if event.url_shortcut %}
                        <a href="{{ event.short_external_url }}"
                           class="js-copy-to-clipboard"
                           title="{% trans %}Copy link to clipboard{% endtrans %}"
                           data-clipboard-text="{{ event.short_external_url }}"
                           data-clipboard-action="copy">
                            {{ event.short_external_url }}
                        </a>
                    {% endif %}
                {% endcall %}
            </dd>
        </dl>
    {% endcall %}
{% endmacro %}


{% macro render_section_dates(event, with_container=true) %}
    {% call render_section(event, 'dates', 'icon-calendar', '.edit_dates', _('Edit event dates'),
                           with_container=with_container) %}
        {% set tz = event.tzinfo %}

        <dl class="i-data-list">
            <dt>{% trans %}Date{% endtrans %}</dt>
            <dd>
                {% if event.start_dt.astimezone(tz).date() == event.end_dt.astimezone(tz).date() %}
                    {{ event.start_dt|format_date('long', timezone=tz) }}
                {% else %}
                    {{ event.start_dt|format_date(timezone=tz) }} - {{ event.end_dt|format_date(timezone=tz) }}
                {% endif %}
            </dd>

            <dt>{% trans %}Time{% endtrans %}</dt>
            <dd>{{ event.start_dt|format_time(timezone=tz) }} - {{ event.end_dt|format_time(timezone=tz) }}</dd>

            <dt>{% trans %}Timezone{% endtrans %}</dt>
            <dd>{{ event.timezone }}</dd>

            {% if event.type == 'conference' %}
                <dt>{% trans %}Screen dates{% endtrans %}</dt>
                <dd>
                    {% if event.start_dt_override or event.end_dt_override %}
                        {{ event.start_dt_display | format_datetime(timezone=tz) }} -
                        {{ event.end_dt_display | format_datetime(timezone=tz) }}
                    {% else %}
                        {{ with_default() }}
                    {% endif %}
                </dd>
            {% endif %}
    {% endcall %}
{% endmacro %}


{% macro render_section_location(event, google_wallet_location_warning, with_container=true) %}
    {% call render_section(event, 'location', 'icon-location', '.edit_location', _('Edit event location'),
                           with_container=with_container) %}
        {% block render_location_data scoped %}
            <dl class="i-data-list">
                <dt>{% trans %}Room{% endtrans %}</dt>
                <dd>{{ with_default(event.room_name) }}</dd>

                <dt>{% trans %}Venue{% endtrans %}</dt>
                <dd>{{ with_default(event.venue_name) }}</dd>

                <dt>{% trans %}Address{% endtrans %}</dt>
                <dd>{{ with_default(event.address|replace('\n', '<br>'|safe)) }}</dd>

                <dt>{% trans %}Map URL{% endtrans %}</dt>
                <dd class="break-word">{{ with_default(event.map_url) }}</dd>
            </dl>
        {% endblock %}

        {% if google_wallet_location_warning %}
            <br>
            {% call message_box('warning') %}
                {% trans %}
                    Google Wallet ticketing is enabled in this event and you specified a venue/room or address, but
                    not both. For the ticket in Google Wallet to display information about the event's physical
                    location you need to fill in both fields.
                {% endtrans %}
            {% endcall %}
        {% endif %}
    {% endcall %}

{% endmacro %}


{% macro render_section_persons(event, with_container=true) %}
    {% set edit_persons_title = _('Edit speakers') if event.type == 'lecture' else _('Edit chairpersons') %}
    {% call render_section(event, 'persons', 'icon-user', '.edit_persons', edit_persons_title,
                           with_container=with_container) %}
        <dl class="i-data-list">
            <dt>
                {%- if event.type == 'lecture' -%}
                    {% trans %}Speakers{% endtrans %}
                {%- else -%}
                    {% trans %}Chairpersons{% endtrans %}
                {%- endif -%}
            </dt>
            <dd>
                {% call with_default() %}
                    {% for event_person_link in event.person_links | sort(attribute='display_order_key') %}
                        <div>
                            <span>{{ event_person_link.display_full_name }}</span>
                            {% if event_person_link.is_submitter %}
                                <span class="badge">{% trans %}Submitter{% endtrans %}</span>
                            {% endif %}
                        </div>
                    {% endfor %}
                {% endcall %}
            </dd>
        </dl>
    {% endcall %}
{% endmacro %}


{% macro render_section_contact_info(event, with_container=true) %}
    {% call render_section(event, 'contact_info', 'icon-info', '.edit_contact_info', _('Edit contact info'),
                           with_container=with_container) %}
        <dl class="i-data-list">
            {% if event.type == 'lecture' %}
                <dt>{% trans %}Organizer{% endtrans %}</dt>
                <dd>{{ with_default(event.organizer_info|truncate(200)) }}</dd>
            {% endif %}

            {% if event.type == 'conference' %}
                <dt>{% trans %}Additional info{% endtrans %}</dt>
                <dd>{{ with_default(event.additional_info|striptags|truncate(200)) }}</dd>
            {% endif %}

            <dt>{% trans %}Contact title{% endtrans %}</dt>
            <dd>{{ with_default(event.contact_title) }}</dd>

            <dt>{% trans %}Email{% endtrans %}</dt>
            <dd>{{ with_default(event.contact_emails|join(', ')) }}</dd>

            <dt>{% trans %}Phone{% endtrans %}</dt>
            <dd>{{ with_default(event.contact_phones|join(', ')) }}</dl>
        </dl>
    {% endcall %}
{% endmacro %}


{% macro render_section_classification(event, has_reference_types, has_event_labels, with_container=true) %}
    {% set edit_cls_title = _('Edit keywords / references / label') if event.type == 'meeting' else _('Edit keywords / label') %}
    {% call render_section(event, 'classification', 'icon-tag', '.edit_classification', edit_cls_title,
                           with_container=with_container) %}
        <dl class="i-data-list">
            <dt>{% trans %}Keywords{% endtrans %}</dt>
            <dd>
                {% call with_default() %}
                    {% for keyword in event.keywords %}
                        <span class="badge">{{ keyword }}</span>
                    {% endfor %}
                {% endcall %}
            </dd>
            {% if event.type == 'meeting' and has_reference_types %}
                <dt>{% trans %}External IDs{% endtrans %}</dt>
                <dd>
                    {% call with_default() %}
                        {% set references = event.references|sort(attribute='value')|sort(attribute='reference_type.name') %}
                        {% if references %}
                            {% for reference in references -%}
                                {{ reference.value }} ({{ reference.reference_type.name }})
                                {% if not loop.last %}<br>{% endif %}
                            {% endfor %}
                        {% endif %}
                    {% endcall %}
                </dd>
            {% endif %}
            {% if has_event_labels %}
                <dt>{% trans 'Classification' %}Label{% endtrans %}</dt>
                <dd>
                    {% call with_default() %}
                        {{ event.get_label_markup('mini') }}
                    {% endcall %}
                    {% if event.is_not_happening %}
                        <p>
                            {% trans %}
                                This label indicates that the event is not happening. Reminders are disabled.
                            {% endtrans %}
                        </p>
                    {% endif %}
                </dd>
            {% endif %}
        </dl>
    {% endcall %}
{% endmacro %}


{% macro render_section_languages(event, with_container=true) %}
    {% call render_section(event, 'languages', 'icon-bubble-quote', '.edit_languages', _('Edit supported languages'),
                           with_container=with_container) %}
        <dl class="i-data-list">
            <dt>{% trans %}Default language{% endtrans %}</dt>
            <dd>{{ with_default(event.default_language) }}</dd>

            <dt>{% trans %}Enforce language{% endtrans %}</dt>
            <dd>{{ _('Yes') if event.enforce_locale else _('No') }}</dd>

            <dt>{% trans %}Additional languages{% endtrans %}</dt>
            <dd>{{ with_default(event.supported_languages|join(', ')) }}</dd>
        </dl>
        <script>
            (() => {
                function disableDefaultLocale() {
                    const defaultLocaleValue = $('#default_locale').val();
                    const enforceLocale = $('#enforce_locale').prop('checked');
                    if (!defaultLocaleValue || !enforceLocale) {
                        // Disable all when no default locale is used
                        $(`input[name=supported_locales]`).prop('disabled', true);
                        return;
                    }
                    const $inputElement = $(`input[name=supported_locales][value='${defaultLocaleValue}']`);
                    $inputElement.prop('disabled', true);
                    $('input[name=supported_locales]').not($inputElement).prop('disabled', false);
                }

                $('#event-settings-languages')
                    .on('ajaxForm:show', () => {
                        disableDefaultLocale();
                    })
                    .on('change', '#default_locale, #enforce_locale', evt => {
                        disableDefaultLocale();
                    });
            })();
        </script>
    {% endcall %}
{% endmacro %}


{% macro render_event_settings(event, has_reference_types, has_event_labels, google_wallet_location_warning, section=none, with_container=true) %}
    {% if section is none or section == 'data' %}
        {{ render_section_data(event, with_container=with_container) }}
    {% endif %}
    {% if section is none or section == 'dates' %}
        {{ render_section_dates(event, with_container=with_container) }}
    {% endif %}
    {% if section is none or section == 'location' %}
        {{ render_section_location(event, google_wallet_location_warning, with_container=with_container) }}
    {% endif %}
    {% if section is none or section == 'persons' %}
        {{ render_section_persons(event, with_container=with_container) }}
    {% endif %}
    {% if section is none or section == 'contact_info' %}
        {{ render_section_contact_info(event, with_container=with_container) }}
    {% endif %}
    {% if section is none or section == 'classification' %}
        {{ render_section_classification(event, has_reference_types, has_event_labels, with_container=with_container) }}
    {% endif %}
    {% if section is none or section == 'languages' %}
        {{ render_section_languages(event, with_container=with_container) }}
    {% endif %}
{% endmacro %}
