<?php

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

use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\nre_stations_entity_type\Entity\NreStationEckEntity;
use Drupal\nre_stations_entity_type\Service\Constants as NREStationEntityConstants;
use Drupal\Core\Url;
use Drupal\nre_train_operating_companies\Entity\NreTrainOperatingCompanyEckEntity;

/**
 * Preprocess hook for eck_entity__link_block__$ID().
 */
function toc_emr_preprocess_eck_entity__link_block__signup_link(&$vars) {
  $entity = $vars['elements']['#link_block'];
  $vars['block_class'] = 'bl-vh cbelianr bg-purple bl-lilfr py-lg-2 px-lg-3 mb-4 emr-lb-signup ';
  $vars['title_display'] = 'noflex';
}

/**
 * Preprocess hook for node__destination().
 *
 * This is loading in a custom block group that is enabled and rendering it
 * in the content. Ideally we would just load the blocks here.
 */
function toc_emr_preprocess_node__route(&$vars) {
  $vars['content'] = [];
  if ($routetemplateblock = _tm_misc_render_block('routetemplateblock')) {
    $vars['content'][] = $routetemplateblock;
  }
  if ($routefaqblock = _tm_misc_render_block('routefaqblock')) {
    $vars['content'][] = $routefaqblock;
  }
}

/**
 * Preprocess hook for node__destination().
 *
 * @todo We have bundle name class. We need to differentiate them between
 * GA and WMR
 */
function toc_emr_preprocess_node__destination(&$vars) {
  if (isset($vars['form'])) {
    unset($vars['form']);
  }

  $entity = $vars['node'];
  // FAQs, let's use Routes.
  $vars['faqs'] = _tm_misc_destination_attraction_faqs($entity, 'field_dest_par_faqs');
  // Default area map, no marker required.
  $leaflet_map_format = _tm_leaflet_format(12, 0, 0, ['iconType' => 'html']);
  $map = _tm_misc_get_field_content($entity, 'field_dest_ge_location', 'render', $leaflet_map_format);
  $vars['map'] = $map;
  // Stations, needed for cards and locations.
  $vars['stations'] = _tm_misc_destination_attraction_maps($entity, 'field_dest_tp_stations_crs');
  // Extra info.
  $text_format = _tm_text_format([], 0, 'input', 'full_html');
  $vars['extra'] = _tm_misc_get_field_content($entity, 'field_dest_stf_extra_content', 'render', $text_format);
  $vars['swap_sidebar_form'] = TRUE;
}

/**
 * Preprocess hook and content for tab.
 *
 * @param $entity
 * @param \Drupal\nre_stations_entity_type\Service\Constants $entityFields
 *
 * @return array
 * @see toc_master_preprocess_eck_entity__nres_ee_nre_station().
 *
 */
function _toc_emr_nre_station_accordion_content_fares($entity, NREStationEntityConstants $entityFields): array {
  $rows = [];
  $html = _toc_emr_render_available_facility_location($entity, $entityFields::FIELD_FARES_TICKET_OFFICE, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Ticket office information');
  $html = _toc_emr_render_available_facility_opening_times($entity, $entityFields::FIELD_FARES_TICKET_OFFICE, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Ticket office opening times');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_FARES_PREPURCHASE_COLLECTION, TRUE, 'Pre-purchase collection', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_FARES_TICKET_MACHINE, TRUE, 'Ticket machine', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_FARES_OYSTERCARD_ISSUED, TRUE, 'Oyster cards issued here', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_FARES_OYSTERCARD_TOPUP, TRUE, 'Top up Oyster cards here', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_FARES_USE_OYSTERCARD, TRUE, 'Use Oyster cards here', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_FARES_OYSTER_COMMENTS, TRUE, 'Oyster cards information', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_FARES_SMARTCARD_ISSUED, TRUE, 'Smartcards issued here', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_FARES_SMARTCARD_TOPUP, TRUE, 'Top up Smartcards here', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_FARES_SMARTCARD_VALIDATOR, TRUE, 'Validate Smartcards here', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_FARES_SMARTCARD_COMMENTS, TRUE, 'Smartcards information', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_FARES_TRAVELCARD, TRUE, 'Travel card', $entity, 'field');
  $html = _toc_emr_render_penalty_fares_train_operator($entity, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Penalty fares');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_FARES_PENALTY_FARES_URL, TRUE, 'Penalty fares URL', $entity, 'field');
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_emr_nre_station_accordion_content_passenger_services($entity, NREStationEntityConstants $entityFields): array {
  $rows = [];
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_PASSENGER_SERVICES_CUSTOMER_SERVICE, TRUE, 'Customer service information', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_PASSENGER_SERVICES_TELE_SALES, TRUE, 'Tele sales', $entity, 'field');
  $html = _toc_emr_render_service_available($entity, $entityFields::FIELD_PASSENGER_SERVICES_LEFT_LUGGAGE, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Left luggage');
  $html = _toc_emr_render_service_contact_details($entity, $entityFields::FIELD_PASSENGER_SERVICES_LEFT_LUGGAGE, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Left luggage contact details');
  $html = _toc_emr_render_service_opening_times($entity, $entityFields::FIELD_PASSENGER_SERVICES_LOST_PROPERTY, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Lost property opening times');
  $html = _toc_emr_render_service_contact_details($entity, $entityFields::FIELD_PASSENGER_SERVICES_LOST_PROPERTY, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Lost property contact details');
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_emr_nre_station_accordion_content_station_facilities($entity, NREStationEntityConstants $entityFields): array {
  $rows = [];
  $html = _toc_emr_render_available_facility_opening_times($entity, $entityFields::FIELD_STATION_FACILITIES_FIRST_CLASS_LOUNGE, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'First class lounge opening times');
  $html = _toc_emr_render_available_facility_annotation($entity, $entityFields::FIELD_STATION_FACILITIES_FIRST_CLASS_LOUNGE, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'First class lounge information');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_STATION_FACILITIES_SEATED_AREA, TRUE, 'Seated area available', $entity, 'field');
  $html = _toc_emr_render_available_facility_opening_times($entity, $entityFields::FIELD_STATION_FACILITIES_WAITING_ROOM, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Waiting room opening times');
  $html = _toc_emr_render_available_facility_location($entity, $entityFields::FIELD_STATION_FACILITIES_WAITING_ROOM, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Waiting room information');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_STATION_FACILITIES_TROLLEYS, TRUE, 'Trolleys available', $entity, 'field');
  $html = _toc_emr_render_available_facility_available($entity, $entityFields::FIELD_STATION_FACILITIES_STATION_BUFFET, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Station buffet available');
  $html = _toc_emr_render_available_facility_annotation($entity, $entityFields::FIELD_STATION_FACILITIES_STATION_BUFFET, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Station buffet information');
  $html = _toc_emr_render_available_facility_available($entity, $entityFields::FIELD_STATION_FACILITIES_TOILETS, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Toilets available');
  $html = _toc_emr_render_available_facility_location($entity, $entityFields::FIELD_STATION_FACILITIES_TOILETS, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Toilets information');
  $html = _toc_emr_render_available_facility_available($entity, $entityFields::FIELD_STATION_FACILITIES_BABY_CHANGE, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Baby changing facilities available');
  $html = _toc_emr_render_available_facility_location($entity, $entityFields::FIELD_STATION_FACILITIES_BABY_CHANGE, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Baby changing facilities information');
  $html = _toc_emr_render_available_facility_available($entity, $entityFields::FIELD_STATION_FACILITIES_SHOWERS, $entityFields, TRUE);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Showers');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_STATION_FACILITIES_TELEPHONES_EXIST, TRUE, 'Telephones available', $entity, 'field');
  $html = _toc_emr_render_available_facility_available($entity, $entityFields::FIELD_STATION_FACILITIES_WIFI, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Wifi available');
  $html = _toc_emr_render_available_facility_annotation($entity, $entityFields::FIELD_STATION_FACILITIES_WIFI, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Wifi information');
  $html = _toc_emr_render_available_facility_available($entity, $entityFields::FIELD_STATION_FACILITIES_WEB_KIOSK, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Web kiosk available');
  $html = _toc_emr_render_available_facility_location($entity, $entityFields::FIELD_STATION_FACILITIES_WEB_KIOSK, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Web kiosk information');
  $html = _toc_emr_render_available_facility_available($entity, $entityFields::FIELD_STATION_FACILITIES_POST_BOX, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Post box available');
  $html = _toc_emr_render_available_facility_location($entity, $entityFields::FIELD_STATION_FACILITIES_POST_BOX, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Post box information');
  $html = _toc_emr_render_available_facility_available($entity, $entityFields::FIELD_STATION_FACILITIES_TOURIST_INFORMATION, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Tourist information available');
  $html = _toc_emr_render_available_facility_location($entity, $entityFields::FIELD_STATION_FACILITIES_TOURIST_INFORMATION, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Tourist information information');
  $html = _toc_emr_render_available_facility_available($entity, $entityFields::FIELD_STATION_FACILITIES_ATM_MACHINE, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'ATM available');
  $html = _toc_emr_render_available_facility_location($entity, $entityFields::FIELD_STATION_FACILITIES_ATM_MACHINE, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'ATM information');
  $html = _toc_emr_render_available_facility_available($entity, $entityFields::FIELD_STATION_FACILITIES_BUREAU_DE_CHANGE, $entityFields, TRUE);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Bureau de change available');
  $html = _toc_emr_render_available_facility_location($entity, $entityFields::FIELD_STATION_FACILITIES_BUREAU_DE_CHANGE, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Bureau de change information');
  $html = _toc_emr_render_available_facility_available($entity, $entityFields::FIELD_STATION_FACILITIES_SHOPS, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Shops available');
  $html = _toc_emr_render_available_facility_annotation($entity, $entityFields::FIELD_STATION_FACILITIES_SHOPS, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Shops information');
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_emr_nre_station_accordion_content_accessibility($entity, NREStationEntityConstants $entityFields): array {
  $rows = [];
  $html = _toc_emr_render_service_annotation($entity, $entityFields::FIELD_ACCESSIBILITY_HELPLINE, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Helpline information');
  $html = _toc_emr_render_service_contact_details($entity, $entityFields::FIELD_ACCESSIBILITY_HELPLINE, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Helpline contact details');
  $html = _toc_emr_render_available_facility_annotation($entity, $entityFields::FIELD_ACCESSIBILITY_STAFF_HELP_AVAILABLE, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Staff help information');
  $html = _toc_emr_render_available_facility_opening_times($entity, $entityFields::FIELD_ACCESSIBILITY_STAFF_HELP_AVAILABLE, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Staff help opening times');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_ACCESSIBILITY_INDUCTION_LOOP, TRUE, 'Induction loop available', $entity, 'field');
  $html = _toc_emr_render_available_facility_available($entity, $entityFields::FIELD_ACCESSIBILITY_ACCESSIBLE_TICKET_MACHINES, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Accessible ticket machines available');
  $html = _toc_emr_render_available_facility_annotation($entity, $entityFields::FIELD_ACCESSIBILITY_ACCESSIBLE_TICKET_MACHINES, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Accessible ticket machines information');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_ACCESSIBILITY_HEIGHT_ADJUSTED_TICKET_OFFICE_COUNTER, TRUE, 'Accessible booking office counter available', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_ACCESSIBILITY_RAMP_FOR_TRAIN_ACCESS, TRUE, 'Ramp for train access available', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_ACCESSIBILITY_ACCESSIBLE_TAXIS, TRUE, 'Accessible taxis information', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_ACCESSIBILITY_ACCESSIBLE_PUBLIC_TELEPHONES, TRUE, 'Accessible public telephones information', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_ACCESSIBILITY_NEAREST_STATIONS_WITH_MORE_FACILITIES_ANNOTATION, TRUE, 'Nearest station with more facilities', $entity, 'field');
  $html = _toc_emr_render_available_facility_available($entity, $entityFields::FIELD_ACCESSIBILITY_NATIONAL_KEY_TOILETS, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'National Key toilets available');
  $html = _toc_emr_render_available_facility_location($entity, $entityFields::FIELD_ACCESSIBILITY_NATIONAL_KEY_TOILETS, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'National Key toilets information');
  $html = _toc_emr_render_step_free_access($entity, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Step free access coverage');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_ACCESSIBILITY_STEP_FREE_ACCESS_ANNOTATION, TRUE, 'Step free access information', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_ACCESSIBILITY_TICKET_GATES, TRUE, 'Ticket gates exist', $entity, 'field');
  $html =_toc_emr_render_available_facility_available($entity, $entityFields::FIELD_ACCESSIBILITY_IMPAIRED_MOBILITY_SET_DOWN, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Impaired mobility set down available');
  $html =_toc_emr_render_available_facility_annotation($entity, $entityFields::FIELD_ACCESSIBILITY_IMPAIRED_MOBILITY_SET_DOWN, $entityFields);
  $rows = _tm_append_table_row($rows, $html, TRUE, 'Impaired mobility set down information');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_ACCESSIBILITY_WHEELCHAIRS_AVAILABLE, TRUE, 'Wheelchairs', $entity, 'field');
  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_nre_station_accordion_content_interchange($entity, NREStationEntityConstants $entityFields) {
  $rows = [];

  _tm_nre_stations_accordion_content_interchange_cycle($rows, $entity, $entityFields, 'Cycle storage number of spaces');
  _tm_nre_stations_accordion_content_interchange_car_parks($rows, $entity, $entityFields);
  _tm_nre_stations_accordion_content_interchange_replacement_map($rows, $entity, $entityFields);

  $rows = _tm_append_table_row($rows, $entityFields::FIELD_INTERCHANGE_TAXI_RANK, TRUE, 'Taxi rank information', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_INTERCHANGE_ONWARD_TRAVEL, TRUE, 'Bus services information', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_INTERCHANGE_METRO_SERVICES, TRUE, 'Metro services information', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_INTERCHANGE_AIRPORT, TRUE, 'Airport information', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_INTERCHANGE_PORT, TRUE, 'Port information', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_INTERCHANGE_CAR_HIRE, TRUE, 'Car hire information', $entity, 'field');
  $rows = _tm_append_table_row($rows, $entityFields::FIELD_INTERCHANGE_CYCLE_HIRE, TRUE, 'Cycle hire information', $entity, 'field');

  if (count($rows)) {
    return [
      '#type' => 'table',
      '#header' => [],
      '#rows' => $rows,
    ];
  }
  return [];
}

function _toc_emr_render_penalty_fares_train_operator($entity, NREStationEntityConstants $entityFields) {
  $operators = _tm_misc_get_field_content($entity, $entityFields::FIELD_FARES_PENALTY_FARES_TRAIN_OPERATOR, 'render', [], -1);
  $html = '';
  if ($operators) {
    $html .= 'Applicable Operators: ' . implode(', ', $operators);
  }
  if ($html) {
    $html = \Drupal\Core\Render\Markup::create($html);
  }
  return $html;
}

function _toc_emr_render_available_facility_location($entity, string $available_facility_field, NREStationEntityConstants $entityFields) {
  $available_facility_paragraph = _tm_misc_get_field_content($entity, $available_facility_field, 'reference');
  return _tm_misc_get_field_content($available_facility_paragraph, $entityFields::PARAGRAPH_AVAILABLE_FACILITY_FIELD_LOCATION, 'render');
}

function _toc_emr_render_available_facility_annotation($entity, string $available_facility_field, NREStationEntityConstants $entityFields) {
  $available_facility_paragraph = _tm_misc_get_field_content($entity, $available_facility_field, 'reference');
  return _tm_misc_get_field_content($available_facility_paragraph, $entityFields::PARAGRAPH_OPENING_HOURS_FIELD_ANNOTATION, 'render');
}

function _toc_emr_render_available_facility_opening_times($entity, string $available_facility_field, NREStationEntityConstants $entityFields) {
  $available_facility_paragraph = _tm_misc_get_field_content($entity, $available_facility_field, 'reference');
  $opening_hours_paragraph = _tm_misc_get_field_content($available_facility_paragraph, $entityFields::PARAGRAPH_AVAILABLE_FACILITY_FIELD_OPEN, 'reference');
  return _toc_emr_render_opening_times($opening_hours_paragraph, $entityFields);
}

function _toc_emr_render_available_facility_available($entity, string $available_facility_field, NREStationEntityConstants $entityFields, bool $return_unknown = FALSE) {
  $available_facility_paragraph = _tm_misc_get_field_content($entity, $available_facility_field, 'reference');
  return _toc_emr_render_available($available_facility_paragraph, $entityFields::PARAGRAPH_AVAILABLE_FACILITY_FIELD_AVAILABLE, $return_unknown);
}

function _toc_emr_render_service_annotation($entity, string $available_facility_field, NREStationEntityConstants $entityFields) {
  $available_facility_paragraph = _tm_misc_get_field_content($entity, $available_facility_field, 'reference');
  return _tm_misc_get_field_content($available_facility_paragraph, $entityFields::PARAGRAPH_AVAILABLE_FACILITY_FIELD_ANNOTATION, 'render');
}

function _toc_emr_render_service_available($entity, string $service_field, NREStationEntityConstants $entityFields, bool $return_unknown = FALSE) {
  $service_paragraph = _tm_misc_get_field_content($entity, $service_field, 'reference');
  return _toc_emr_render_available($service_paragraph, $entityFields::PARAGRAPH_SERVICE_FIELD_AVAILABLE, $return_unknown);
}

function _toc_emr_render_service_contact_details($entity, string $service_field, NREStationEntityConstants $entityFields) {
  $service_paragraph = _tm_misc_get_field_content($entity, $service_field, 'reference');
  $contact_details_paragraph = _tm_misc_get_field_content($service_paragraph, $entityFields::PARAGRAPH_SERVICE_FIELD_CONTACT_DETAILS, 'reference');
  return _toc_emr_render_contact_details($contact_details_paragraph, $entityFields);
}

function _toc_emr_render_service_opening_times($entity, string $service_field, NREStationEntityConstants $entityFields) {
  $service_paragraph = _tm_misc_get_field_content($entity, $service_field, 'reference');
  $opening_hours_paragraph = _tm_misc_get_field_content($service_paragraph, $entityFields::PARAGRAPH_SERVICE_FIELD_OPEN, 'reference');
  return _toc_emr_render_opening_times($opening_hours_paragraph, $entityFields);
}

function _toc_emr_render_opening_times($opening_hours_paragraph, NREStationEntityConstants $entityFields) {
  $opening_times = _tm_misc_get_field_content($opening_hours_paragraph, $entityFields::PARAGRAPH_OPENING_HOURS_FIELD_DAY_AND_TIME_AVAILABILITY, 'render', [], -1);
  $annotation = _tm_misc_get_field_content($opening_hours_paragraph, $entityFields::PARAGRAPH_OPENING_HOURS_FIELD_ANNOTATION, 'render');
  $html = '';
  if ($opening_times) {
    $html .= '<ul>';
    foreach ($opening_times as $opening_time) {
      $html .= '<li>'. $opening_time . '</li>';
    }
    $html .= '</ul>';
  }
  if ($annotation) {
    $html .= '<p>Note:</p>'. $annotation;
  }
  if ($html) {
    $html = \Drupal\Core\Render\Markup::create($html);
  }
  return $html;
}

function _toc_emr_render_contact_details($contact_details_paragraph, NREStationEntityConstants $entityFields) {
  $annotation = _tm_misc_get_field_content($contact_details_paragraph, $entityFields::PARAGRAPH_CONTACT_DETAILS_FIELD_ANNOTATION, 'render');
  $html = '';
  if ($annotation) {
    $html .= '<p>Note: '. $annotation .'</p>';
  }
  $primary_telephone = _tm_misc_get_field_content($contact_details_paragraph, $entityFields::PARAGRAPH_CONTACT_DETAILS_FIELD_PRIMARY_TELEPHONE_NUMBER, 'render');
  if ($primary_telephone) {
    $html .= '<p>Telephone: '. $primary_telephone .'</p>';
  }
  $alternative_telephones = _tm_misc_get_field_content($contact_details_paragraph, $entityFields::PARAGRAPH_CONTACT_DETAILS_FIELD_ALTERNATE_PUBLIC_TELEPHONE_NUMBERS, 'render', [], -1);
  if ($alternative_telephones) {
    $html .= '<p>Alternate numbers:</p><ul>';
    foreach ($alternative_telephones as $alternative_telephone) {
      $html .= '<li>'. $alternative_telephone .'</li>';
    }
    $html .= '</ul>';
  }
  $alternative_international_telephones = _tm_misc_get_field_content($contact_details_paragraph, $entityFields::PARAGRAPH_CONTACT_DETAILS_FIELD_ALTERNATE_INTERNAL_TELEPHONE_NUMBERS, 'render', [], -1);
  if ($alternative_international_telephones) {
    $html .= '<p>International numbers:</p><ul>';
    foreach ($alternative_international_telephones as $alternative_international_telephone) {
      $html .= '<li>'. $alternative_international_telephone .'</li>';
    }
    $html .= '</ul>';
  }
  $fax = _tm_misc_get_field_content($contact_details_paragraph, $entityFields::PARAGRAPH_CONTACT_DETAILS_FIELD_FAX_NUMBER, 'render');
  if ($fax) {
    $html .= '<p>Fax: '. $fax .'</p>';
  }
  $primary_minicom = _tm_misc_get_field_content($contact_details_paragraph, $entityFields::PARAGRAPH_CONTACT_DETAILS_FIELD_PRIMARY_MINICOM_NUMBER, 'render');
  if ($primary_minicom) {
    $html .= '<p>Minicom number: '. $primary_minicom .'</p>';
  }
  $alternate_minicom = _tm_misc_get_field_content($contact_details_paragraph, $entityFields::PARAGRAPH_CONTACT_DETAILS_FIELD_ALTERNATE_MINICOM_NUMBER, 'render');
  if ($alternate_minicom) {
    $html .= '<p>Alternative minicom number: '. $alternate_minicom .'</p>';
  }
  $url = _tm_misc_get_field_content($contact_details_paragraph, $entityFields::PARAGRAPH_CONTACT_DETAILS_FIELD_URL, 'render');
  if ($url) {
    $html .= '<p>URL: '. $url .'</p>';
  }
  if ($html) {
    $html = \Drupal\Core\Render\Markup::create($html);
  }
  return $html;
}

function _toc_emr_render_available($entity, string $available_field, bool $return_unknown) {
  $available = _tm_misc_get_field_content($entity, $available_field);
  if ($available === '1' || $available === '0') {
    return ($available === '1' ? 'Yes': 'No');
  }
  return $return_unknown ? 'Unknown' : FALSE;
}

function _toc_emr_render_step_free_access($entity, NREStationEntityConstants $entityFields) {
  $access = _tm_misc_get_field_content($entity, $entityFields::FIELD_ACCESSIBILITY_STEP_FREE_ACCESS_COVERAGE);

  switch ($access) {
    case 'wholeStation':
      return 'Whole station';
    case 'partialStation':
      return 'Partial station';
    case 'allPlatforms':
      return 'All platforms';
    case 'noPartOfStation':
      return 'No';
    case 'unknown':
      return 'Unknown';
  }

  return FALSE;
}
