{#
/**
 * @file
 * Default theme implementation to display a paragraph.
 *
 * Available variables:
 * - paragraph: Full paragraph entity.
 *   Only method names starting with "get", "has", or "is" and a few common
 *   methods such as "id", "label", and "bundle" are available. For example:
 *   - paragraph.getCreatedTime() will return the paragraph creation timestamp.
 *   - paragraph.id(): The paragraph ID.
 *   - paragraph.bundle(): The type of the paragraph, for example, "image" or "text".
 *   - paragraph.getOwnerId(): The user ID of the paragraph author.
 *   See Drupal\paragraphs\Entity\Paragraph for a full list of public properties
 *   and methods for the paragraph object.
 * - content: All paragraph items. Use {{ content }} to print them all,
 *   or print a subset such as {{ content.field_example }}. Use
 *   {{ content|without('field_example') }} to temporarily suppress the printing
 *   of a given child element.
 * - attributes: HTML attributes for the containing element.
 *   The attributes.class element may contain one or more of the following
 *   classes:
 *   - paragraphs: The current template type (also known as a "theming hook").
 *   - paragraphs--type-[type]: The current paragraphs type. For example, if the paragraph is an
 *     "Image" it would result in "paragraphs--type--image". Note that the machine
 *     name will often be in a short form of the human readable label.
 *   - paragraphs--view-mode--[view_mode]: The View Mode of the paragraph; for example, a
 *     preview would result in: "paragraphs--view-mode--preview", and
 *     default: "paragraphs--view-mode--default".
 * - view_mode: View mode; for example, "preview" or "full".
 * - logged_in: Flag for authenticated user status. Will be true when the
 *   current user is a logged-in member.
 * - is_admin: Flag for admin user status. Will be true when the current user
 *   is an administrator.
 *
 * @see template_preprocess_paragraph()
 *
 * @ingroup themeable
 */
#}
{{ attach_library('kamihaya_cms_paragraph_type/image-link-css') }}
{% block paragraph %}
  {% block content %}
    {% set align = content.field_align|render|striptags|trim|lower %}
    {%
    set classes = [
        'paragraph',
        'paragraph--type--' ~ paragraph.bundle|clean_class,
        view_mode ? 'paragraph--view-mode--' ~ view_mode|clean_class,
        not paragraph.isPublished() ? 'paragraph--unpublished',
        align ? 'align-' ~ align,
      ]
    %}
    <div{{attributes.addClass(classes)}}>
      {# Image link #}
      {% set imageLink = content.field_image_link.0['#url'] %}

      {# Image url #}
      {% set imageUrl = content.field_image_url %}
      {% set width = paragraph.field_width.0.value %}
      {% set width_unit = paragraph.field_width_unit.0.value %}
      {% set mobile_height = paragraph.field_mobile_height.0.value %}
      {% set padding = paragraph.field_padding.0.value %}
      {% set valign = paragraph.field_image_text_valign.0.value %}
      {% set desc_position = paragraph.field_image_desc_position.0.value %}

      {% if width %}
        {% set image_style = [width_unit == 'px' ? ('width: 100%; max-width: ' ~ width ~ width_unit ~ ';') : ('width: ' ~ width ~ (width_unit ? width_unit ~ ';' : 'px;'))] %}
        {% if width_unit == 'vw' %}
          {%
            set image_style = image_style|merge([
              'margin-left: calc(-50vw + 50%);',
              'max-width: none;',
          ])
          %}
        {% endif %}
      {% endif %}
      {% if mobile_height %}
        <style>
          @media(max-width: 767px) {
            .paragraph--type--image-link img {
              min-height: {{mobile_height}}px;
              object-fit: cover;
            }
          }
        </style>
      {% endif %}

      {# Background #}
      {% if content.field_text_bg_color|render %}
        {% set bg_color = content.field_text_bg_color|render|striptags|trim|lower %}
        {% set bg_pd_x = paragraph.field_text_bg_padding_x.0.value %}
        {% set bg_pd_y = paragraph.field_text_bg_padding_y.0.value %}
        {% set bg_width = paragraph.field_text_bg_width.0.value %}
        {% set bg_width_unit = paragraph.field_text_bg_width_unit.0.value %}
        {%
          set bg_classes = [
            'image-text-bg',
            bg_pd_x ? bg_pd_x,
            bg_pd_y ? bg_pd_y,
          ]
        %}
        {%
          set bg_style = [
            bg_color ? 'background-color: ' ~ bg_color ~ ';',
            bg_width ? 'width: ' ~ bg_width ~ (bg_width_unit ? bg_width_unit ~ ';' : 'px;'),
          ]
        %}
      {% endif %}
      {%
        set text_classes = [
          'image-link-text',
          content.field_image_text_halign|render ? 'image-text-halign-' ~ paragraph.field_image_text_halign.0.value,
          valign ? 'image-text-valign-' ~ valign,
        ]
      %}

      {% set size = paragraph.field_image_text_size.0.value %}
      {% set bold = paragraph.field_font_bold.0.value %}
      {% set color = content.field_image_text_color|render|striptags|trim|lower %}
      {%
        set style = [
          size ? 'font-size: '  ~  size  ~  'px;' : 'font-size: 16px;',
          color ? 'color: '  ~ color  ~ ';' : 'color: #fff;',
          bold ? 'font-weight: bold;',
        ]
      %}
      {% set dec_size = paragraph.field_image_desc_text_size.0.value %}
      {% set dec_color = content.field_image_desc_color|render|striptags|trim|lower %}
      {% set dec_align = paragraph.field_image_desc_text_align.0.value %}
      {% set dec_style = [
        dec_size ? 'font-size: ' ~ dec_size ~ 'px;' : 'font-size: 16px;',
        dec_color ? 'color: ' ~ dec_color ~ ';' : (color ? 'color: ' ~ color ~ ';' : 'color: #fff;'),
      ] %}
      <div class="bg-image" {% if image_style %} style="{{ image_style|join(' ') }}" {% endif %}>

        {% if content.field_image_link|render and content.field_link_text.0 is empty %}
          <a class="image-link" href="{{ imageLink }}" target="{{ content.field_image_link[0]['#url'].external ? '_blank' : '_self' }}">
        {% endif %}

        {% if content.field_image_text|render and valign == 'above' %}
          <div class="{{ text_classes|join(' ') }}" style="{{ style|join(' ') }}">
            {% if bg_classes %}
              <div class="{{ bg_classes|join(' ') }}" style="{{ bg_style|join(' ') }}">
            {% endif %}
            <div>
              <p>{{ content.field_image_text }}</p>
              {% if content.field_image_description|render and desc_position != 1 %}
                 <p {% if dec_style %} style="{{ dec_style|join(' ') }}" {% endif %} class="image-description{% if dec_align %} text-align-{{ dec_align }}{% endif %}">{{ content.field_image_description }}</p>
              {% endif %}
            </div>
            {% if bg_classes %}
              </div>
            {% endif %}
          </div>
        {% endif %}

        {# Image #}
        {% if content.field_image|render %}
          {% if content.field_mobile_image|render %}
            {% set mobileImageUrl = content.field_mobile_image_url %}
              <picture>
                <source srcset="{{ mobileImageUrl }}" media="(max-width:767px)">
          {% endif %}
          <img src="{{ imageUrl }}" alt="{{ content.field_image.0['#item'].alt }}" class="w-100"/>
          {% if content.field_mobile_image|render %}
            </picture>
          {% endif %}
        {% endif %}

        {# Text #}
        {% if content.field_image_text|render and valign != 'above'%}
          {% if  valign != 'under' %}
            <div class="mask"{% if padding %} class="{{ padding }};"{% endif %}>
          {% endif %}
            <div class="{{ text_classes|join(' ') }}" style="{{ style|join(' ') }}">
              {% if bg_classes %}
                <div class="{{ bg_classes|join(' ') }}" style="{{ bg_style|join(' ') }}">
              {% endif %}
              <div>
                <p>{{ content.field_image_text }}</p>
                {% if content.field_image_description|render and desc_position != 1 %}
                  <p {% if dec_style %} style="{{ dec_style|join(' ') }}" {% endif %} class="image-description{% if dec_align %} text-align-{{ dec_align }}{% endif %}">{{ content.field_image_description }}</p>
                {% endif %}
              </div>
              {% if bg_classes %}
                </div>
              {% endif %}
            </div>
          {% if valign != 'under' %}
            </div>
          {% endif %}
        {% endif %}
        {# Text #}
        {% if content.field_link_text|render %}
          {% set mobile_align = content.field_link_text_sp_halign|render|striptags|trim|lower %}
          {%
            set link_text_classes = [
              'link-text',
              content.field_link_text_halign|render ? 'link-text-halign-' ~ paragraph.field_link_text_halign.0.value,
              content.field_link_text_valign|render ? 'link-text-valign-' ~ paragraph.field_link_text_valign.0.value,
              mobile_align ? 'sp-align-' ~ mobile_align,
            ]
          %}
          <div class="link-text-mask"{% if padding %} class="{{ padding }};"{% endif %}>
            <div class="{{ link_text_classes|join(' ') }}">
              {% if bg_classes %}
                <div class="{{ bg_classes|join(' ') }}" style="{{ bg_style|join(' ') }}">
              {% endif %}
              {{ content.field_link_text }}
              {% if bg_classes %}
                </div>
              {% endif %}
            </div>
          </div>
        {% endif %}
        {% if content.field_image_description|render and desc_position == 1 %}
           <p {% if dec_style %} style="{{ dec_style|join(' ') }}" {% endif %} class="image-description{% if dec_align %} text-align-{{ dec_align }}{% endif %}">{{ content.field_image_description }}</p>
        {% endif %}

        {% if content.field_image_link|render and content.field_link_text.0 is empty %}
          </a>
        {% endif %}
      </div>
    </div>
  {% endblock %}
{% endblock paragraph %}
