{#
  Copyright DB InfraGO AG and contributors
  SPDX-License-Identifier: Apache-2.0
#}
{%
  from 'common_macros.html.j2' import
  description,
  display_property_label,
  display_property_values,
  linked_name_with_icon,
  render_allocated_functions,
  render_capability_section,
  render_connected_nodes_section,
  render_exchange_items_catalog,
  render_interfaces_section,
  show_other_attributes
%}

<h1>Definition of {{ object.name }}</h1>
{{ description(object) | safe }}

<h2>Capabilities</h2>
{% set capabilities = object.involved_capabilities %}
{{ render_capability_section(object, capabilities) | safe }}

<h2>Component Boundary</h2>
<p>
  The figure below provides an overview of <i>{{ object.name }}</i>'s boundary
  and depicts all the nodes that it is connected to.
</p>
{{ render_diagram(object, "context_diagram", display_derived_interfaces=True) }}
{{ render_connected_nodes_section(object, capabilities) | safe }}

<h2>Allocated Functions</h2>
{{ render_allocated_functions(object) | safe }}

<h2>Software Interfaces</h2>
{% set interfaces = object.related_exchanges %}
{% set exchange_items_catalog = [] %}
{{ render_interfaces_section(object, interfaces, exchange_items_catalog) | safe }}
<h2>Electrical interfaces of {{ object.name }}</h2>

<table>
  <thead>
    <tr>
      <th>Port</th>
      <th>Cable ID</th>
      <th>Target</th>
      <th>Target Port</th>
    </tr>
  </thead>
  <tbody>
    {%- for port in object.physical_ports %}
      {%- set cables = port.links %}
      {%- if cables %}
        {%- for cable in cables %}
          <tr>
            {%- set other_port = cable.ends | reject("equalto", port) | first %}
            {%- if loop.first %}
              <td rowspan="{{ cables | length }}">
                {{ linked_name_with_icon(port) | safe }}
              </td>
            {%- endif %}
            <td>{{ linked_name_with_icon(cable) | safe }}</td>
            <td>{{ linked_name_with_icon(other_port.owner) | safe }}</td>
            <td>{{ linked_name_with_icon(other_port) | safe }}</td>
          </tr>
        {%- endfor %}
      {%- else %}
        <tr>
          <td>{{ linked_name_with_icon(port) | safe }}</td>
          <td colspan="3">No cable connections planned</td>
        </tr>
      {%- endif %}
    {%- endfor %}
  </tbody>
</table>

{{ display_property_values(object) | safe }}

{% if exchange_items_catalog %}
  <h2>Appendix A: Exchange Items Catalogue</h2>
  <p>
    The following items (e.g. energy, material, information) are exchanged
    through the interactions with its interface partners.
  </p>
  {{ render_exchange_items_catalog(exchange_items_catalog) | safe }}
{% endif %}

<h2>Other Properties</h2>
{%-
  set excluded = [
      "allocated_functions",
      "context_diagram",
      "description",
      "diagrams",
      "name",
      "owner",
      "parent",
      "parts",
      "ports",
      "realizing_components",
      "related_exchanges",
      "state_machines",
      "xtype",
  ]
-%}
{{ show_other_attributes(object, excluded=excluded) | safe }}
