<?php

/**
 * @file
 * Functions to support theming modules in the Toc Master theme.
 */

use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;
use Drupal\image\Entity\ImageStyle;

/**
 * Implements hook_form_alter().
 *
 * Trainline suite booking - Desktop and exposed form.
 */
function toc_emr_preprocess_tls_booking_form(&$vars) {
  $vars['el']['button_type'] = 'btn-default';
  $vars['el']['chip_icon'] = 'far fa-check-circle';
}
function toc_emr_preprocess_tls_booking_widget(&$vars) {
  $vars['button_type'] = 'btn-default';
  $vars['el']['chip_icon'] = 'far fa-check-circle';
}
function toc_emr_preprocess_tls_booking_form_exposed_alternative(&$vars) {
  $vars['el']['button_type'] = 'btn-default';
  $vars['el']['button_class'] = 'mb-3 p-2 px-3 rounded-3 w-100';
  $vars['el']['button_size'] = 'btn-lg';
}

/**
 * Preprocess for toc_best_fares_block_content().
 *
 * @param array $vars
 *   Theme variables.
 */
function toc_emr_preprocess_toc_best_fares_block_content(array &$vars) {
  $vars['tickets'] = _tm_misc_fares_direction_string($vars['tickets'], 'one-way', 'return');
}

/**
 * Preprocess for route_information_block().
 *
 * @param array $vars
 *   Theme variables.
 */
function toc_emr_preprocess_cheapest_ticket_types_block(array &$vars) {
  $vars["button_status"] = 'default';
}

/**
 * Preprocess hook for all card_blocks.
 */
function toc_emr_preprocess_card_block__front_bottom(&$vars) {
  if (!empty($vars['items'])) {
    foreach ($vars['items'] as $delta => $item) {
      /**  @var Drupal\eck\Entity\EckEntity $block */
      $block = $item['#card_block'];
      $block->__set('card_class', "modern-card cbelianr modern-card-alt mt-5");
      $vars['items'][$delta]['#card_block'] = $block;
    }
  }
}

function toc_emr_preprocess_card_block_alter(array &$suggestions, array $variables) {
  $config = $vars['element']['config'];
  $suggestions = [];

  if (!empty($config['entities'])) {
    $ids = array_column($config['entities'], 'target_id');
    // If one of the entity IDs is 21, use the 'bookearlycard' template suggestion.
    if (in_array(21, $ids)) {
      $suggestions[] = 'card_block__bookearlycard';
    }
    else {
      $suggestions[] = 'card_block__' . implode('_', $ids);
    }
  }

  if (!empty($config['machine_name'])) {
    $suggestions[] = 'card_block__' . $config['machine_name'];
  }

  return $suggestions;
}

function toc_emr_preprocess_card_block(array &$variables) {
  if (!empty($variables['element']['entities'])) {
    foreach ($variables['element']['entities'] as &$entity) {
      if (!empty($entity['media']['id']) && $entity['media']['type'] == 'image') {
        // Load the media entity using its ID.
        $media = \Drupal::entityTypeManager()
          ->getStorage('media')
          ->load($entity['media']['id']);
        if ($media) {
          // Get the view builder for media and build a render array.
          $view_builder = \Drupal::entityTypeManager()->getViewBuilder('media');
          // Use an appropriate view mode (e.g., 'teaser') to render the media.
          $entity['media']['render'] = $view_builder->view($media, 'teaser');
        }
      }
    }
  }
}

/**
 * Preprocess for interstitial_messages_modal().
 */
function toc_emr_preprocess_interstitial_messages_modal(&$vars) {
  $vars['confirm_button_type'] = 'default';
}

/**
 * Preprocess for journey_check_updates_items().
 */
function toc_emr_preprocess_journey_check_updates_items(&$vars) {
  $vars['cutoff'] = 6;
  $vars['item_name'] = 'announcements';
}
