<?php

/**
 * Preprocess for buy_tickets_header().
 *
 * @param array $vars
 *   Theme variables.
 */
function toc_sr_preprocess_buy_tickets_header(array &$vars) {
  // Disable Live Departures
  $vars['ld_search'] = False;
  // Enable Check Service and Next Train forms.
  $vars['check_service_form'] = $vars['check_service'] ?? NULL;
  $vars['next_train_form'] = $vars['next_train'] ?? NULL;
}

/**
 * Preprocess for page_banner_random().
 */
function toc_sr_preprocess_page_banner_random(array &$vars) {
  // Use the original image size for the bookend style banner overlay.
  $overlay_image_mid = $vars['element']['config']['overlay_image'] ?? 0;
  if ($overlay_image_mid) {
    $overlay_image = _tm_misc_get_styled_images_from_media($overlay_image_mid, '', 'field_media_image', 'media', 'image', 'poly-over crsl-img');
    if ($overlay_image) {
      $vars['element']['overlay'] = $overlay_image;
    }
  }

  // Provide functionality to overwrite it for custom nodes.
  // Not entirely sure this is the way to do it as it has module services,
  // but it was the only way I could think of for the theme to do something
  // like this.
  $node = \Drupal::routeMatch()->getParameter('node');
  if (!$node) {
    return;
  }

  switch ($node->bundle()) {
    case 'tourism_hub_page':
      if (!\Drupal::hasService('tourism_hub.service')
        || !\Drupal::hasService('toc_page_banner.banner_services')) {
        break;
      }
      $region = _tm_misc_get_field_content($node, 'field_thp_er_region');
      // Ensure region is NULL if we don't have a value.
      if (!$region) {
        $region = NULL;
      }
      $th_service = \Drupal::service('tourism_hub.service');
      $banner_settings = $th_service->getPageSettings($region)->get('banner');

      if ($banner_settings['use_landing_page_banner'] ?? false) {
        $banner_settings = $th_service->getPageSettings()->get('banner');
      }

      $id = $banner_settings['image'] ?? false;
      if (!$id) {
        break;
      }
      $tpb_service = \Drupal::service('toc_page_banner.banner_services');
      $image = $tpb_service->getStyledImagesFromMedia($id, 'banner_tall', 'field_media_image', 'media', 'crsl-img');

      if ($image) {
        $vars['element']['random'] = $image;
        $vars['element']['overlay'] = NULL;
        $vars['element']['config'] = NULL;
        $vars['carousel_class_size'] = 'bnr_crsl_md';
      }

      break;
  }
}

/**
 * This is a single entity, so perhaps in content-content ? doesn't really
 * belong here. SR front page top link block, adding auto-height.
 */
function toc_sr_preprocess_link_block__front_banner(&$vars) {
  $template_vars = [
    'link_scope' => 'block',
    'block_class' => 'bl-va cbelianr h-100 d-flex mhs-7',
    'header_tag' => 'h2',
  ];
  _toc_master_preprocess_link_block($vars, $template_vars);
}

/**
 * Preprocess for toc_best_fares_block_content().
 */
function toc_sr_preprocess_toc_best_fares_block_content(array &$vars) {
  $vars["card_button_status"] = 'success';
  $vars["button_size"] = 'btn-lg';
  $vars["status"] = 'outline-light btn-lg px-2 py-1 mb-1';
}

/**
 * Preprocess for tls_booking_form_exposed().
 */
function toc_sr_preprocess_tls_booking_form_exposed(array &$vars) {
  $vars['el']['button_type'] = 'btn-success';
  $vars['el']['button_size'] = 'btn-lg';
  $vars['el']['button_class'] = 'w-100 mb-0';
  $vars['el']['hide_display_suffix'] = TRUE;
  $vars['el']['button_text'] = "Find times and tickets <i class=\"fas fa-chevron-right\"></i>";
  $vars['el']['display_group_icons'] = [
    'date' => 'far fa-calendar-alt',
    'time' => 'far fa-clock',
    'passenger' => 'fas fa-user',
    'railcard' => 'fas fa-credit-card',
  ];
}

/**
 * Preprocess for tls_booking_form().
 */
function toc_sr_preprocess_tls_booking_form(array &$vars) {
  $vars['el']['button_type'] = 'btn-success';
}

function toc_sr_preprocess_tourism_hub_fares_block(&$vars) {
  $vars['header'] = $vars['title'];
  $vars['origins'] = $vars['items'];
  $theme_config = theme_get_setting('toc_sr_theme_settings');
  if (isset($theme_config['tourism_hub_fares_bg']) && !empty($theme_config['tourism_hub_fares_bg'])) {
    $vars['bg_image'] = _tm_misc_get_styled_images_from_media(
      $theme_config['tourism_hub_fares_bg'],
      'large',
      'field_media_image',
      'media',
      'image',
      'th-fares-bg'
    );
  }
}

/**
 * Preprocess for NRE LDB block.
 */
function toc_sr_preprocess_nre_ldb_block(&$vars) {
  $vars['element']['tabbed'] = TRUE;
  $vars['element']['tabs_style'] = 'right';
  $vars['element']['style'] = '2';
  $vars['element']['hide_auto_update'] = 1;
  $vars['element']['last_updated_text'] = 'Live times at: ';
  $vars['element']['row_limit'] = 10;
  $vars['element']['preview_limit'] = 4;
  $vars['element']['next_limit'] = 10;
}

/**
 * Preprocess for NRE LDB stations.
 */
function toc_sr_preprocess_nre_ldb_stations(&$vars) {
  $vars['element']['no_journeys_message'] = 'There are no trains departing or arriving within the next two hours.';
}

/**
 * Preprocess card_blocks by machine name.
 *
 * This is above the fold, so we are giving the images 'eager' not 'lazy'.
 */
function toc_sr_preprocess_card_block__front_mc_cards(&$vars) {
  $entities = $vars['element']['entities_raw'];
  $items = [];
  foreach ($entities as $entity) {
    /** @var \Drupal\eck\Entity\EckEntity $entity */
    $extras = $entity->__get('extras') ?? [];
    $extras['card_image_loading'] = 'eager';
    $entity->__set('extras', $extras);
    $view_builder = \Drupal::entityTypeManager()->getViewBuilder('card_block');
    $items[] = $view_builder->view($entity);
  }
  if (!empty($items) && ($vars['batch'] >= 1)) {
    $vars['items'] = $items;
    $vars['slides'] = array_chunk($items, $vars['batch']);
  }
}
