<?php

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

use Drupal\Core\Template\Attribute;
use Drupal\nre_stations_entity_type\Service\Constants as NREStationEntityConstants;

/**
 * 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;
}

function _toc_sr_create_list($items, $prefix = '', $suffix = '') {
  $middle = implode($suffix . '</li><li>'. $prefix, $items);
  return \Drupal\Core\Render\Markup::create('<ul><li>'. $prefix . $middle . $suffix .'</li></ul>');
}

function _toc_sr_get_pickup_types_list($entity, $entityFields, $prefix) {
  $items = _tm_misc_get_field_content($entity, $entityFields::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 = \Drupal\Core\Render\Markup::create($content . $popover);
    }
  }
  return $content;
}

function _toc_sr_create_available_facility_content_with_popup($entity, $entityFields, $field) {
  $facility =  _tm_misc_get_field_content($entity, $field, 'reference');
  if ($facility) {
    $content = _tm_misc_get_field_content($facility, $entityFields::PARAGRAPH_AVAILABLE_FACILITY_FIELD_OPEN, 'render');
    if (!$content) {
      $content = _tm_misc_get_field_content($facility, $entityFields::PARAGRAPH_AVAILABLE_FACILITY_FIELD_AVAILABLE, 'render');
    }
    if ($content) {
      $popup = _tm_misc_get_field_content($facility, $entityFields::PARAGRAPH_AVAILABLE_FACILITY_FIELD_ANNOTATION, 'render');
      if (!$popup) {
        $popup = _tm_misc_get_field_content($facility, $entityFields::PARAGRAPH_AVAILABLE_FACILITY_FIELD_LOCATION, 'render');
      }
      if ($popup) {
        $popover = _tm_misc_format_popover_button($popup, 'render');
        $content = \Drupal\Core\Render\Markup::create($content . $popover);
      }
      return $content;
    }
  }
  return FALSE;
}

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

function _toc_sr_create_service_content_with_popup($entity, $entityFields, $field, $content_field = NULL) {
  $service =  _tm_misc_get_field_content($entity, $field, 'reference');
  if ($service) {
    if (is_null($content_field)) {
      $content_field = $entityFields::PARAGRAPH_SERVICE_FIELD_AVAILABLE;
    }
    return _toc_sr_create_content_with_popup($service, $content_field, $entityFields::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'];
  /** @var \Drupal\nre_stations_entity_type\Service\Constants $entityFields **/
  $entityFields = \Drupal::service('nre_stations_entity_type.constants');
  $station_crs = _tm_misc_get_field_content($entity, $entityFields::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['second_info_accordion_title'] = 'Transport links';

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

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

  // If there is a gallery and nearest stations map not available, make the map smaller.
  if (!(empty($vars['gallery']) || $entity->hasField('field_computed_nres_nearby_stations_map'))) {
    $leaflet_map_format = _tm_leaflet_format(15, 0, 0, [], 200);
    $vars['map'] = _tm_misc_get_field_content($entity, $entityFields::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, NREStationEntityConstants $entityFields): array {
  $rows = [];
  $sub_rows = [];
  $ticket_office = _tm_misc_get_field_content($entity, $entityFields::FIELD_FARES_TICKET_OFFICE, 'reference');
  if ($ticket_office) {
    $sub_rows = _tm_append_table_row($sub_rows, $entityFields::PARAGRAPH_AVAILABLE_FACILITY_FIELD_OPEN, TRUE, 'Opening hours', $ticket_office, 'field');
    $sub_rows = _tm_append_table_row($sub_rows, $entityFields::PARAGRAPH_AVAILABLE_FACILITY_FIELD_LOCATION, TRUE, 'Location', $ticket_office, 'field');
  }
  $sub_rows = _tm_append_table_row($sub_rows, $entityFields::FIELD_FARES_TICKET_MACHINE, TRUE, 'Ticket machines', $entity, 'field');
  $collection = _tm_misc_get_field_content($entity, $entityFields::FIELD_FARES_PREPURCHASE_COLLECTION, 'reference');
  if ($collection) {
    $items = _toc_sr_get_pickup_types_list($collection, $entityFields, '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, $entityFields, $entityFields::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, $entityFields, $entityFields::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, $entityFields::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, $entityFields::FIELD_FARES_SMARTCARD_ISSUED, TRUE, 'Smartcards issued at this station', $entity, 'field');
  $topup = _tm_misc_get_field_content($entity, $entityFields::FIELD_FARES_SMARTCARD_TOPUP, 'reference');
  if ($topup) {
    $items = _toc_sr_get_pickup_types_list($topup, $entityFields, '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, $entityFields::FIELD_FARES_SMARTCARD_COMMENTS, TRUE, 'Smartcard tickets', $entity, 'field');
  $sub_rows = _tm_append_table_row($sub_rows, $entityFields::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, NREStationEntityConstants $entityFields): array {
  $rows = [];
  $items = _tm_misc_get_field_content($entity, $entityFields::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, $entityFields::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, $entityFields, $entityFields::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, $entityFields, $entityFields::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, $entityFields, $entityFields::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, $entityFields::FIELD_PASSENGER_SERVICES_CUSTOMER_SERVICE, 'reference');
  if ($customer_service) {
    $rows = _tm_append_table_row($rows, $entityFields::PARAGRAPH_SERVICE_FIELD_ANNOTATION, TRUE, 'Customer services notes', $customer_service, 'field');
  }
  $content = _toc_sr_create_service_content_with_popup($entity, $entityFields, $entityFields::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, $entityFields, $entityFields::FIELD_PASSENGER_SERVICES_LOST_PROPERTY, $entityFields::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, $entityFields, $entityFields::FIELD_STATION_FACILITIES_TROLLEYS);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Trolleys');
  }
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_STAFFING_CCTV, TRUE, 'CCTV at the station', $entity, 'field');
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_accessibility_support($entity, NREStationEntityConstants $entityFields): array {
  $rows = [];
  $sub_rows = [];
  $content = _toc_sr_create_content_with_popup($entity, $entityFields::FIELD_ACCESSIBILITY_STEP_FREE_ACCESS_COVERAGE, $entityFields::FIELD_ACCESSIBILITY_STEP_FREE_ACCESS_ANNOTATION);
  if ($content) {
    $sub_rows = _tm_append_table_row($sub_rows, $content, TRUE, 'CCTV at the station');
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, $entityFields, $entityFields::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, $entityFields, $entityFields::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, $entityFields, $entityFields::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, $entityFields::FIELD_ACCESSIBILITY_INDUCTION_LOOP, TRUE, 'Induction loop', $entity, 'field');
  $content = _toc_sr_create_available_facility_content_with_popup($entity, $entityFields, $entityFields::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, $entityFields, $entityFields::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, $entityFields, $entityFields::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, $entityFields, $entityFields::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, $entityFields, $entityFields::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, $entityFields, $entityFields::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, $entityFields, $entityFields::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, $entityFields, $entityFields::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, $entityFields::FIELD_INTERCHANGE_CAR_PARKS, 'reference', [], -1);
  if ($car_parks) {
    $accessible_spaces = 'Not specified';
    foreach ($car_parks as $car_park) {
      $spaces = _tm_misc_get_field_content($car_park, $entityFields::PARAGRAPH_CAR_PARK_FIELD_CCTV);
      if ($spaces !== '0') {
        $accessible_spaces = $spaces;
        break;
      }
    }
    $sub_rows = _tm_append_table_row($sub_rows, $accessible_spaces, TRUE, 'Accessible spaces');
  }
  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, $entityFields, $entityFields::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, $entityFields, $entityFields::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, $entityFields::FIELD_ACCESSIBILITY_HELPLINE, 'reference');
  if ($helpline) {
    $content = _toc_sr_create_content_with_popup($helpline, 'Yes', $entityFields::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, NREStationEntityConstants $entityFields): array {
  $rows = [];
  $content = _toc_sr_create_available_facility_content_with_popup($entity, $entityFields, $entityFields::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, $entityFields, $entityFields::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, $entityFields, $entityFields::FIELD_STATION_FACILITIES_BABY_CHANGE);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Baby changing');
  }
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_lounges_waiting_rooms($entity, NREStationEntityConstants $entityFields): array {
  $rows = [];
  $content = _toc_sr_create_available_facility_content_with_popup($entity, $entityFields, $entityFields::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, $entityFields, $entityFields::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, $entityFields, $entityFields::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, NREStationEntityConstants $entityFields): array {
  $rows = [];
  $content = _toc_sr_create_available_facility_content_with_popup($entity, $entityFields, $entityFields::FIELD_ACCESSIBILITY_IMPAIRED_MOBILITY_SET_DOWN);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Impaired mobility set down/pick up points available');
  }
  $car_parks = _tm_misc_get_field_content($entity, $entityFields::FIELD_INTERCHANGE_CAR_PARKS, 'reference', [], -1);
  if ($car_parks) {
    $has_cctv = 'No';
    foreach ($car_parks as $car_park) {
      $cctv = _tm_misc_get_field_content($car_park, $entityFields::PARAGRAPH_CAR_PARK_FIELD_CCTV);
      if ($cctv === '1') {
        $has_cctv = 'Yes';
        break;
      }
    }
    $rows = _tm_append_table_row($rows, $has_cctv, TRUE, 'Car park CCTV');
  }
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_shopping($entity, NREStationEntityConstants $entityFields): array {
  $rows = [];
  $buffet =  _tm_misc_get_field_content($entity, $entityFields::FIELD_STATION_FACILITIES_STATION_BUFFET, 'reference');
  if ($buffet) {
    // @todo - how do we tell this is yes?
    $content = _toc_sr_create_content_with_popup($buffet, 'Yes', $entityFields::PARAGRAPH_AVAILABLE_FACILITY_FIELD_ANNOTATION, FALSE);
    if ($content) {
      $rows = _tm_append_table_row($rows, $content, TRUE, 'Refreshment facilities');
    }
  }
  $content = _toc_sr_create_available_facility_content_with_popup($entity, $entityFields, $entityFields::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, NREStationEntityConstants $entityFields): array {
  $rows = [];
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_STATION_FACILITIES_TELEPHONES_EXIST, TRUE, 'Pay phones', $entity, 'field');
  $content = _toc_sr_create_available_facility_content_with_popup($entity, $entityFields, $entityFields::FIELD_STATION_FACILITIES_WIFI);
  if ($content) {
    $rows = _tm_append_table_row($rows, $content, TRUE, 'Public Wi-Fi');
  }
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_sr_nre_station_accordion_content_cycling($entity, NREStationEntityConstants $entityFields): array {
  $rows = [];
  $sub_rows = [];
  $content = _tm_misc_get_field_content($entity, $entityFields::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, $entityFields::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, $entityFields::FIELD_INTERCHANGE_CYCLE_STORAGE_LOCATION, TRUE, 'Location', $entity, 'field');
  $sub_rows = _tm_append_table_row($sub_rows, $entityFields::FIELD_INTERCHANGE_CYCLE_STORAGE_SHELTERED, TRUE, 'Sheltered', $entity, 'field');
  $sub_rows = _tm_append_table_row($sub_rows, $entityFields::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, $entityFields::FIELD_INTERCHANGE_CYCLE_HIRE, TRUE, 'Cycle hire', $entity, 'field');
  $url = \Drupal\Core\Url::fromUri('https://www.nationalrail.co.uk/on-the-train/train-travel-with-bicycles');
  $link = \Drupal\Core\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, NREStationEntityConstants $entityFields): array {
  $content = _tm_misc_get_field_content($entity, $entityFields::FIELD_INTERCHANGE_RAIL_REPLACEMENT_ANNOTATION, 'render');
  if ($content) {
    return [
      '#type' => 'markup',
      '#markup' => $content,
    ];
  }
  return [];
}

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

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

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

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

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

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

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