{# LIBRARIES #}
{{ attach_library('ui_patterns/libraries.bs_card') }}
{{ attach_library('ui_patterns/bs_card.card_lib') }}

{# VARIABLES #}
{% set alt = alt|default(title) %}

{% set card_attributes = card_attributes|default(create_attribute()) %}
{% set content_attributes = content_attributes|default(create_attribute()) %}
{% set header_attributes = header_attributes|default(create_attribute()) %}
{% set footer_attributes = footer_attributes|default(create_attribute()) %}

{% set link_scope = link_scope|default(false) %}
{% set card_link = card_link|default(false) %}

{% set body_content = body_content|default(false) %}
{% set footer_content = footer_content|default(false) %}
{% set header_content = header_content|default(false) %}

{% set card_class = [
  'card',
  card_class ?? '',
  card_boardered ? '' : "n-brd",
  card_rounded ? '' : "n-rnd",
  card_fade ? 'with-fade',
  (card_link and (link_scope == 'card')) ? "card-link",
] %}

{% set card_tag, header_tag, body_tag, footer_tag = "div", "div", "div", "div" %}

{# Determine link scope and set element tags correctly. #}
{% if link_scope and card_link %}
  {% if link_scope == 'card' %}
    {% set card_tag = "a" %}
    {% set card_attributes = card_attributes
      .setAttribute("href", card_link)
      .setAttribute("aria-label", alt)
    %}
  {% elseif link_scope == 'header' %}
    {% set badge_alt = alt %}
    {% if badge_text_top and badge_text_bottom %}
      {% set badge_alt = badge_text_top ~ ' ' ~ badge_text_bottom ~ ' - ' ~ alt %}
    {% endif %}
    {% set header_tag = "a" %}
    {% set header_attributes = header_attributes
      .setAttribute("href", card_link)
      .setAttribute("aria-label", badge_alt)
    %}
  {% elseif link_scope == 'body' %}
    {% set body_tag = "a" %}
    {% set content_attributes = content_attributes
      .setAttribute("href", card_link)
      .setAttribute("aria-label", alt)
    %}
  {% elseif link_scope == 'footer' %}
    {% set footer_tag = "a" %}
    {% set footer_attributes = footer_attributes
      .setAttribute("href", card_link)
      .setAttribute("aria-label", alt)
    %}
  {% elseif link_scope == 'top' %}
    {% set badge_alt = alt %}
    {% if badge_text_top and badge_text_bottom %}
      {% set badge_alt = badge_text_top ~ ' ' ~ badge_text_bottom ~ ' - ' ~ alt %}
    {% endif%}
    {% set header_tag = "a" %}
    {% set header_attributes = header_attributes
      .setAttribute("href", card_link)
      .setAttribute("aria-label", badge_alt)
    %}
    {% set body_tag = "a" %}
    {% set content_attributes = content_attributes
      .setAttribute("href", card_link)
      .setAttribute("aria-label", alt)
    %}
  {% elseif link_scope == 'bottom' %}
    {% set body_tag = "a" %}
    {% set content_attributes = content_attributes
      .setAttribute("href", card_link)
      .setAttribute("aria-label", alt)
    %}
    {% set footer_tag = "a" %}
    {% set footer_attributes = footer_attributes
      .setAttribute("href", card_link)
      .setAttribute("aria-label", alt)
    %}
  {% endif %}
{% endif %}

{# TEMPLATE #}
<{{ card_tag }} {{ card_attributes.addClass(card_class) }}>

{# Card header. Block, content and clause. #}
  {% if show_header %}
    <{{ header_tag }} {{ header_attributes.addClass(["card-header"]) }}>
    {% block card_header %}
      {# Header content. This is usually a title. #}
      {% if header_content %}
        {{ header_content|raw }}
      {% endif %}

      {% if image %}
        {{ image }}
      {% endif %}

      {# Header image. This goes full width and is not padded. #}
      {% if img_alt and img_url %}
        {% include "@atoms/html5/image/image.twig" with {
          'alt': img_alt,
          'url': img_url,
          'img_attributes': img_attributes|default(create_attribute()),
          'img_class': 'w-100 card-image',
          'type': 'fg'
        } only %}
      {% endif %}

    {% endblock %}
    </{{ header_tag }}>
  {% endif %}

  {# Card Body. Block, content and clause #}
<{{ body_tag }} {{ content_attributes.addClass([content_class ?? '', 'card-body']) }}>
{% block card_body %}
  {% if body_content %}
    {{ body_content|raw }}
  {% endif %}
{% endblock %}
</{{ body_tag }}>

{# Card Body. Block, content and clause #}
  {% if show_footer %}
    <{{ footer_tag }} {{ footer_attributes.addClass(["card-footer"]) }}>
    {% block card_footer %}
      {% if footer_content %}
        {{ footer_content|raw }}
      {% endif %}
    {% endblock %}
    </{{ footer_tag }}>
  {% endif %}

</{{ card_tag }}>
