{#- -*- mode: sgml; tab-width: 1; indent-tabs-mode: nil -*-
 #
 # SPDX-License-Identifier: MIT
-#}

{%- import('macros/printElementId.xml.jinja2') as peid %}

{%- macro printComplexValue(cv, tag_name="COMPLEX-VALUE") %}
<{{tag_name}}>
  {%- for val in cv %}
  {%- if val is none %}
  <SIMPLE-VALUE />
  {%- elif hasattr(val, "extend")  %}
  {#- we check if the value has an extend() method, and if this
   #  is the case, we assume that it is a nested complex value.
   #}
  {{ printComplexValue(val) | indent(2) }}
  {%- else %}
  <SIMPLE-VALUE>{{val}}</SIMPLE-VALUE>
  {%- endif %}
  {%- endfor %}
</{{tag_name}}>
{%- endmacro %}

{# print either a simple or a complex comparam
 #}
{%- macro printAnyComparam(cp) %}
{#
 # This is slightly hacky: To distinguish complex and simple
 # communication parameters, we check if the 'cp' object has a
 # 'comparams' attribute. If it does, it is a complex parameter.
 #}
{%- if hasattr(cp, 'subparams') %}
{{- printComplexComparam(cp) }}
{%- else %}
{{- printSimpleComparam(cp) }}
{%- endif %}
{%- endmacro %}

{%- macro printSimpleComparam(cp) %}
<COMPARAM {{-peid.printElementIdAttribs(cp)}}
          PARAM-CLASS="{{cp.param_class}}"
          CPTYPE="{{cp.cptype.value}}"
         {{make_xml_attrib("DISPLAY-LEVEL", cp.display_level)}}{#- #}
          CPUSAGE="{{cp.cpusage.value}}">
  {{ peid.printElementIdSubtags(cp)|indent(1) }}
  <PHYSICAL-DEFAULT-VALUE>{{cp.physical_default_value_raw}}</PHYSICAL-DEFAULT-VALUE>
 <DATA-OBJECT-PROP-REF ID-REF="{{cp.dop_ref.ref_id}}" />
</COMPARAM>
{%- endmacro %}

{%- macro printComplexComparam(cp) %}
<COMPLEX-COMPARAM {{-peid.printElementIdAttribs(cp)}}
                  PARAM-CLASS="{{cp.param_class}}"
                  CPTYPE="{{cp.cptype.value}}"
                  {{make_xml_attrib("DISPLAY-LEVEL", cp.display_level)}}{#- #}
                  CPUSAGE="{{cp.cpusage.value}}"
                  {{make_bool_xml_attrib("ALLOW-MULTIPLE-VALUES", cp.allow_multiple_values_raw)}}
                  {#- #}>
 {{ peid.printElementIdSubtags(cp)|indent(1) }}
 {%- for sub_cp in cp.subparams %}
 {{- printAnyComparam(sub_cp) | indent(1, first=True) }}
 {%- endfor %}
 {%- if cp.physical_default_value is not none %}
 {{ printComplexValue(cp.physical_default_value, "COMPLEX-PHYSICAL-DEFAULT-VALUE") | indent(1) }}
 {%- endif %}
</COMPLEX-COMPARAM>
{%- endmacro %}
