<?php

/**
 * @file
 * Theming for individual forms by ID.
 */

use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;

/**
 * Implements hook_form_views_exposed_form_alter().
 */
function toc_emr_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {

}

/**
 * Implements hook_form_views_exposed_form_alter().
 */
function _toc_emr_form_views_exposed_form_alter__destinations__listing(&$form, FormStateInterface $form_state, $form_id) {
  $form["#attributes"]['class'] = ['pu-4'];
}

/**
 * Implements hook_form_views_exposed_form_alter().
 */
function _toc_emr_form_views_exposed_form_alter__news_blocks__travel_inspiration(&$form, FormStateInterface $form_state, $form_id) {
  $form["#attributes"]['class'] = [];
}

/**
 * Implements hook_form_views_exposed_form_alter().
 */
function _toc_emr_form_views_exposed_form_alter__offers_frontend_views__block_1(&$form, FormStateInterface $form_state, $form_id) {
  $form["#attributes"]['class'] = [];
}

function _toc_emr_preprocess_input__submit__offers_frontend_views__block_1($vars) {
  $vars['class'] = 'mra';
  return $vars;
}

/**
 * Implements hook__preprocess_views_exposed_form().
 */
function toc_emr_preprocess_views_exposed_form__destinations__listing(&$vars) {
  $filters_tabcordion_id = Html::getUniqueId("tabcordion");

  // Move Value to Text which is rendered with text|raw.
  $vars['form']['actions']['reset']['#text'] = '<i class="fas fa-times-circle me-2"></i>' . $vars['form']['actions']['reset']['#value'];
  $vars['form']['actions']['reset']['#size'] = 'sm';
  $vars['form']['actions']['reset']['#status'] = 'default';
  $vars['form']['actions']['reset']['#outline'] = TRUE;
  $vars['form']['actions']['reset']['#attributes']['class'][] = 'mb-0';

  // Create a new submit button for embedding in the Categories filter.
  $new_button = $vars['form']['actions']['submit'];
  $new_button['#attributes']['class'][] = 'mt-0 me-0';
  $new_button['#value'] = 'Filter by these categories';
  $new_button['#status'] = 'default';
  $new_button['#outline'] = TRUE;
  $new_button['#id'] = _toc_rndid_ifempty($new_button, '#id');
  $first_button = \Drupal::service('renderer')->render($new_button);
  // Create the first tab for our tabcordion.
  $prefix = "<div class='row'><div class='col-md-8'>";
  $suffix = "</div><div class='col-md-4'>$first_button</div></div>";
  $first_item = _toc_master_views_filters_tabcordion_tab($vars['form']['field_dest_er_category_target_id'], $filters_tabcordion_id, $prefix, $suffix);
  unset($vars['form']['field_dest_er_category_target_id']);

  // Create a new submit button for embedding in the Places filter.
  $new_button = $vars['form']['actions']['submit'];
  $new_button['#attributes']['class'][] = 'mt-0 me-0 mb-0';
  $new_button['#value'] = 'Filter by these destinations';
  $new_button['#status'] = 'default';
  $new_button['#outline'] = TRUE;
  $new_button['#id'] = _toc_rndid_ifempty($new_button, '#id');
  $second_button = \Drupal::service('renderer')->render($new_button);
  // Create the first tab for our tabcordion.
  $prefix = "<div class='row'><div class='col-md-8'>";
  $suffix = "</div><div class='col-md-4'>$second_button</div></div>";
  $second_item = _toc_master_views_filters_tabcordion_tab($vars['form']['nid_entityreference_filter'], $filters_tabcordion_id, $prefix, $suffix);
  unset($vars['form']['nid_entityreference_filter']);

  // Create a new reset in the tabcordion.
  $new_reset = $vars['form']['actions']['reset'];
  $new_reset['#id'] = _toc_rndid_ifempty($new_reset, '#id');
  // Create the tabcordion component.
  $vars['filters_tabcordion'] = [
    '#type' => 'pattern',
    '#id' => 'bs-accordion-items',
    '#variant' => 'aci_c_b',
    '#fields' => [
      'items' => [
        $first_item,
        $second_item,
      ],
      'list_prefix' => '<i class="fas fa-sliders-h"></i> Showing ',
      'list_suffix' => $new_reset,
    ],
    '#settings' => [
      'pid' => $filters_tabcordion_id,
      'toggle' => 'chevron',
    ],
  ];

  $vars['form']['actions']['submit']['#attributes']['class'][] = 'destinations-ajax-submit-btn';
}

/**
 * Implements hook__preprocess_views_exposed_form().
 */
function toc_emr_preprocess_views_exposed_form__news_blocks__travel_inspiration(&$vars) {

  $filters_tabcordion_id = Html::getUniqueId("tabcordion");
  if (!empty($vars['form_original'])) {
    $vars['form'] = $vars['form_original'];
  }

  // Move Value to Text which is rendered with text|raw.
  $vars['form']['actions']['reset']['#text'] = '<i class="fas fa-times-circle me-2"></i>' . $vars['form']['actions']['reset']['#value'];
  $vars['form']['actions']['reset']['#size'] = 'sm';
  $vars['form']['actions']['reset']['#attributes']['class'][] = 'mb-0';
  $vars['form']['actions']['reset']['#status'] = 'default';
  $vars['form']['actions']['reset']['#outline'] = TRUE;

  // Create a new submit button for embedding in the Categories filter.
  $new_button = $vars['form']['actions']['submit'];
  $new_button['#attributes']['class'][] = 'mt-0 me-0 mb-0';
  $new_button['#value'] = 'Filter by these categories';
  $new_button['#status'] = 'default';
  $new_button['#outline'] = TRUE;
  $new_button['#id'] = _toc_rndid_ifempty($new_button, '#id');
  $first_button = \Drupal::service('renderer')->render($new_button);

  // Create the first tab for our tabcordion.
  $prefix = "<div class='row'><div class='col-md-8'>";
  $suffix = "</div><div class='col-md-4'>$first_button</div></div>";
  $first_item = _toc_master_views_filters_tabcordion_tab($vars['form']['field_travel_inspiration_cat_target_id'], $filters_tabcordion_id, $prefix, $suffix);
  unset($vars['form']['field_travel_inspiration_cat_target_id']);

  // Create a new reset in the tabcordion.
  $new_reset = $vars['form']['actions']['reset'];
  $new_reset['#id'] = _toc_rndid_ifempty($new_reset, '#id');
  // Create the tabcordion component.
  $vars['filters_tabcordion'] = [
    '#type' => 'pattern',
    '#id' => 'bs-accordion-items',
    '#variant' => 'aci_c',
    '#fields' => [
      'items' => [
        $first_item,
      ],
      'list_prefix' => '<i class="fas fa-sliders-h"></i> Showing ',
      'list_suffix' => $new_reset,
    ],
    '#settings' => [
      'pid' => $filters_tabcordion_id,
      'toggle' => 'chevron',
    ],
  ];

  $vars['form']['actions']['submit']['#status'] = 'info';
  $vars['form']['actions']['submit']['#attributes']['class'][] = 'travel-inspiration-ajax-submit-btn';
}

/**
 * Implements hook_preprocess_views_exposed_form().
 */
function toc_emr_preprocess_views_exposed_form__offers_frontend_views__block_1(&$vars) {
  $filters_tabcordion_id = Html::getUniqueId("tabcordion");
  if (!empty($vars['form_original'])) {
    $vars['form'] = $vars['form_original'];
  }

  $vars['form']['field_of_type_target_id']['#attributes']['class'][] = 'fs-tags-no-label mb-0';

  $vars['form']['combine']['#title_display'] = 'invisible';
  $vars['form']['combine']['#size'] = 'lg';
  $vars['form']['combine']['#input_suffix']['#size'] = 'lg';

  $vars['form']['actions']['reset']['#text'] = '<i class="fas fa-times-circle me-3"></i>' . $vars['form']['actions']['reset']['#value'];
  $vars['form']['actions']['reset']['#size'] = 'sm';
  $vars['form']['actions']['reset']['#attributes']['class'][] = 'mb-0';
  $vars['form']['actions']['reset']['#status'] = 'default';
  $vars['form']['actions']['reset']['#outline'] = TRUE;

  $vars['form']['actions']['#element_classes'] = 'form-group-no-pad';
  // Create a new submit button for embedding in the Categories filter.
  $new_button = $vars['form']['actions']['submit'];
  $new_button['#value'] = 'Filter by these categories';
  $new_button['#attributes']['class'][] = 'mb-0 mt-3';
  $new_button['#status'] = 'default';
  $new_button['#id'] = _toc_rndid_ifempty($new_button, '#id');
  $first_button = \Drupal::service('renderer')->render($new_button);
  $first_item = _toc_master_views_filters_tabcordion_tab($vars['form']['field_of_category_target_id'], $filters_tabcordion_id, '', $first_button);
  unset($vars['form']['field_of_category_target_id']);
  unset($vars['form']['combine_1']);
  unset($vars['form']['field_of_region_target_id']);

  // Create a new reset in the tabcordion.
  $new_reset = $vars['form']['actions']['reset'];
  $new_reset['#id'] = _toc_rndid_ifempty($new_reset, '#id');

  // Create the tabcordion component.
  $vars['filters_tabcordion'] = [
    '#type' => 'pattern',
    '#id' => 'bs-accordion-items',
    '#variant' => 'aci_c_nbgi',
    '#fields' => [
      'items' => [
        $first_item,
      ],
      'list_prefix' => '<i class="fas fa-sliders-h"></i> Showing ',
      'list_suffix' => $new_reset,
    ],
    '#settings' => [
      'pid' => $filters_tabcordion_id,
      'toggle' => 'chevron',
    ],
  ];

  $vars['form']['actions']['submit']['#attributes']['class'][] = 'offers-ajax-submit-btn';
  $vars['form']['field_of_type_target_id']['#attributes']['onchange'] = 'jQuery(".offers-ajax-submit-btn").click()';

  unset($vars['form']['field_of_type_target_id']);
}

/**
 * Implements hook_preprocess_fieldset__FIELD_ID().
 */
function toc_emr_preprocess_fieldset__field_dest_er_category_target_id(&$vars) {
  toc_master_preprocess_fieldset__checkboxes($vars);
  _toc_emr_clear_fieldset_radioboxes($vars);
}

/**
 * Implements hook_preprocess_fieldset__FIELD_ID().
 */
function toc_emr_preprocess_fieldset__field_travel_inspiration_cat_target_id(&$vars) {
  toc_master_preprocess_fieldset__checkboxes($vars);
  _toc_emr_clear_fieldset_radioboxes($vars);
}

/**
 * Implements hook_preprocess_fieldset__FIELD_ID().
 */
function toc_emr_preprocess_fieldset__nid_entityreference_filter__checkboxes(&$vars) {
  toc_master_preprocess_fieldset__checkboxes($vars);
  _toc_emr_clear_fieldset_radioboxes($vars);
}

/**
 * Implements hook_preprocess_fieldset__FIELD_ID().
 */
function toc_emr_preprocess_fieldset__field_of_category_target_id__checkboxes(&$vars) {
  toc_master_preprocess_fieldset__checkboxes($vars);
  _toc_emr_clear_fieldset_radioboxes($vars);
}

/**
 * Implements hook_preprocess_fieldset__FIELD_ID().
 */
function toc_emr_preprocess_fieldset__field_of_region_target_id__checkboxes(&$vars) {
  toc_master_preprocess_fieldset__checkboxes($vars);
  _toc_emr_clear_fieldset_radioboxes($vars);
}

/**
 * Helper function. Hide the tags and margin/padding for fieldsets in filters.
 *
 * @param $vars
 *
 * @return void
 */
function _toc_emr_clear_fieldset_radioboxes(&$vars) {
  $vars['attributes']['class'][] = 'no-shd';
  $vars['attributes']['class'][] = 'no-tags';
  $vars['attributes']['class'][] = 'mb-0';
}

/**
 * Implements hook__preprocess_views_exposed_form().
 * Search modal form.
 */
function toc_emr_preprocess_views_exposed_form__search_content__page_results(&$vars) {

  // Default was hidden, set to before.
  $vars['form']['search_api_fulltext']['#title_display'] = 'invisible';
  $vars['form']['search_api_fulltext']['#element_classes'] = 'col-md-11 mb-0 mt-0 me-2';
  $vars['form']['search_api_fulltext']["#label_attributes"]['class'][] = 'h2 text-white ffm';

  $vars['form']['actions']['#attributes']['class'][] = 'col-md-4 mt-3 d-none';
  $vars['form']['actions']['submit']['#attributes']['class'][] = 'w-100 align-self-end mb-2 ffm';
  // $vars['form']['actions']['submit']['#size'] = 'lg';
  $vars['form']['actions']['#attributes']['id'] = 'edit-actions-search';

  // The default function adds the 'addon' for all search boxes, unset that.
  if (!empty($vars['form']['search_api_fulltext']['#input_suffix'])) {
    $vars['form']['search_api_fulltext']['#input_suffix'] =
      '<button data-drupal-selector="edit-submit-search-content" class="button js-form-submit form-submit btn btn-default btn-lg mla" type="submit" id="edit-submit-search-content--2" value="Search" data-persistent-id="edit-search-views-exposed-form-search-content" data-dashlane-label="true" data-dashlane-rid="15ec5ef945af53c8">
    <span class="sr-only">Search</span><i class="fas fa-chevron-right"></i>
  </button>';
  }
}

/**
 * This should be moved to a block/module config.
 *
 * @param $vars
 *
 * @return void
 */
function toc_emr_preprocess_form__select_destination_autocomplete_form(&$vars) {
  $markup = '<p>Start typing the name of a East Midlands Railway destination, select from the dropdown list and click/tap the search icon.</p>';
  $vars['children'] = [
    ['#markup' => $markup],
    ['#markup' => $vars['children']],
  ];
  $vars['attributes']['class'][] = 'mb-0';
}

function toc_emr_form_nre_stations_ldb_blocks_next_trains_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $station = \Drupal::request()->get('station');
  if (!$station) {
    if (!empty($form['toc_stations'])) {
      $form['toc_stations']['#input_prefix'] = FALSE;
      $form['toc_stations']['#title_display'] = 'before';
      $form['toc_stations']['#label_attributes']['class'] = ['lbl-alt'];
    }
    if (!empty($form['station'])) {
      $form['station']['#input_prefix'] = FALSE;
      $form['station']['#title_display'] = 'before';
      $form['station']['#attributes']['placeholder'] = 'Enter station name';
      $form['station']['#label_attributes']['class'] = ['lbl-alt'];
    }
  }
}

function toc_emr_form_nre_stations_ldb_live_boards_finder_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (isset($form['callingAt'])) {
    $form['callingAt']['#input_prefix'] = $form['callingAt']['#title'] ?? '';
    $form['callingAt']['#title_display'] = 'invisible';
    $form['callingAt']['#element_classes'] = 'mt-3 solid-background';
  }
  $form['station']['#element_classes'] .= ' solid-background';
  unset($form['station']['#input_suffix']);
  $form['#attributes']['class'][] = 'mb-0 stations-autocomplete';
  if (($key = array_search('visually-hidden', $form['actions']['submit']['#attributes']['class'])) !== FALSE) {
    unset($form['actions']['submit']['#attributes']['class'][$key]);
  }
  $form['actions']['submit']['#attributes']['class'][] = 'w-100 h3 py-2 rounded-3';
  $form['actions']['submit']['#value'] = Markup::create('Search live trains');
}
