<?php

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

/**
 * Preprocess hook for node__article().
 */
function toc_ga_preprocess_node__article(&$vars) {
  // The entity.
  $entity = $vars['node'];
  $cats = _tm_misc_get_field_content($entity, 'field_blog_category');
  $tags = _tm_misc_format_tags($cats);
  $published_date = _tm_misc_format_date_overridden($entity, 'toc_long', 'created');
  $changed_date = _tm_misc_format_date_overridden($entity, 'toc_long', 'changed', []);
  $vars['pub_date'] = "Published on: " . $published_date;
  $vars['changed_date'] = "Last updated: " . $changed_date;
  $vars['tags'] = $tags;
}

/**
 * Preprocess hook for eck_entity__nres_ee_nre_station().
 */
function toc_ga_preprocess_eck_entity__nres_ee_nre_station(&$vars) {
  $entity = $vars['eck_entity'];
  $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;

  // Data for extended header.
  $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) {
      $data = _tm_misc_get_field_content($times, 'field_nres_eeb_par_day_and_time', 'render', [], -1);
      if ($data) {
        $vars['ticket_office'] = $data;
      }
      $annotation = _tm_misc_get_field_content($times, 'field_nres_eeb_sta_annotation', 'render');
      if ($annotation) {
        $vars['ticket_office'][] = $annotation;
      }
    }
  }
  if (!isset($vars['ticket_office'])) {
    $vars['ticket_office'] = [[
      '#type' => 'markup',
      '#markup' => '<i class="fas fa-times"></i>',
    ]];
  }

  $vars['post'] = $vars['elements']['post'] ?? '';
  $checklist = [
    'Ticket office' => 'field_nres_eeb_par_ticket_office',
    'Step-free access' => 'field_nres_eeb_sta_step_free',
    'Prepurchase collection' => 'field_nres_eeb_par_collection',
    'Car park' => 'field_nres_eeb_par_car_parks',
    'Ticket machine' => 'field_nres_eeb_lt_ticket_mach',
    'Toilets' => 'field_nres_eeb_par_toilets',
    'Taxi-rank' => 'field_nres_eeb_sta_taxi_rank'
  ];

  $station_features = nre_stations_entity_type_get_station_features($station_crs);
  if ($station_features) {
    foreach ($station_features['features'] as &$feature) {
      if ($feature == 'Car park' && isset($vars['elements']['car_parks_entities'])) {
        $feature = $vars['elements']['car_parks_entities'];
      }
    }
    $vars['checklist'] = $station_features['features'];
  }

}
