<?php

/**
 * @file
 * File containing Counter Paragraph module.
 */

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

/**
 * Implements hook_theme().
 */
function d_p_counters_theme($existing, $type, $theme, $path) {
  return [
    'paragraph__d_p_group_of_counters' => [
      'base hook' => 'paragraph',
    ],
    'paragraph__d_p_single_counter' => [
      'base hook' => 'paragraph',
    ],
    'field__paragraph__field_d_counter_reference__d_p_group_of_counters' => [
      'base hook' => 'field',
    ],
    'field__paragraph__field_d_number__d_p_single_counter' => [
      'base hook' => 'field',
    ],
    'field__paragraph__field_d_main_title__d_p_single_counter' => [
      'base hook' => 'field',
    ],
  ];
}

/**
 * Implements hook_preprocess_HOOK().
 */
function d_p_counters_preprocess_paragraph__d_p_group_of_counters(array &$variables) {
  $variables['#attached']['library'][] = 'd_p_counters/d_p_counters';
}

/**
 * Implements hook_preprocess_field().
 */
function d_p_counters_preprocess_field(&$variables) {
  if ($variables['element']['#entity_type'] == 'paragraph') {
    if ($variables['element']['#bundle'] == 'd_p_group_of_counters') {
      if ($variables['field_name'] == 'field_d_counter_reference') {
        $paragraph = $variables['element']['#object'];
        $logger = \Drupal::logger('d_p_counters');
        try {
          /** @var \Drupal\d_p\Plugin\Field\ConfigurationStorageFieldItemListInterface $settings_field */
          $settings_field = ConfigurationStorage::getSettingsFieldFromEntity($paragraph);

          $columns = [
            'col-md-' => $settings_field->getSettingValue(ParagraphSettingTypesInterface::COLUMN_COUNT_SETTING_NAME),
            'col-sm-' => $settings_field->getSettingValue(ParagraphSettingTypesInterface::COLUMN_COUNT_TABLET_SETTING_NAME),
            'col-' => $settings_field->getSettingValue(ParagraphSettingTypesInterface::COLUMN_COUNT_MOBILE_SETTING_NAME),
          ];

          $grid_size = 12;
          $classes = [];
          foreach ($columns as $key => $value) {
            $classes[] = $key . ($grid_size / $value);
          }

          $variables['column_class'] = implode(' ', $classes);
        }
        catch (MissingConfigurationStorageFieldException $exception) {
          $logger->error($exception->getMessage());
        }

        // Add custom classes.
        foreach ($variables["items"] as &$item) {
          try {
            /** @var \Drupal\d_p\Plugin\Field\ConfigurationStorageFieldItemListInterface $item_setting_field */
            $item_setting_field = ConfigurationStorage::getSettingsFieldFromEntity($item["content"]["#paragraph"]);

            $item['attributes']->addClass($item_setting_field->getClasses());
          }
          catch (MissingConfigurationStorageFieldException $exception) {
            $logger->error($exception->getMessage());
          }
        }
      }
    }
  }
}

/**
 * Implements hook_d_p_centered_ckeditor_widget_paragraphs().
 */
function d_p_counters_d_p_centered_ckeditor_widget_paragraphs(array &$paragraph_types) {
  $paragraph_types[] = 'd_p_group_of_counters';
}
