<?php

/**
 * @file
 * Contains d_commerce_product module.
 */

use Drupal\commerce_product\Entity\ProductInterface;
use Drupal\commerce_product\Entity\ProductVariationInterface;
use Drupal\Core\Entity\EntityInterface;

/**
 * Implements hook_preprocess_field() for HTML document templates.
 */
function d_commerce_product_preprocess_field(&$variables) {
  $object = $variables['element']['#object'] ?? NULL;
  if ($object instanceof ProductInterface) {
    switch ($variables['field_name']) {
      case 'field_product_images':
        $variables['#attached']['library'][] = 'd_commerce_product/d_product_commerce_slider';
        $slider = views_embed_view('commerce_product_images', 'block_1');
        $variables['slider'] = \Drupal::service('renderer')->render($slider);
        break;

      case 'variations':
        $variables['label_hidden'] = $variables['element']['#items']->count() <= 1;
        break;

      case 'title':
        $variables['product_title'] = _d_commerce_product_get_title($object);
        break;

      default:
        break;
    }
  }
}

/**
 * Implements hook_preprocess_HOOK().
 *
 * Remove title block from commerce product page.
 *
 * @see hook_preprocess_HOOK()
 */
function d_commerce_product_preprocess_page(&$variables) {
  $route_name = \Drupal::routeMatch()->getRouteName();
  $no_title_routes = ['entity.commerce_product.canonical'];
  if (in_array($route_name, $no_title_routes) && !isset($variables['node'])) {
    foreach ($variables['page']['content'] as &$element) {
      if (isset($element['#plugin_id']) && $element['#plugin_id'] == 'page_title_block') {
        $element['#access'] = FALSE;
      }
    }
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function d_commerce_product_preprocess_commerce_cart_block(&$variables) {
  $variables['#attached']['library'][] = 'd_commerce_product/d_product_commerce.cart';
}

/**
 * Implements hook_theme().
 *
 * Register theme's for commerce product.
 *
 * @see hook_theme()
 */
function d_commerce_product_theme($existing, $type, $theme, $path) {
  return [
    'commerce_product__droopler_products__full' => [
      'template' => 'commerce-product--droopler-products--full',
      'base hook' => 'commerce_product',
    ],
    'commerce_product__droopler_products__teaser' => [
      'template' => 'commerce-product--droopler-products--teaser',
      'base hook' => 'commerce_product__droopler_products__teaser',
    ],
    'commerce_product_variation__droopler_products__teaser' => [
      'template' => 'commerce-product-variation--droopler-products--teaser',
      'base hook' => 'commerce_product_variation__droopler_products__teaser',
    ],
    'field__commerce_product__field_product_images__droopler_products' => [
      'template' => 'field--commerce-product--field-product-images--droopler-products',
      'base hook' => 'media',
    ],
    'field__commerce_product__title__droopler_products' => [
      'template' => 'field--commerce-product--title--droopler-products',
      'base hook' => 'field',
    ],
    'views_view__commerce_product_images' => [
      'template' => 'views-view--commerce_product_images',
      'base hook' => 'views_view__product_images',
    ],
    'views_view__commerce_product_images__attachment_1' => [
      'template' => 'views-view--commerce_product_images--attachment_1',
      'base hook' => 'views_view__product_images_attachment_1',
    ],
  ];
}

/**
 * Implements hook_entity_view_mode_alter().
 */
function d_commerce_product_entity_view_mode_alter(&$view_mode, EntityInterface $entity, $context) {
  if (
    $entity instanceof ProductVariationInterface
    && 'summary' === $view_mode
  ) {
    $view_mode = 'teaser';
  }
}

/**
 * Returns product page title based on number of variations.
 *
 * @param \Drupal\commerce_product\Entity\ProductInterface $product
 *   Displayed product.
 *
 * @return string
 *   Product page title.
 */
function _d_commerce_product_get_title(ProductInterface $product) {
  $variationsIds = $product->getVariationIds();
  if (count($variationsIds) > 1) {
    $title = $product->getTitle();
  }
  else {
    $variation = $product->getDefaultVariation();
    $title = $variation ? $variation->getTitle() : $product->getTitle();
  }

  return $title;
}
