<?php

/**
 * @file
 * Contains owlcarousel.module..
 */

use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function owlcarousel_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the owl module.
    case 'help.page.owlcarousel':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('OWL Carousel: Download code at: https://github.com/tabvn/owl') . '</p>';
      $output .= '<p>' . t('Video tutorial how to install: https://www.youtube.com/watch?v=PzBMJqkNHQ0') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_theme().
 */
function owlcarousel_theme() {
  return [
    'owlcarousel' => [
      'variables' => ['items' => NULL, 'settings' => NULL],
    ],
    'owlcarousel_views' => [
      'variables' => [],
    ],
  ];
}

/**
 * Themeable for owlcarousel.
 */
function template_preprocess_owlcarousel(&$variables) {
  $items = $variables['items'];

  $settings = _owlcarousel_format_settings($variables['settings']);

  $variables['attributes']['class'][] = 'owl-slider-wrapper';
  $variables['attributes']['class'][] = 'owl-carousel';
  $variables['attributes']['class'][] = 'owl-theme';
  $variables['attributes']['data-settings'] = json_encode($settings);

  $html = '';
  if (!empty($items)) {
    $i = 0;
    foreach ($items as $item) {
      $html .= '<div class="owl-item-inner owl-item-inner' . $i . '">' . \Drupal::service('renderer')->render($item) . '</div>';
      $i++;
    }
  }
  $output = [
    '#type' => 'markup',
    '#markup' => $html,
  ];
  $variables['output'] = \Drupal::service('renderer')->render($output);

}

/**
 * Prepares variables for Views OwlCarousel carousel templates.
 *
 * Default template: owlcarousel-views.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *     - view: A View object.
 */
function template_preprocess_owlcarousel_views(array &$variables) {
  $handler = $variables['view']->style_plugin;

  $settings = _owlcarousel_format_settings($handler->options);
  // Used JSON_FORCE_OBJECT to force the numbers
  // in the $settings array keys.
  $variables['attributes']['data-settings'] = json_encode($settings, JSON_FORCE_OBJECT);
  $variables['attributes']['class'][] = 'owl-slider-wrapper';
  $variables['attributes']['class'][] = 'owl-carousel';
  $variables['attributes']['class'][] = 'owl-theme';

  $attached = [
    '#attached' => ['library' => ['owlcarousel/owlcarousel']],
  ];
  \Drupal::service('renderer')->render($attached);
  template_preprocess_views_view_unformatted($variables);
}

/**
 * Return formatted js array of settings.
 */
function _owlcarousel_format_settings($settings) {
  $settings['items'] = (int) $settings['items'];

  $settings['margin'] = (int) $settings['margin'];
  $settings['nav'] = (bool) $settings['nav'];
  $settings['autoplay'] = (bool) $settings['autoplay'];
  $settings['autoplayHoverPause'] = (bool) $settings['autoplayHoverPause'];
  $settings['loop'] = (bool) $settings['loop'];
  $settings['dots'] = (bool) $settings['dots'];
  $settings['rtl'] = (bool) $settings['rtl'];

  if (isset($settings['itemsMobile'])) {
    $dimensionMobile = isset($settings['dimensionMobile']) ? (int) $settings['dimensionMobile'] : 0;
    // Create an array for $itemsMobile
    $itemsMobile = [];
    // Set the 'items' key in $itemsMobile
    $itemsMobile['items'] = isset($settings['itemsMobile']) ? (int) $settings['itemsMobile'] : 0;
    // Check if $settings['responsive'] is an array; if not, initialize it
    if (!isset($settings['responsive']) || !is_array($settings['responsive'])) {
      $settings['responsive'] = [];
    }
    // Add $itemsMobile to $settings['responsive'] at the specified dimension
    $settings['responsive'][$dimensionMobile] = $itemsMobile;
  }
  
  if (isset($settings['itemsTablet'])) {
    $dimensionTablet = isset($settings['dimensionTablet']) ? (int) $settings['dimensionTablet'] : 0;
    // Create an array for $itemsTablet
    $itemsTablet = [];
    // Set the 'items' key in $itemsMobile
    $itemsTablet['items'] = isset($settings['itemsTablet']) ? (int) $settings['itemsTablet'] : 0;
    // Check if $settings['responsive'] is an array; if not, initialize it
    if (!isset($settings['responsive']) || !is_array($settings['responsive'])) {
      $settings['responsive'] = [];
    }
    // Add $itemsMobile to $settings['responsive'] at the specified dimension
    $settings['responsive'][$dimensionTablet] = $itemsTablet;
  }

  if (isset($settings['itemsDesktop'])) {
    $dimensionDesktop = isset($settings['dimensionDesktop']) ? (int) $settings['dimensionDesktop'] : 0;
    // Create an array for $itemsDesktop
    $itemsDesktop = [];
    // Set the 'items' key in $itemsDesktop
    $itemsDesktop['items'] = isset($settings['itemsDesktop']) ? (int) $settings['itemsDesktop'] : 0;
    // Check if $settings['responsive'] is an array; if not, initialize it
    if (!isset($settings['responsive']) || !is_array($settings['responsive'])) {
      $settings['responsive'] = [];
    }
    // Add $itemsDesktop to $settings['responsive'] at the specified dimension
    $settings['responsive'][$dimensionDesktop] = $itemsDesktop;
  }

  if (isset($settings['image_style'])) {
    unset($settings['image_style']);
  }
  if (isset($settings['image_link'])) {
    unset($settings['image_link']);
  }

  return $settings;
}
