<?php

/**
 * @file
 * Contains d_p_carousel module with Carousel Paragraph.
 */

use Drupal\d_p\ParagraphSettingTypesInterface;
use Drupal\d_p\Plugin\Field\FieldType\ConfigurationStorage;

/**
 * Implements hook_theme().
 */
function d_p_carousel_theme($existing, $type, $theme, $path) {
  return [
    'paragraph__d_p_carousel' => [
      'base hook' => 'paragraph',
    ],
    'paragraph__d_p_carousel_item' => [
      'base hook' => 'paragraph',
    ],
    'field__field_d_p_cs_item_reference' => [
      'base hook' => 'field',
    ],
  ];
}

/**
 * Implements hook_preprocess_field().
 */
function d_p_carousel_preprocess_field(array &$variables) {
  if ($variables['element']['#entity_type'] == 'paragraph') {
    if ($variables['element']['#bundle'] == 'd_p_carousel') {
      if ($variables['field_name'] == 'field_d_p_cs_item_reference') {
        /**
         * @var \Drupal\paragraphs\Entity\Paragraph $paragraph
         */
        $paragraph = $variables['element']['#object'];
        /** @var \Drupal\d_p\Plugin\Field\ConfigurationStorageFieldItemListInterface $setting_field */
        $setting_field = ConfigurationStorage::getSettingsFieldFromEntity($paragraph);

        $columns = [
          'md' => ParagraphSettingTypesInterface::COLUMN_COUNT_SETTING_NAME,
          'sm' => ParagraphSettingTypesInterface::COLUMN_COUNT_TABLET_SETTING_NAME,
          'xs' => ParagraphSettingTypesInterface::COLUMN_COUNT_MOBILE_SETTING_NAME,
        ];

        $columns_values = [];

        foreach ($columns as $breakpoint => $config_name) {
          $value = $setting_field->getSettingValue($config_name);
          $columns_values['columns_' . $breakpoint] = $value;
        }

        $variables['#attached']['drupalSettings']['d_p_carousel'][$paragraph->id()] = $columns_values;
      }
    }
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function d_p_carousel_preprocess_paragraph__d_p_carousel(array &$variables) {
  $variables['#attached']['library'][] = 'd_p_carousel/slick';
  $variables['#attached']['library'][] = 'd_p_carousel/d_p_carousel';
  $variables['#attached']['drupalSettings']['d_p_carousel']['sm'] = 992;
  $variables['#attached']['drupalSettings']['d_p_carousel']['xs'] = 540;
  $variables['attributes']['data-id'][] = $variables['paragraph']->id();
}

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

  /**
   * @var \Drupal\link\Plugin\Field\FieldType\LinkItem $link
   */
  $link = $paragraph->get('field_d_cta_link')->first();
  if (!is_null($link)) {
    $variables['has_link'] = TRUE;
    $variables['attributes']['href'][] = $link->getUrl()->toString();
  }
  else {
    $variables['has_link'] = FALSE;
  }
}

/**
 * Implements hook_d_p_centered_ckeditor_widget_paragraphs().
 */
function d_p_carousel_d_p_centered_ckeditor_widget_paragraphs(array &$paragraph_types) {
  $paragraph_types[] = 'd_p_carousel';
  $paragraph_types[] = 'd_p_carousel_item';
}
