<?php

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

use Drupal\Core\Link;
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;
use Drupal\nre_stations_entity_type\Constants as NREStationEntityConstants;
use Drupal\nre_train_operating_companies\Entity\NreTrainOperatingCompanyEckEntity;

/**
 * Preprocess hook for eck_entity__link_block__$ID().
 */
function toc_sr_preprocess_eck_entity__link_block__smartcard(&$vars) {
  $vars['block_class'] = 'bl-vh cbelianr bl-lilfr py-lg-2 px-lg-3 mb-4 sr-lb-signup';
  $vars['title_display'] = 'noflex';
}

/**
 * Preprocess hook for node__dog_offer().
 */
function toc_sr_preprocess_node__dog_offer(&$vars) {
  _toc_sr_preprocess_offers($vars, 'dog_offer');
}

/**
 * Preprocess hook for node__dog_offer().
 */
function toc_sr_preprocess_node__offer(&$vars) {
  _toc_sr_preprocess_offers($vars, 'offer');
}

/**
 * Preprocess for both types of offer nodes.
 *
 * @param $vars
 *
 * @return void
 */
function _toc_sr_preprocess_offers(&$vars, $type = '') {
  // Here we are formatting the back link. Everything else is from TOC Master.
  $entity = $vars['node'];
  $offers_config = \Drupal::service('config.factory')->getEditable('offers_content_type.settings');
  $back_link = $offers_config->get('offer_listings_path');
  $offer_category_terms_string = _tm_misc_get_field_content($entity, 'field_of_category');
  $tags = _tm_misc_format_tags_views_filter_links($offer_category_terms_string, $back_link, 'ocat');
  $vars['tags'] = $tags;
  // Hide Intro text for offers as the template has logic to override main.
  $allowed = ['br', 'p', 'ul', 'ol', 'li', 'a', 'em', 'strong', 'h2'];
  $text_format = _tm_text_format($allowed, 0);
  $main_text = _tm_misc_get_field_content($entity, 'field_of_long_description', 'render', $text_format);
  if ($type == 'offer') {
    $vars['intro_text'] = FALSE;
  }
  $vars['main_text'] = $main_text;
}

/**
 * Preprocess hook for eck_entity__the_list_events().
 * Used for the event place.
 */
function toc_sr_preprocess_eck_entity__the_list_events(&$vars) {
  // SR currently don't want to show the booking link button because
  // the layout doesn't work when there is two buttons and lots of categories
  // and they say it isn't needed
  $vars['booking_link_url'] = FALSE;
}

function _toc_sr_split_words_in_array($items) {
  foreach ($items as $i => $item) {
    $items[$i] = preg_replace('/([a-z])([A-Z])/', '$1 $2', $item);
  }
  return $items;
}

/**
 * Create a html <UL> list from an array of strings.
 *
 * @param array $items
 *   Array of items that are to be rendered into a list.
 * @param string $prefix
 *   String to prefix every list item with.
 * @param string $suffix
 *   String to suffix every list item with.
 *
 * @return \Drupal\Component\Render\MarkupInterface|string
 *   Returns html list.
 */
function _toc_sr_create_list(array $items, $prefix = '', $suffix = '') {
  if ($items) {
    $middle = implode($suffix . '</li><li>' . $prefix, $items);
    return Markup::create('<ul><li>' . $prefix . $middle . $suffix . '</li></ul>');
  }
  return '';
}

function _toc_sr_get_pickup_types_list($entity, $prefix) {
  $items = _tm_misc_get_field_content($entity, NREStationEntityConstants::PARAGRAPH_TICKET_PICKUP_FIELD_FACILITIES, 'render', [], -1);
  $types = [];
  if ($items) {
    $items = _toc_sr_split_words_in_array($items);
    foreach ($items as $item) {
      if ($item === 'Not Available') {
        $types = [];
        break;
      }
      $types[] = $item;
    }
  }
  if ($types) {
    $types = _toc_sr_create_list($types, $prefix);
  }
  return $types;
}


function _toc_sr_create_content_with_popup($entity, $content, $popup_field, $is_field = TRUE) {
  if ($is_field) {
    $content = _tm_misc_get_field_content($entity, $content, 'render');
  }
  if ($content) {
    $popup = _tm_misc_get_field_content($entity, $popup_field, 'render');
    if ($popup) {
      $popover = _tm_misc_format_popover_button($popup, 'render');
      $content = Markup::create('<div class="d-flex justify-content-between align-items-start"><div>' . $content . '</div>' . $popover . '</div>');
    }
  }
  return $content;
}

function _toc_sr_create_available_facility_content_with_popup($entity, $field) {
  $facility = _tm_misc_get_field_content($entity, $field, 'reference');
  if ($facility) {
    $content = _tm_misc_get_field_content($facility, NREStationEntityConstants::PARAGRAPH_AVAILABLE_FACILITY_FIELD_OPEN, 'render');
    if (!$content) {
      $content = _tm_misc_get_field_content($facility, NREStationEntityConstants::PARAGRAPH_AVAILABLE_FACILITY_FIELD_AVAILABLE, 'render');
    }
    if ($content) {
      $popup = _tm_misc_get_field_content($facility, NREStationEntityConstants::PARAGRAPH_AVAILABLE_FACILITY_FIELD_ANNOTATION, 'render');
      if (!$popup) {
        $popup = _tm_misc_get_field_content($facility, NREStationEntityConstants::PARAGRAPH_AVAILABLE_FACILITY_FIELD_LOCATION, 'render');
      }
      if ($popup) {
        $popover = _tm_misc_format_popover_button($popup, 'render');
        $content = Markup::create('<div class="d-flex justify-content-between align-items-start"><div>' . $content . '</div>' . $popover . '</div>');
      }
      return $content;
    }
  }
  return FALSE;
}

function _toc_sr_create_opening_hours_content_with_popup($entity, $field) {
  $hours = _tm_misc_get_field_content($entity, $field, 'reference');
  if ($hours) {
    $content = _tm_misc_get_field_content($hours, NREStationEntityConstants::PARAGRAPH_OPENING_HOURS_FIELD_DAY_AND_TIME_AVAILABILITY, 'render', [], -1);
    if ($content) {
      $content = _toc_sr_create_list($content);
    }
    return _toc_sr_create_content_with_popup($hours, $content, NREStationEntityConstants::PARAGRAPH_OPENING_HOURS_FIELD_ANNOTATION, FALSE);
  }
  return FALSE;
}

function _toc_sr_create_service_content_with_popup($entity, $field, $content_field = NULL) {
  $service = _tm_misc_get_field_content($entity, $field, 'reference');
  if ($service) {
    if (is_null($content_field)) {
      $content_field = NREStationEntityConstants::PARAGRAPH_SERVICE_FIELD_AVAILABLE;
    }
    return _toc_sr_create_content_with_popup($service, $content_field, NREStationEntityConstants::PARAGRAPH_SERVICE_FIELD_ANNOTATION);
  }
  return FALSE;
}

/**
 * Preprocess hook for eck_entity__nres_ee_nre_station().
 */
function toc_sr_preprocess_eck_entity__nres_ee_nre_station(&$vars) {
  $entity = $vars['eck_entity'];
  $station_crs = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_CRS);
  // Use the extended header version.
  $vars['header_use_extended'] = TRUE;
  $vars['top_layout'] = 'address_and_link';
  $vars['header_title_crs'] = TRUE;

  // Data for extended header.
  $ticket_office_dates = [];
  $office = _tm_misc_get_field_content($entity, 'field_nres_eeb_par_ticket_office', 'reference');
  if ($office) {
    $times = _tm_misc_get_field_content($office, 'field_nres_eeb_par_open', 'reference');
    if ($times) {
      $ticket_office_dates = _tm_misc_get_field_content($times, 'field_nres_eeb_par_day_and_time', 'render', [], -1);
    }
  }

  $vars['checklist'] = [];
  $station_features = nre_stations_entity_type_get_station_features($station_crs);
  if ($station_features) {
    $num_station_features = count($station_features['features']);
    $num_ticket_office_dates = is_array($ticket_office_dates) ? count($ticket_office_dates) : 0;
    $break = (int) floor(($num_station_features + $num_ticket_office_dates + 1) / 2) - $num_ticket_office_dates;
    foreach ($station_features['features'] as $i => $feature) {
      $item = [
        'text' => $feature,
        'icon' => 'far fa-check',
      ];
      if ($feature == 'Ticket office') {
        $item['subtext'] = $ticket_office_dates;
      }
      if ($i === $break) {
        $item['break'] = TRUE;
      }
      $vars['checklist'][] = $item;
    }
    $vars['checklist'][] = [
      'url' => 'https://accessmap.nationalrail.co.uk/',
      'text' => 'National Rail Accessibility Map',
      'icon' => 'far fa-wheelchair',
    ];
  }

  $vars['ldb_use_exposed'] = TRUE;
  $vars['ldb_position'] = 'before';
  $vars['ldb_exposed_title'] = '';
  $vars['show_buy_tickets_button'] = TRUE;
  $vars['buy_tickets_button_class'] = 'btn-outline-custom-color mt-4 border-2';
  $vars['intro_classes'] = 'intro-primary';

  $vars['timetables_title'] = 'Relevant timetables';
  $vars['timetables'] = views_embed_view('timetables_entity', 'station_pages');

  $vars['info_accordion_title'] = 'Station features and facilities';
  $vars['info_accordion_copy_prefix_classes'] = 'mt-0 h5 ffn';
  $vars['second_info_accordion_title'] = 'Transport links';

  $operator = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_STATION_OPERATOR);

  if ($operator && $company = NreTrainOperatingCompanyEckEntity::loadByAtoc($operator)) {
    $vars['info_accordion_copy_prefix'] = 'Station managed by:';
    $vars['info_accordion_copy_company'] = $company->getTitle();
  }

  $leaflet_map_format = _tm_leaflet_format(15, 0, 0, [], 200);
  $vars['map_show_short'] = TRUE;

  if (!(empty($vars['gallery']) || $entity->hasField('field_computed_nres_nearby_stations_map'))) {
    $vars['map'] = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_LATLON, 'render', $leaflet_map_format);
  }

  $vars['#attached']['library'][] = 'ui_patterns/libraries.bs_popover';
  $vars['#attached']['library'][] = 'ui_patterns/bs_popover.popover_lib';
}

function _toc_sr_nre_station_accordion_content_ticket_buying_collection($entity): array {
  $rows = [];
  $sub_rows = [];
  $ticket_office = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_FARES_TICKET_OFFICE, 'reference');
  if ($ticket_office) {
    $sub_rows = _tm_append_table_row($sub_rows, NREStationEntityConstants::PARAGRAPH_AVAILABLE_FACILITY_FIELD_OPEN, TRUE, 'Opening hours', $ticket_office, 'field');
    $sub_rows = _tm_append_table_row($sub_rows, NREStationEntityConstants::PARAGRAPH_AVAILABLE_FACILITY_FIELD_LOCATION, TRUE, 'Location', $ticket_office, 'field');
  }
  $sub_rows = _tm_append_table_row($sub_rows, NREStationEntityConstants::FIELD_FARES_TICKET_MACHINE, TRUE, 'Ticket machines', $entity, 'field');
  $collection = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_FARES_PREPURCHASE_COLLECTION, 'reference');
  if ($collection) {
    $items = _toc_sr_get_pickup_types_list($collection, 'Collect tickets at ');
    if ($items) {
      $sub_rows = _tm_append_table_row($sub_rows, $items, TRUE, 'Collect tickets bought online');
    }
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_ACCESSIBLE_TICKET_MACHINES);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Accessible ticket machines');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_HEIGHT_ADJUSTED_TICKET_OFFICE_COUNTER);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Height adjusted ticket office counter');
  }
  $sub_rows = _tm_append_table_row($sub_rows, NREStationEntityConstants::FIELD_ACCESSIBILITY_INDUCTION_LOOP, TRUE, 'Induction loop', $entity, 'field');
  if (count($sub_rows)) {
    $rows[] = [
      [
        'data' => 'Ticket Office',
        'colspan' => '2',
        'header' => TRUE,
        'class' => 'header',
      ],
    ];
    $rows = array_merge($rows, $sub_rows);
  }
  $sub_rows = [];
  $sub_rows = _tm_append_table_row($sub_rows, NREStationEntityConstants::FIELD_FARES_SMARTCARD_ISSUED, TRUE, 'Smartcards issued at this station', $entity, 'field');
  $topup = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_FARES_SMARTCARD_TOPUP, 'reference');
  if ($topup) {
    $items = _toc_sr_get_pickup_types_list($topup, 'Load smartcard at ');
    if ($items) {
      $sub_rows = _tm_append_table_row($sub_rows, $items, TRUE, 'Smartcard tickets');
    }
  }
  $sub_rows = _tm_append_table_row($sub_rows, NREStationEntityConstants::FIELD_FARES_SMARTCARD_COMMENTS, TRUE, 'Smartcard tickets', $entity, 'field');
  $sub_rows = _tm_append_table_row($sub_rows, NREStationEntityConstants::FIELD_FARES_SMARTCARD_VALIDATOR, TRUE, 'Smartcard validators', $entity, 'field');
  if (count($sub_rows)) {
    $rows[] = [
      [
        'data' => 'Smartcards',
        'colspan' => '2',
        'header' => TRUE,
        'class' => 'header',
      ],
    ];
    $rows = array_merge($rows, $sub_rows);
  }
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_help_support($entity): array {
  $rows = [];
  $items = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_INFORMATION_SYSTEMS_INFORMATION_AVAILABLE_FROM_STAFF, 'render', [], -1);
  if ($items) {
    $items = _toc_sr_create_list($items);
    $rows = _tm_append_table_row($rows, $items, TRUE, 'Information available from staff');
  }
  $items = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_INFORMATION_SYSTEMS_CIS, 'render', [], -1);
  if ($items) {
    $items = _toc_sr_create_list(_toc_sr_split_words_in_array($items));
    $rows = _tm_append_table_row($rows, $items, TRUE, 'Customer information screens');
  }
  $content = _toc_sr_create_opening_hours_content_with_popup($entity, NREStationEntityConstants::FIELD_INFORMATION_SYSTEMS_INFORMATION_SERVICES_OPEN);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Information services opening times');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_STAFF_HELP_AVAILABLE);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Staff help available');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_INFORMATION_SYSTEMS_CUSTOMER_HELP_POINTS);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Customer help points');
  }
  $customer_service = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_PASSENGER_SERVICES_CUSTOMER_SERVICE, 'reference');
  if ($customer_service) {
    $rows = _tm_append_table_row($rows, NREStationEntityConstants::PARAGRAPH_SERVICE_FIELD_ANNOTATION, TRUE, 'Customer services notes', $customer_service, 'field');
  }
  $content = _toc_sr_create_service_content_with_popup($entity, NREStationEntityConstants::FIELD_PASSENGER_SERVICES_LEFT_LUGGAGE);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Luggage storage');
  }
  $content = _toc_sr_create_service_content_with_popup($entity, NREStationEntityConstants::FIELD_PASSENGER_SERVICES_LOST_PROPERTY, NREStationEntityConstants::PARAGRAPH_SERVICE_FIELD_OPEN);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Lost property');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_STATION_FACILITIES_TROLLEYS);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Trolleys');
  }
  $rows = _tm_append_table_row($rows, NREStationEntityConstants::FIELD_STAFFING_CCTV, TRUE, 'CCTV at the station', $entity, 'field');
  $content = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_INDUCTION_LOOP);
  if ($content === '1') {
    $content = 'Induction loops are at designated listening areas';
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Induction loop availability');
  }
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_accessibility_support($entity): array {
  $rows = [];
  $sub_rows = [];
  $content = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_STEP_FREE_ACCESS_COVERAGE);
  if ($content) {
    switch ($content) {
      case 'partialStation':
        $content = 'This station has a degree of step-free access to the platform, which may be in both directions or in one direction only - please check details.';
        break;

      case 'wholeStation':
        $content = 'This station has step-free access to all platforms / the platform.';
        break;

      case 'noPartOfStation':
        $content = 'This station does not have step-free access to any platform.';
        break;

      default:
        $content = '';
    }
  }
  if ($content) {
    $popup = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_STEP_FREE_ACCESS_ANNOTATION, 'render');
    if ($popup) {
      $popover = _tm_misc_format_popover_button($popup, 'render');
      $content = Markup::create('<div class="d-flex justify-content-between align-items-start"><div>' . $content . '</div>' . $popover . '</div>');
    }
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Step free access');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_TICKET_GATES);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Ticket barriers and gates');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_ACCESSIBLE_TICKET_MACHINES);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Accessible ticket machines');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_HEIGHT_ADJUSTED_TICKET_OFFICE_COUNTER);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Height adjusted ticket office counter');
  }
  $sub_rows = _tm_append_table_row($sub_rows, NREStationEntityConstants::FIELD_ACCESSIBILITY_INDUCTION_LOOP, TRUE, 'Induction loop', $entity, 'field');
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_RAMP_FOR_TRAIN_ACCESS);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Ramp for train access');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_NATIONAL_KEY_TOILETS);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Accessible toilets');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_STATION_FACILITIES_WAITING_ROOM);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Waiting rooms');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_STATION_FACILITIES_SEATED_AREA);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Seating area');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_WHEELCHAIRS_AVAILABLE);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Wheelchairs available');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_STATION_FACILITIES_TROLLEYS);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Trolleys');
  }
  if (count($sub_rows)) {
    $rows[] = [
      [
        'data' => 'Station accessibility',
        'colspan' => '2',
        'header' => TRUE,
        'class' => 'header',
      ],
    ];
    $rows = array_merge($rows, $sub_rows);
  }
  $sub_rows = [];
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_ACCESSIBLE_TAXIS);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Accessible taxis here');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_IMPAIRED_MOBILITY_SET_DOWN);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Impaired mobility set down/pick-up point');
  }
  $car_parks = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_INTERCHANGE_CAR_PARKS, 'reference', [], -1);
  if ($car_parks) {
    $accessible_space_info = '';
    $accessible_equipment_info = '';
    $multiple = count($car_parks) > 1;
    $num = 1;
    foreach ($car_parks as $car_park) {
      $name = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_NAME);
      if (!$name) {
        $name = 'Car Park ' . $num;
      }
      $accessible_spaces = 'Not specified';
      $spaces = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_NUMBER_ACCESSIBLE_SPACES);
      if ($spaces !== '0') {
        $accessible_spaces = $spaces . ' Spaces';
        $popup = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_ACCESSIBLE_SPACES_NOTE, 'render');
        if ($popup) {
          $popover = _tm_misc_format_popover_button($popup, 'render');
          $accessible_spaces = Markup::create('<div class="d-flex justify-content-between align-items-start"><div>' . $accessible_spaces . '</div>' . $popover . '</div>');
        }
      }
      if ($multiple) {
        $accessible_spaces = '<p><strong>' . $name . '</strong></p><p>' .
          $accessible_spaces . '</p>';
      }
      $accessible_space_info .= $accessible_spaces;
      $accessible_equipment = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_ACCESSIBLE_CAR_PARK_EQUIPMENT);
      if ($accessible_equipment === '1') {
        $accessible_equipment = 'Yes';
      }
      elseif ($accessible_equipment === '0') {
        $accessible_equipment = 'No';
      }
      if ($accessible_equipment) {
        $popup = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_ACCESSIBLE_CAR_PARK_EQUIPMENT_NOTE, 'render');
        if ($popup) {
          $popover = _tm_misc_format_popover_button($popup, 'render');
          $accessible_equipment = Markup::create('<div class="d-flex justify-content-between align-items-start"><div>' . $accessible_equipment . '</div>' . $popover . '</div>');
        }
        if ($multiple) {
          $accessible_equipment = '<p><strong>' . $name . '</strong></p><p>' .
            $accessible_equipment . '</p>';
        }
        $accessible_equipment_info .= $accessible_equipment;
      }
      $num++;
    }
    if ($accessible_space_info) {
      $accessible_space_info = Markup::create($accessible_space_info);
      $sub_rows = _tm_append_table_row($sub_rows, $accessible_space_info, TRUE, 'Accessible spaces');
    }
    if ($accessible_equipment_info) {
      $accessible_equipment_info = Markup::create($accessible_equipment_info);
      $sub_rows = _tm_append_table_row($sub_rows, $accessible_equipment_info, TRUE, 'Accessible car park equipment');
    }
  }
  if (count($sub_rows)) {
    $rows[] = [
      [
        'data' => 'Getting to and from the station',
        'colspan' => '2',
        'header' => TRUE,
        'class' => 'header',
      ],
    ];
    $rows = array_merge($rows, $sub_rows);
  }
  $sub_rows = [];
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_STAFF_HELP_AVAILABLE);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Staff help available');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_INFORMATION_SYSTEMS_CUSTOMER_HELP_POINTS);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Customer help points and meeting point');
  }
  $helpline = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_HELPLINE, 'reference');
  if ($helpline) {
    $content = _toc_sr_create_content_with_popup($helpline, 'Yes', NREStationEntityConstants::PARAGRAPH_SERVICE_FIELD_ANNOTATION, FALSE);
    if ($content) {
      $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Helpline');
    }
  }
  if (count($sub_rows)) {
    $rows[] = [
      [
        'data' => 'Staff help',
        'colspan' => '2',
        'header' => TRUE,
        'class' => 'header',
      ],
    ];
    $rows = array_merge($rows, $sub_rows);
  }
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_toilets_baby_changing($entity): array {
  $rows = [];
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_STATION_FACILITIES_TOILETS);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Toilets');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_NATIONAL_KEY_TOILETS);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Accessible toilets');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_STATION_FACILITIES_BABY_CHANGE);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Baby changing');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_STATION_FACILITIES_SHOWERS);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Showers');
  }
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_lounges_waiting_rooms($entity): array {
  $rows = [];
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_STATION_FACILITIES_WAITING_ROOM);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Waiting rooms');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_STATION_FACILITIES_SEATED_AREA);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Seating area');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_STATION_FACILITIES_FIRST_CLASS_LOUNGE);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'First class lounge');
  }
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_car_parking($entity): array {
  $rows = [];
  $car_parks = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_INTERCHANGE_CAR_PARKS, 'reference', [], -1);
  if ($car_parks) {
    $multiple = count($car_parks) > 1;
    $num = 1;
    foreach ($car_parks as $car_park) {
      if ($multiple) {
        $name = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_NAME);
        if (!$name) {
          $name = 'Car Park ' . $num;
        }
        $rows[] = [
          [
            'data' => $name,
            'colspan' => '2',
            'header' => TRUE,
            'class' => 'header',
          ],
        ];
      }
      $car_park_service_struc = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_SERVICE, 'reference');
      if ($car_park_service_struc) {
        $content = _tm_misc_get_field_content($car_park_service_struc, NREStationEntityConstants::PARAGRAPH_SERVICE_FIELD_OPERATOR_NAME, 'render');
        if ($content) {
          $contact_struc = _tm_misc_get_field_content($car_park_service_struc, NREStationEntityConstants::PARAGRAPH_SERVICE_FIELD_CONTACT_DETAILS, 'render');
          if ($contact_struc) {
            $popover = _tm_misc_format_popover_button($contact_struc, 'render');
            $content = Markup::create('<div class="d-flex justify-content-between align-items-start"><div>' . $content . '</div>' . $popover . '</div>');
          }
          $rows = _tm_append_table_row($rows, $content, TRUE, 'Operator');
        }
        $rows = _tm_append_table_row($rows, NREStationEntityConstants::PARAGRAPH_SERVICE_FIELD_OPEN, TRUE, 'Car parking availability', $car_park_service_struc, 'field');
      }
      $car_park_spaces = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_SPACES);
      if ($car_park_spaces != 0) {
        $rows = _tm_append_table_row($rows, $car_park_spaces, TRUE, 'Spaces');
      }
      $accessible_spaces = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_NUMBER_ACCESSIBLE_SPACES);
      if ($accessible_spaces != 0) {
        $popup = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_ACCESSIBLE_SPACES_NOTE, 'render');
        if ($popup) {
          $popover = _tm_misc_format_popover_button($popup, 'render');
          $accessible_spaces = Markup::create('<div class="d-flex justify-content-between align-items-start"><div>' . $accessible_spaces . '</div>' . $popover . '</div>');
        }
        $rows = _tm_append_table_row($rows, $accessible_spaces, TRUE, 'Accessible spaces');
      }
      $accessible_equipment = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_ACCESSIBLE_CAR_PARK_EQUIPMENT);
      if ($accessible_equipment === '1') {
        $accessible_equipment = 'Yes';
      }
      elseif ($accessible_equipment === '0') {
        $accessible_equipment = 'No';
      }
      if ($accessible_equipment !== FALSE) {
        $popup = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_ACCESSIBLE_CAR_PARK_EQUIPMENT_NOTE, 'render');
        if ($popup) {
          $popover = _tm_misc_format_popover_button($popup, 'render');
          $accessible_equipment = Markup::create('<div class="d-flex justify-content-between align-items-start"><div>' . $accessible_equipment . '</div>' . $popover . '</div>');
        }
        $rows = _tm_append_table_row($rows, $accessible_equipment, TRUE, 'Accessible car park equipment');
      }
      $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_ACCESSIBILITY_IMPAIRED_MOBILITY_SET_DOWN);
      if ($content) {
        $rows = _tm_append_table_row($rows, $content, TRUE, 'Impaired mobility set down/pick up points available');
      }
      $cctv = '';
      $cctv_value = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_CCTV);
      if ($cctv_value === '1') {
        $cctv = 'Yes';
      }
      elseif ($cctv_value === '0') {
        $cctv = 'No';
      }
      if ($cctv) {
        $rows = _tm_append_table_row($rows, $cctv, TRUE, 'Car park CCTV');
      }
      $details = [];
      $free = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_CHARGES_FREE);
      if ($free === '1') {
        $charges = 'Parking is free';
      }
      else {
        $charges = 'Parking charges apply';
        $hourly = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_CHARGES_PER_HOUR);
        if ($hourly) {
          $details[] = 'Rate per hour ' . $hourly;
        }
        $daily = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_CHARGES_DAILY);
        if ($daily) {
          $details[] = 'Rate per day ' . $daily;
        }
        $saturday = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_CHARGES_SATURDAY);
        if ($saturday) {
          $details[] = 'Rate on Saturday ' . $saturday;
        }
        $sunday = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_CHARGES_SUNDAY);
        if ($sunday) {
          $details[] = 'Rate on Sunday ' . $sunday;
        }
        $weekly = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_CHARGES_WEEKLY);
        if ($weekly) {
          $details[] = 'Rate per week ' . $weekly;
        }
        $monthly = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_CHARGES_MONTHLY);
        if ($monthly) {
          $details[] = 'Rate per month ' . $monthly;
        }
        $three_monthly = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_CHARGES_THREE_MONTHLY);
        if ($three_monthly) {
          $details[] = 'Rate per three months ' . $three_monthly;
        }
        $six_monthly = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_CHARGES_SIX_MONTHLY);
        if ($six_monthly) {
          $details[] = 'Rate per six months ' . $six_monthly;
        }
        $annual = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_CHARGES_ANNUAL);
        if ($annual) {
          $details[] = 'Rate per year ' . $annual;
        }
      }
      $note = _tm_misc_get_field_content($car_park, NREStationEntityConstants::PARAGRAPH_CAR_PARK_FIELD_CHARGES_NOTE);
      if ($note) {
        $details[] = $note;
      }
      if ($details) {
        $popup = implode('<br/>', $details);
        $popover = _tm_misc_format_popover_button($popup, 'render');
        $charges = Markup::create('<div class="d-flex justify-content-between align-items-start"><div>' . $charges . '</div>' . $popover . '</div>');
      }
      $rows = _tm_append_table_row($rows, $charges, TRUE, 'Parking charges');
      $num++;
    }
  }
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_shopping($entity): array {
  $rows = [];
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_STATION_FACILITIES_ATM_MACHINE);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'ATM');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_STATION_FACILITIES_STATION_BUFFET);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Refreshment facilities');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_STATION_FACILITIES_SHOPS);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Shops');
  }
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_wifi_phones($entity): array {
  $rows = [];
  $rows = _tm_append_table_row($rows, NREStationEntityConstants::FIELD_STATION_FACILITIES_TELEPHONES_EXIST, TRUE, 'Pay phones', $entity, 'field');
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_STATION_FACILITIES_WIFI);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Public Wi-Fi');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, NREStationEntityConstants::FIELD_STATION_FACILITIES_POST_BOX);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Post Boxes');
  }
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_cycling($entity): array {
  $rows = [];
  $sub_rows = [];
  $content = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_INTERCHANGE_CYCLE_STORAGE_SPACES);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'Spaces');
  }
  $cycle_storage_types = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_INTERCHANGE_CYCLE_STORAGE_TYPE, 'render', [], -1);
  if ($cycle_storage_types) {
    $cycle_storage_types = _toc_sr_create_list(_toc_sr_split_words_in_array($cycle_storage_types));
    $sub_rows = _tm_append_table_row($sub_rows, $cycle_storage_types, TRUE, 'Type of storage');
  }
  $sub_rows = _tm_append_table_row($sub_rows, NREStationEntityConstants::FIELD_INTERCHANGE_CYCLE_STORAGE_LOCATION, TRUE, 'Location', $entity, 'field');
  $sub_rows = _tm_append_table_row($sub_rows, NREStationEntityConstants::FIELD_INTERCHANGE_CYCLE_STORAGE_SHELTERED, TRUE, 'Sheltered', $entity, 'field');
  $sub_rows = _tm_append_table_row($sub_rows, NREStationEntityConstants::FIELD_INTERCHANGE_CYCLE_STORAGE_CCTV, TRUE, 'Cycle storage CCTV', $entity, 'field');
  if (count($sub_rows)) {
    $rows[] = [
      [
        'data' => 'Bicycle storage',
        'colspan' => '2',
        'header' => TRUE,
        'class' => 'header',
      ],
    ];
    $rows = array_merge($rows, $sub_rows);
  }
  $sub_rows = [];
  $sub_rows = _tm_append_table_row($sub_rows, NREStationEntityConstants::FIELD_INTERCHANGE_CYCLE_HIRE, TRUE, 'Cycle hire', $entity, 'field');
  $url = Url::fromUri('https://www.nationalrail.co.uk/on-the-train/train-travel-with-bicycles');
  $link = Link::fromTextAndUrl('Train travel with bicycles', $url);
  $build = $link->toRenderable();
  $content = \Drupal::service('renderer')->render($build);
  $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'More information');
  if (count($sub_rows)) {
    $rows[] = [
      [
        'data' => 'Bicycle hire',
        'colspan' => '2',
        'header' => TRUE,
        'class' => 'header',
      ],
    ];
    $rows = array_merge($rows, $sub_rows);
  }
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_rail_replacement($entity): array {
  $content = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_INTERCHANGE_RAIL_REPLACEMENT_ANNOTATION, 'render');
  if ($content) {
    return [
      '#type' => 'markup',
      '#markup' => $content,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_taxi($entity): array {
  $content = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_INTERCHANGE_TAXI_RANK, 'render');
  if ($content) {
    return [
      '#type' => 'markup',
      '#markup' => $content,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_bus($entity): array {
  $content = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_INTERCHANGE_ONWARD_TRAVEL, 'render');
  if ($content) {
    return [
      '#type' => 'markup',
      '#markup' => $content,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_underground($entity): array {
  $content = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_INTERCHANGE_METRO_SERVICES, 'render');
  if ($content) {
    return [
      '#type' => 'markup',
      '#markup' => $content,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_airport($entity): array {
  $content = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_INTERCHANGE_AIRPORT, 'render');
  if ($content) {
    return [
      '#type' => 'markup',
      '#markup' => $content,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_port($entity): array {
  $content = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_INTERCHANGE_PORT, 'render');
  if ($content) {
    return [
      '#type' => 'markup',
      '#markup' => $content,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_car_hire($entity): array {
  $content = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_INTERCHANGE_CAR_HIRE, 'render');
  if ($content) {
    return [
      '#type' => 'markup',
      '#markup' => $content,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_bicycle_hire($entity): array {
  $content = _tm_misc_get_field_content($entity, NREStationEntityConstants::FIELD_INTERCHANGE_CYCLE_HIRE, 'render');
  if ($content) {
    return [
      '#type' => 'markup',
      '#markup' => $content,
    ];
  }
  return [];
}

/**
 * Preprocess hook for eck_entity__card_block().
 */
function toc_sr_preprocess_eck_entity__card_block(&$vars) {
  $vars['item']['icon_top_right'] = TRUE;
}
