<?php

/**
 * @file
 * Contains d_paragraphs.module..
 */

use Drupal\Core\Template\Attribute;
use Drupal\d_p\Exception\MissingConfigurationStorageFieldException;
use Drupal\d_p\ParagraphSettingTypesInterface;
use Drupal\d_p\Plugin\Field\FieldType\ConfigurationStorage;

/**
 * Implements hook_preprocess_HOOK().
 */
function d_p_side_image_preprocess_paragraph__d_p_side_image(array &$variables) {
  $paragraph = $variables['paragraph'];

  try {
    /** @var \Drupal\d_p\Plugin\Field\ConfigurationStorageFieldItemListInterface $setting_field */
    $setting_field = ConfigurationStorage::getSettingsFieldFromEntity($paragraph);

    $image_side = $setting_field
      ->getSettingValue(ParagraphSettingTypesInterface::PARAGRAPH_SETTING_SIDE_IMAGE_LAYOUT);
  }
  catch (MissingConfigurationStorageFieldException $exception) {
    \Drupal::logger('d_p_side_image')->error($exception->getMessage());
    return;
  }

  $wrapper_attributes = [
    'class' => [
      'image-side-' . $image_side,
    ],
  ];
  $variables['d_p_side_image_attributes'] = new Attribute($wrapper_attributes);

  switch ($image_side) {
    case 'left-wide':
    case 'right-wide':
      $variables['image_class'] = 'col-md-6 col-lg-7';
      $variables['text_class'] = 'col-md-6 col-lg-5';
      break;

    case 'left':
    case 'right':
    default:
      $variables['image_class'] = $variables['text_class'] = 'col-md-6';
      break;
  }
}

/**
 * Implements hook_theme().
 */
function d_p_side_image_theme($existing, $type, $theme, $path) {
  return [
    'paragraph__d_p_side_image' => [
      'base hook' => 'paragraph',
    ],
  ];
}
