{% extends 'entity/page.html.j2' %}
{% set person = person | default(entity) %}
{% set page_title %}
    {% with embedded=True %}
        {% include 'entity/label--person.html.j2' %}
    {% endwith %}
{% endset %}
{% block page_content %}
    {% include 'entity/meta--person.html.j2' %}

    {% set places = person.presences | select('public') | selectattr('event', 'public') | map(attribute='event.place') | reject('none') | 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 'entity/list--place.html.j2' %}
            {% endwith %}
        </section>
    {% endif %}

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

    {% with notes = person.notes %}
        {% include 'section-notes.html.j2' %}
    {% endwith %}

    {% 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 %}
        {% if person.names | select('public') | list | length %}
            {% with embedded=True %}
                {% set person_individual_name %}
                    {% include 'entity/label--person.html.j2' %}
                {% endset %}
            {% endwith %}
        {% endif %}
        <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, entity_contexts=entity_contexts(person) -%}
                                                {%- include 'entity/label--person.html.j2' -%}
                                            {%- endwith -%}
                                        </span>
                                    {%- endmacro %}
                                    {% if person_individual_name is defined %}
                                        {% trans person_individual_name = person_individual_name, 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') | select('public') | map(attribute='names') | flatten | select('public') | 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>
                    {% set public_siblings = siblings | select('public') | list %}
                    {% if public_siblings | length > 0 %}
                        <div class="family-breakdown">
                            {% with people=public_siblings, type_of='foaf:Person', property='rel:siblingOf' %}
                                {% include 'entity/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, entity_contexts=entity_contexts(person) %}
                            {% include 'entity/label--person.html.j2' %}
                        {% endwith %}
                    </span>
                {%- endmacro %}
                {% for family_parents, family_children in person | person_descendant_families %}
                    {% set family_parents = family_parents | reject('eq', person) | list %}
                    <div class="family">
                        <div class="family-description">
                            <div class="family-description-content">
                                <p>
                                    {%- if family_parents | length > 0 -%}
                                        {% if person_individual_name is defined %}
                                            {%- trans child_count = family_children | length, person_individual_name = person_individual_name, co_parent_labels = family_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 = family_children | length, co_parent_labels = family_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_individual_name is defined %}
                                            {%- trans child_count = family_children | length, person_individual_name = person_individual_name -%}
                                                {{ person_individual_name }} had a child.
                                            {%- pluralize -%}
                                                {{ person_individual_name }} had {{ child_count }} children.
                                            {%- endtrans -%}
                                        {%- else -%}
                                            {%- trans child_count = family_children | length -%}
                                                They had a child.
                                            {%- pluralize -%}
                                                They had {{ child_count }} children.
                                            {%- endtrans -%}
                                        {%- endif -%}
                                    {%- endif -%}
                                </p>
                                {% set ns = namespace(descendant_affiliation_names=[]) %}
                                {% for per_parent_child in family_children %}
                                    {% set ns.descendant_affiliation_names = ns.descendant_affiliation_names + (per_parent_child | walk('children') | list + [per_parent_child]) | select('public') | map(attribute='names') | flatten | select('public') | map(attribute='affiliation') | reject('none') | list %}
                                {% endfor %}
                                {% set ns.descendant_affiliation_names = ns.descendant_affiliation_names | unique | list | sort | list %}
                                {% if ns.descendant_affiliation_names | length > 0 %}
                                    <p>
                                        {%- trans -%}
                                            Descendant names include
                                        {%- endtrans %} {{ ns.descendant_affiliation_names[:3] | join(', ') -}}
                                            {%- if ns.descendant_affiliation_names | length <= 3 -%}
                                                .
                                            {%- else %},
                                            {%- with content_if_hidden = '...',
                                                content_if_shown = ns.descendant_affiliation_names[3:] | join(', ') ~ '.',
                                                items = ns.descendant_affiliation_names[3:] -%}
                                                {%- include 'show-countable.html.j2' -%}
                                            {%- endwith -%}
                                        {%- endif -%}
                                    </p>
                                {% endif %}
                            </div>
                        </div>
                        {% set public_family_children = family_children | select('public') | list %}
                        {% if public_family_children | length > 0 %}
                            {% with people=public_family_children, type_of='foaf:Person', property='rel:parentOf' %}
                                <div class="family-breakdown">
                                    {% include 'entity/list--person.html.j2' %}
                                </div>
                            {% endwith %}
                        {% endif %}
                    </div>
                {% endfor %}
            {% endif %}
        </section>

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

    {% with events=person | person_timeline_events %}
        {% include 'timeline.html.j2' %}
    {% endwith %}

    {% with files = person.associated_files %}
        {% include 'media.html.j2' %}
    {% endwith %}
{% endblock %}