{% extends 'base.html.j2' %}
{% set page_title %}
    {% with embedded=True %}
        {% include 'label/person.html.j2' %}
    {% endwith %}
{% endset %}
{% block page_content %}
    {% include 'meta/person.html.j2' %}

    {% set places = person.presences | map(attribute='event.place') | reject('equalto', None) | list | unique | list %}
    {% if places | length > 0 %}
        <section id="places">
            <h2 class="js-visually-hidden">
                {% trans %}Places{% endtrans %}
                {% with url = page_resource | url ~ '#places'%}
                    {% include 'permalink.html.j2' %}
                {% endwith %}
            </h2>
            {% with map_hides_list = true %}
                {% include 'list-place.html.j2' %}
            {% endwith %}
        </section>
    {% endif %}

    {% if 'betty.plugin.wikipedia.Wikipedia' in plugins %}
        <section id="wikipedia">
            <h2 class="visually-hidden">
                {% trans %}About{% endtrans %}
                {% with url = page_resource | url ~ '#wikipedia'%}
                    {% include 'permalink.html.j2' %}
                {% endwith %}
            </h2>
            {% with entity=person %}
                {% include 'wikipedia.html.j2' %}
            {% endwith %}
        </section>
    {% endif %}

    {% set parents = person.parents | list %}
    {% set siblings = person.siblings | list %}
    {% set children = person.children | list %}

    {% set has_family =  parents | length > 0 or siblings | length > 0 or children | length > 0 %}

    {% if has_family %}
        <section id="family">
            <h2>
                {% trans %}Family{% endtrans %}
                {% with url = page_resource | url ~ '#family'%}
                    {% include 'permalink.html.j2' %}
                {% endwith %}
            </h2>
            {% if parents | length > 0 or siblings | length > 0 %}
                <div class="family">
                    <div class="family-description">
                        <div class="family-description-content">
                            <p>
                                {% if parents | length > 0 %}
                                    {% macro parent_label(parent) -%}
                                        <span typeof="foaf:Person" property="rel:childOf">{% with person=parent, person_context=person %}{% include 'label/person.html.j2' %}{% endwith %}</span>
                                    {%- endmacro %}
                                    {% if person.name and person.name.individual %}
                                        {% trans person_individual_name = person.name.individual, parent_labels = parents | map(parent_label) | join(', ') %}{{ person_individual_name }} is the child of {{ parent_labels }}.{% endtrans %}
                                    {% else %}
                                        {% trans parent_labels = parents | map(parent_label) | join(', ') %}They are the child of {{ parent_labels }}.{% endtrans %}
                                    {% endif %}
                                {% endif %}
                                {% if siblings | length > 0 %}
                                    {%- trans sibling_count = siblings | length -%}
                                        They grew up with a sibling.
                                    {%- pluralize -%}
                                        They grew up with {{ sibling_count }} siblings.
                                    {%- endtrans -%}
                                {% endif %}
                            </p>
                            {% set ancestral_affiliation_names = person | walk('parents') | map(attribute='names') | flatten | map(attribute='affiliation') | reject('none') | unique | list | sort %}
                            {% if ancestral_affiliation_names | length > 0 %}
                                <p>
                                    {%- trans -%}
                                        Ancestral names include
                                    {%- endtrans %}
                                    {{ ancestral_affiliation_names[:3] | join(', ') -}}
                                    {%- if ancestral_affiliation_names | length <= 3 -%}
                                        .
                                    {%- else %},
                                        {% with content_if_hidden = '...',
                                            content_if_shown = ancestral_affiliation_names[3:] | join(', ') ~ '.',
                                            items = ancestral_affiliation_names[3:] -%}
                                            {%- include 'show-countable.html.j2' -%}
                                        {%- endwith -%}
                                    {%- endif -%}
                                </p>
                            {% endif %}
                        </div>
                    </div>
                    {% if siblings | length > 0 %}
                        <div class="family-breakdown">
                            {% with persons=siblings, type_of='foaf:Person', property='rel:siblingOf' %}
                                {% include 'list-person.html.j2' %}
                            {% endwith %}
                        </div>
                    {% endif %}
                </div>
            {% endif %}

            {% if children | length > 0 %}
                {% macro co_parent_label(parent) -%}
                    <span typeof="foaf:Person" property="rel:spouseOf">{% with person=parent, person_context=person %}{% include 'label/person.html.j2' %}{% endwith %}</span>
                {%- endmacro %}
                {% for parents, per_parent_children in children | groupby('parents.list') %}
                    <div class="family">
                        <div class="family-description">
                            <div class="family-description-content">
                                <p>
                                    {% do parents.remove(person) %}
                                    {%- if parents | length > 0 -%}
                                        {%- if person.name and person.name.individual -%}
                                            {%- trans child_count = per_parent_children | length, person_individual_name = person.name.individual, co_parent_labels = parents | map(co_parent_label) | join(', ') -%}
                                                {{ person_individual_name }} had a child with {{ co_parent_labels }}.
                                            {%- pluralize -%}
                                                {{ person_individual_name }} had {{ child_count }} children with {{ co_parent_labels }}.
                                            {%- endtrans -%}
                                        {%- else -%}
                                            {%- trans child_count = per_parent_children | length, co_parent_labels = parents | map(co_parent_label) | join(', ') -%}
                                                They had a child with {{ co_parent_labels }}.
                                            {%- pluralize -%}
                                                They had {{ child_count }} children with {{ co_parent_labels }}.
                                            {%- endtrans -%}
                                        {%- endif -%}
                                    {%- else -%}
                                        {%- if person.name and person.name.individual -%}
                                            {%- trans child_count = per_parent_children | length, person_individual_name = person.name.individual -%}
                                                {{ person_individual_name }} had a child.
                                            {%- pluralize -%}
                                                {{ person_individual_name }} had {{ child_count }} children.
                                            {%- endtrans -%}
                                        {%- else -%}
                                            {%- trans child_count = per_parent_children | length -%}
                                                They had a child.
                                            {%- pluralize -%}
                                                They had {{ child_count }} children.
                                            {%- endtrans -%}
                                        {%- endif -%}
                                    {%- endif -%}
                                </p>
                                {% set descendant_affiliation_names = per_parent_children | map(attribute='names') | flatten | map(attribute='affiliation') | reject('none') | unique | list | sort %}
                                {% if descendant_affiliation_names | length > 0 %}
                                    <p>
                                        {%- trans -%}
                                            Descendant names include
                                        {%- endtrans %} {{ descendant_affiliation_names[:3] | join(', ') -}}
                                            {%- if descendant_affiliation_names | length <= 3 -%}
                                                .
                                            {%- else %},
                                            {%- with content_if_hidden = '...',
                                                content_if_shown = descendant_affiliation_names[3:] | join(', ') ~ '.',
                                                items = descendant_affiliation_names[3:] -%}
                                                {%- include 'show-countable.html.j2' -%}
                                            {%- endwith -%}
                                        {%- endif -%}
                                    </p>
                                {% endif %}
                            </div>
                        </div>
                        {% with persons=per_parent_children, type_of='foaf:Person', property='rel:parentOf' %}
                            <div class="family-breakdown">
                                {% include 'list-person.html.j2' %}
                            </div>
                        {% endwith %}
                    </div>
                {% endfor %}
            {% endif %}
        </section>
    {% endif %}

    <div class="featured tree" data-betty-person-id="{{ person.id }}" data-betty-people="{{ 'people.json' | url }}"></div>

    {% set events = person.presences | map(attribute='event') | rejectattr('date', 'none') | selectattr('date.comparable') | list %}
    {% if person.start or person.end %}
        {% set associated_people = [
            person.parents | map(attribute='parents') | flatten,
            person.parents,
            person.parents | map(attribute='children') | flatten,
            person.children | map(attribute='parents') | flatten,
            person.children,
            person.children | map(attribute='children') | flatten,
        ] | flatten | list %}
        {% set associated_events = [
            associated_people | map(attribute='start'),
            associated_people | map(attribute='end'),
        ] | flatten | reject('none') | rejectattr('date', 'none') | selectattr('date.comparable') | list %}
            {% if person.start is not none and person.start.date is not none and person.start.date.comparable %}
                {% set associated_events = associated_events | rejectattr('date', 'lt', person.start.date) %}
            {% endif %}
            {% if person.end is not none and person.end.date is not none and person.end.date.comparable %}
                {% set associated_events = associated_events | rejectattr('date', 'gt', person.end.date) %}
            {% endif %}
            {% set events = (events + (associated_events | list)) | unique | list %}
    {% endif %}
    {% if events | length > 0 %}
        <section id="timeline">
            <h2>
                {% trans %}Timeline{% endtrans %}
                {% with url = page_resource | url ~ '#timeline'%}
                    {% include 'permalink.html.j2' %}
                {% endwith %}
            </h2>
            {% with person_context=person %}
                {% include 'list-event.html.j2' %}
            {% endwith %}
        </section>
    {% endif %}

    {% set files = person.associated_files | list %}
    {% if files | length > 0 %}
        <section id="media">
            <h2>
                {% trans %}Media{% endtrans %}
                {% with url = page_resource | url ~ '#media'%}
                    {% include 'permalink.html.j2' %}
                {% endwith %}
            </h2>
            {% include 'list-file.html.j2' %}
        </section>
    {% endif %}
{% endblock %}