<?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_lnr_preprocess_tls_booking_form(&$vars) {
  // Simply override the button style for the form.
  $vars['el']['button_type'] = 'btn-default';
}

function toc_lnr_preprocess_tls_booking_widget(&$vars) {
  // Simply override the button style for the form.
  $vars['button_type'] = 'btn-default';
}

/**
 * Preprocess for toc_best_fares_block_content().
 *
 * @param array $vars
 *   Theme variables.
 */
function toc_lnr_preprocess_toc_best_fares_block_content(array &$vars) {
  $vars["card_button_status"] = 'secondary';
  $vars["status"] = 'default';
}

/**
 * Preprocess for page_banner_random().
 */
function toc_lnr_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;
    }
  }
}

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

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

/**
 * Preprocess for buy_tickets_header().
 *
 * @param array $vars
 *   Theme variables.
 */
function toc_lnr_preprocess_buy_tickets_header(array &$vars) {
  // Enable Plan Journey form.
  $vars['plan_journey_form'] = $vars['plan_journey'] ?? NULL;
}

function toc_lnr_preprocess_journey_check_updates_items(array &$vars) {
  if (empty($vars['element']->getItems())) {
    $vars['top_message'] = [
      'text' => 'No problems reported',
      'icon_left' => 'far fa-info-circle',
    ];
  }
}

function toc_lnr_preprocess_journey_check_summary_status(array &$vars) {
  $vars['element']->setStatusText('Travel updates');
}

/**
 * Preprocess hook for link blocks - Advanced tickets with latest news.
 */
function toc_lnr_preprocess_link_block__latest_news_header(&$vars) {
  $template_vars = [
    'link_scope' => 'readmore',
    'block_class' => 'bl-vl cbelianr flex-fill mb-1',
    'header_tag' => 'h2',
    'readmore' => 'More news',
    'readmore_icon_right' => 'fas fa-chevron-right',
  ];
  _toc_master_preprocess_link_block($vars, $template_vars);
}

/**
 * Preprocess hook for link blocks - Advanced tickets with latest news.
 */
function toc_lnr_preprocess_link_block__adv_tickets_latest_news(&$vars) {
  $template_vars = [
    'link_scope' => 'block',
    'block_class' => 'bl-vk cbelianr flex-fill',
    'header_tag' => 'h3',
  ];
  _toc_master_preprocess_link_block($vars, $template_vars);
}
