{#
    WARNING: this file is in use in both ActivityBundle and CalendarBundle.

    Take care when editing this file.

    Maybe should we think about abstracting this file a bit more ? Moving it to PersonBundle ?
#}
{% if context == 'calendar_accompanyingCourse' or context == 'calendar_person' %}
    {% import "@ChillCalendar/_invite.html.twig" as invite %}
{% endif %}

{% macro href(pathname, key, value) %}
    {% set parms = { (key): value } %}
    {{ path(pathname, parms) }}
{% endmacro %}

{% macro insert_onthefly(type, entity, parent = null) %}
    {% include '@ChillMain/OnTheFly/_insert_vue_onthefly.html.twig' with {
        action: 'show', displayBadge: true,
        targetEntity: { name: type, id: entity.id },
        buttonText: entity|chill_entity_render_string,
        isDead: entity.deathdate is defined and entity.deathdate is not null,
        parent: parent
    } %}
{% endmacro %}

{% macro computeWidth(nbBlocks) %}
    {{ 'flex-basis: ' ~ (100 / nbBlocks)|round(1) ~ '%;' }}
{% endmacro %}

{% set blocks = [] %}
{% if context == 'calendar_accompanyingCourse' or context == 'calendar_person' or entity.activityType.personsVisible %}
    {% if context == 'person' %}
        {% set blocks = blocks|merge([{
            'title': 'Others persons'|trans,
            'items': entity.persons,
            'type': 'person',
            'path' : 'chill_person_view',
            'key'  : 'person_id'
        }]) %}
    {% else %}
        {% set blocks = blocks|merge([{
            'title': 'Persons in accompanying course'|trans,
            'items': entity.personsAssociated,
            'type': 'person',
            'path' : 'chill_person_view',
            'key'  : 'person_id'
        },{
            'title': 'Third persons'|trans,
            'items': entity.personsNotAssociated,
            'type': 'person',
            'path' : 'chill_person_view',
            'key'  : 'person_id',
        }]) %}
    {% endif %}
{% endif %}
{% if context == 'calendar_accompanyingCourse' or context == 'calendar_person' or entity.activityType.thirdPartiesVisible %}
    {% set blocks = blocks|merge([{
        'title': 'Third parties'|trans,
        'items': entity.thirdParties,
        'type': 'thirdparty',
        'path' : 'chill_crud_3party_3party_view',
        'key'  : 'id',
    }]) %}
{% endif %}
{% if context == 'calendar_accompanyingCourse' or context == 'calendar_person' or entity.activityType.usersVisible %}
    {% set blocks = blocks|merge([{
        'title': 'Users concerned'|trans,
        'items': entity.users,
        'type': 'user',
        'key'  : 'id',
    }]) %}
{% endif %}

{% if (render == 'bloc') %}
<div class="{{ context }} flex-bloc concerned-groups">
    {% for bloc in blocks %}

        <div class="item-bloc" style="{{ _self.computeWidth(loop.length) }}">
            <div class="item-row">
                <div class="item-col">
                    <h4>{{ bloc.title }}</h4>
                </div>
                <div class="item-col">
                    <ul class="list-content">
                        {% for item in bloc.items %}
                            <li>
                                {% if bloc.type == 'user' %}
                                    <span class="badge-user">
                                        {{ item|chill_entity_render_box({'render': 'raw', 'addAltNames': false, 'at_date': entity.date }) }}
                                    </span>
                                {% else %}
                                    {{ _self.insert_onthefly(bloc.type, item) }}
                                {% endif %}
                            </li>
                        {% endfor %}
                    </ul>
                </div>
            </div>
        </div>

    {% endfor %}
</div>
{% endif %}

{% if (render == 'row') %}
<div class="concerned-groups">
    {% for bloc in blocks %}
    <div class="group">
        {% if bloc.items|length > 0 %}
            <h4>{{ bloc.title }}</h4>
            <ul class="list-content">
                {% for item in bloc.items %}
                <li>
                    {% if bloc.type == 'user' %}
                        <span class="badge-user">
                            {{ item|chill_entity_render_box({'render': 'raw', 'addAltNames': false, 'at_date': entity.date }) }}
                        </span>
                    {% else %}
                        {{ _self.insert_onthefly(bloc.type, item) }}
                    {% endif %}
                </li>
                {% endfor %}
            </ul>
        {% endif %}
    </div>
    {% endfor %}
</div>
{% endif %}

{% if (render == 'wrap-list') %}
<div class="concerned-groups wrap-list">
    {% for bloc in blocks %}
        <div class="wl-row">
            {% if bloc.items|length > 0 %}
                <div class="wl-col title">
                    <h3>{{ bloc.title }}</h3>
                </div>
                <div class="wl-col list">
                    {% for item in bloc.items %}

                        <span class="wl-item">
                            {% if bloc.type == 'user' %}
                                <span class="badge-user">
                                    {{ item|chill_entity_render_box({'render': 'raw', 'addAltNames': false, 'at_date': entity.date }) }}
                                    {%- if context == 'calendar_accompanyingCourse' or context == 'calendar_person' %}
                                        {% set invite = entity.inviteForUser(item) %}
                                        {% if invite is not null %}
                                        {{ invite.invite_span(invite) }}
                                        {% endif %}
                                    {%- endif -%}
                                </span>
                            {% else %}
                                {{ _self.insert_onthefly(bloc.type, item) }}
                            {% endif %}
                        </span>

                    {% endfor %}
                </div>
            {% endif %}
        </div>
    {% endfor %}
</div>
{% endif %}
