{#
  Copyright DB InfraGO AG and contributors
  SPDX-License-Identifier: Apache-2.0
#}
{%
  from 'common_macros.html.j2' import
  show_other_attributes,
  description,
  linked_name,
  linked_name_with_icon,
  display_traceability,
  render_constraints,
  render_related_diagrams,
  display_property_values
%}

{% macro describe_exchange(object) %}
  {%- set source_function = object.source.owner -%}
  {%- set target_function = object.target.owner -%}
  {%- set source = source_function.owner -%}
  {%- set target = target_function.owner -%}
  The {{ linked_name(source) | safe }} shall provide
  {{ linked_name(object) | safe }} to {{ linked_name(target) | safe }} so that
  the {{ linked_name(target) | safe }} could
  {{ linked_name(target_function)|safe }}.
{% endmacro %}

<h1>Logical Interface {{ object.name }}</h1>
<small class="print:hidden" style="opacity:0.5;"
  >UUID: {{ object.uuid }}</small
>
<h2>Document Purpose</h2>
<p>
  This document specifies a logical interface between
  {{ linked_name(object.source.owner) | safe }} and
  {{ linked_name(object.target.owner) | safe }}.
</p>
<p>
  Its purpose is to explain the need for interface and provide requirements so
  that the interface partners could find an appropriate interface definition
  (solution).
</p>
{% if object.description -%}
  <p>{{ object.description | safe }}</p>
{%- endif %}
{% if object.summary -%}
  <p>{{ object.summary | safe }}</p>
{%- endif %}
<h2>Purpose of the interface</h2>
<p>
  The figure below provides an overview of the behaviours that depend on this
  interface:
</p>
{{ render_diagram(object, "context_diagram") }}
<p>{{ object.name }} enables the following functional interactions:</p>
<table>
  <thead>
    <tr>
      <th>ID</th>
      <th>Interaction description</th>
    </tr>
  </thead>
  <tbody>
    {% for exchange in object.allocated_functional_exchanges %}
      <tr>
        <td>
          <a href="{{ exchange | make_href }}"
            >{{ "LEXC-" + exchange.uuid[:5] | upper }}</a
          >
        </td>
        <td>
          {{ describe_exchange(exchange) | safe }}
          {% if exchange.exchange_items %}
            {% if exchange.exchange_items | length > 1 %}
              <p>
                {{ exchange.name }} is further specified via the following
                Exchange Items:
              </p>
              <ul>
                {% for item in exchange.exchange_items %}
                  <li>{{ linked_name(item) | safe }}</li>
                {% endfor %}
              </ul>
            {% else %}
              <p>
                This interaction is further specified via
                {{ linked_name(exchange.exchange_items[0]) | safe }} Exchange
                Item
              </p>
            {% endif %}
          {% endif %}
        </td>
      </tr>
    {% endfor %}
  </tbody>
</table>

{% set fchains = object.allocated_functional_exchanges.map("involving_functional_chains") %}
{%
  set allocated_functions =
  object.allocated_functional_exchanges.map("source.owner")
  + object.allocated_functional_exchanges.map("target.owner")
%}
{%- if fchains -%}
  <h2>Associated Functional Chains</h2>
  <p>
    {{ object.name }} can be associated to the following functional chains
    through involvements of functions and interactions.
  </p>
  {%- for fchain in fchains -%}
    <h3>{{ linked_name_with_icon(fchain) | safe }}</h3>
    <p>{{ description(fchain) | safe }}</p>
    {%- set involved_caps = model.search("CapabilityRealization").by_involved_chains(fchain) -%}
    {%- if involved_caps -%}
      <h4>Involving capabilities</h4>
      <ul>
        {%- for cap in involved_caps -%}
          <li>{{ linked_name_with_icon(cap) | safe }}</li>
        {%- endfor -%}
      </ul>
    {%- endif -%}
    <h4>Involved functions and interactions</h4>
    <table>
      <thead>
        <tr>
          <th>Involved functions</th>
          <th>Involved interactions</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            <ul>
              {%- for fnc in fchain.involved_functions -%}
                {%- if fnc.uuid in allocated_functions | map(attribute="uuid") -%}
                  <li>{{ linked_name_with_icon(fnc) | safe }}</li>
                {%- endif -%}
              {%- endfor -%}
            </ul>
          </td>
          <td>
            <ul>
              {%- for link in fchain.involved_links -%}
                {%- if link.uuid in object.allocated_functional_exchanges | map(attribute="uuid") -%}
                  <li>{{ linked_name_with_icon(link) | safe }}</li>
                {%- endif -%}
              {%- endfor -%}
            </ul>
          </td>
        </tr>
      </tbody>
    </table>
  {%- endfor -%}
{%- endif -%}

{{ render_related_diagrams(object) | safe }}

{{ render_constraints(object) | safe }}

{{ display_property_values(object, heading="Additional properties") | safe }}

{{ display_traceability(object) | safe }}

<h2>Other attributes</h2>
{%-
  set excluded = [
    "allocated_functional_exchanges",
    "applied_property_value_groups",
    "applied_property_values",
    "constraints",
    "context_diagram",
    "exchange_items",
    "name",
    "parent",
    "property_value_groups",
    "realizing_component_exchanges",
    "source",
    "target",
    "uuid",
    "visible_on_diagrams",
  ]
-%}
{{ show_other_attributes(object, excluded=excluded) | safe }}
