<?php

namespace Drupal\advanced_search\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\advanced_search\AdvancedSearchQuery;
use Drupal\advanced_search\GetConfigTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Config form for Islandora Advanced Search settings.
 */
class SettingsForm extends ConfigFormBase
{

  use GetConfigTrait;

  const CONFIG_NAME = 'advanced_search.settings';
  const SEARCH_QUERY_PARAMETER = 'search_query_parameter';
  const SEARCH_RECURSIVE_PARAMETER = 'search_recursive_parameter';
  const SEARCH_ADD_OPERATOR = 'search_add_operator';
  const SEARCH_REMOVE_OPERATOR = 'search_remove_operator';
  const FACET_TRUNCATE = 'facet_truncate';
  const EDISMAX_SEARCH_FLAG = 'lucene_on_off';
  const EDISMAX_SEARCH_LABEL = 'lucene_label';
  const SEARCH_ALL_FIELDS_FLAG = 'all_fields_on_off';
  const DISPLAY_LIST_FLAG = 'list_on_off';
  const DISPLAY_GRID_FLAG = 'grid_on_off';
  const DISPLAY_DEFAULT = 'default-display-mode';
  const QUERY_FIELDS = 'query_fields';

  /**
   * Constructs a \Drupal\system\ConfigFormBase object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   */
  final public function __construct(ConfigFactoryInterface $config_factory)
  {
    $this->setConfigFactory($config_factory);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container)
  {
    return new static($container->get('config.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId()
  {
    return 'advanced_search_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames()
  {
    return [
      self::CONFIG_NAME,
    ];
  }

  /**
   * Get available fields from all Search API indexes.
   *
   * @return array
   *   An associative array of field identifiers to field labels.
   */
  protected function getAvailableFields()
  {
    $field_options = [];

    // Load all Search API indexes.
    $index_storage = \Drupal::entityTypeManager()->getStorage('search_api_index');
    $indexes = $index_storage->loadMultiple();

    foreach ($indexes as $index) {
      $fields = $index->getFields();
      foreach ($fields as $field_id => $field) {
        $field_options[$field_id] = $field->getLabel() . " (" . $field_id . ")";
      }
    }

    return $field_options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state)
  {
    $form['eDisMax'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Advanced Search Block'),
      '#weight' => -1,
    ];
    $form['eDisMax']['advanced-search-block-description'] = [
      '#markup' => $this->t('Advanced Search Blocks are available in the Blocks interface for each Search API view. When placing an Advanced Search Block, you can configure the fields that are used for field-based search and whether a "recursive" search is available.  The following settings apply to all Advanced Search blocks.'),
      '#weight' => -2,
    ];

    $isEDismax = \Drupal::config(SettingsForm::CONFIG_NAME)->get(self::EDISMAX_SEARCH_FLAG);
    $form['eDisMax'][self::EDISMAX_SEARCH_FLAG] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable Extended DisMax Query.'),
      '#description' => $this->t('<ul> <li>When enabled, all queries using an Advanced Search Block use the Extended Dismax (eDisMax) query processor.</li>
        <li>This setting must be enabled for the “Simple Search Block” to function. </li>
        <li>If enabled, the “Simple Search Block”/”Advanced Search Blocks” support:
           <ul> 
            <li>queries that include AND, OR, NOT, -, and + (user documentation needed)</li>
            <li>Wildcard operator *</li>
            <li>Words in query are treated as distinct words. They are combined using OR unless the user specifies using AND/NOT in their query.</li>
           </ul>
          </li>
        </ul>'),
      '#default_value' => $isEDismax ?? 1,
    ];

    $form['eDisMax']['textfields_container'] = [
      '#type' => 'container',
      '#attributes' => ['id' => 'edismax-container'],
    ];

    $form['eDisMax']['textfields_container'][self::SEARCH_ALL_FIELDS_FLAG] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable searching all fields'),
      '#description' => $this->t('<ul>
          <li>This makes an additional option visible in all Advanced Search Blocks, which searches across all fields. Its label is configured below.</li>
          <li>This setting must be enabled for the "Simple Search Block" to function.</li>
        </ul>'),
      '#default_value' => self::getConfig(self::SEARCH_ALL_FIELDS_FLAG, 0),
    ];
    $form['eDisMax']['textfields_container'][self::EDISMAX_SEARCH_LABEL] = [
      '#type' => 'textfield',
      '#title' => $this->t('If enabled, set the label for the option of searching all fields'),
      '#description' => $this->t('E.g. keyword.'),
      '#default_value' => self::getConfig(self::EDISMAX_SEARCH_LABEL, "Keyword"),
    ];

    // Query Fields Block 
    $form['query_fields_block'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Query Fields'),
      '#weight' => -0.5,
    ];

    $field_options = $this->getAvailableFields();

    $base_url = \Drupal::request()->getSchemeAndHttpHost();
    $query_fields_description = $this->t('Select which fields should be queried when searching all fields label (keyword) chosen. If no fields are selected, <a href="@fields_url" target="_blank">all fields will be searched</a>.', [
      '@fields_url' => $base_url . '/admin/config/search/search-api/index/default_solr_index_islandora_lite/fields',
    ]);

    $form['query_fields_block']['query_fields_description'] = [
      '#type' => 'item',
      '#markup' => '<div style=" background: #f0f0f0; border-left: 4px solid #0074bd;">' . $query_fields_description . '</div>',
    ];

    $form['query_fields_block']['query_fields_wrapper'] = [
      '#type' => 'container',
      '#attributes' => [
        'style' => 'max-height: 350px; overflow-y: auto; border: 1px solid #ccc; padding: 10px; background: #f9f9f9;',
      ],
    ];

    $form['query_fields_block']['query_fields_wrapper'][self::QUERY_FIELDS] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Select fields to query'),
      '#options' => $field_options,
      '#default_value' => self::getConfig(self::QUERY_FIELDS, []),
    ];

    $form['display-mode'] = [
      '#type' => 'fieldset',
      '#title' => $this->t("Pager Block"),
    ];
    $form['display-mode']['pager-block-description'] = [
      '#markup' => $this->t("Pager blocks are available in the Blocks interface for each Search API view.  The following settings apply for all Pager blocks."),
    ];

    $form['display-mode'][self::DISPLAY_LIST_FLAG] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Expose "List view" option.'),
      '#default_value' => self::getConfig(self::DISPLAY_LIST_FLAG, 0),
    ];

    $form['display-mode'][self::DISPLAY_GRID_FLAG] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Expose "Grid view" option.'),
      '#default_value' => self::getConfig(self::DISPLAY_GRID_FLAG, 0),
    ];

    $form['display-mode'][self::DISPLAY_DEFAULT] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Default view mode:'),
      '#options' => [
        'list' => 'List',
        'grid' => 'Grid',
      ],
      '#default_value' => self::getConfig(self::DISPLAY_DEFAULT, 'grid'),
    ];

    $form += [
      'search' => [
        '#type' => 'fieldset',
        '#title' => $this->t('Advanced Search'),
        self::SEARCH_QUERY_PARAMETER => [
          '#type' => 'textfield',
          '#title' => $this->t('Search Query Parameter'),
          '#description' => $this->t('The url parameter in which the advanced search query is stored.'),
          '#default_value' => AdvancedSearchQuery::getQueryParameter(),
        ],
        self::SEARCH_RECURSIVE_PARAMETER => [
          '#type' => 'textfield',
          '#title' => $this->t('Recurse Query Parameter'),
          '#description' => $this->t('The url parameter which can toggle recursive search.'),
          '#default_value' => AdvancedSearchQuery::getRecurseParameter(),
        ],
        self::SEARCH_ADD_OPERATOR => [
          '#type' => 'textfield',
          '#title' => $this->t('Facet Add Operator'),
          '#description' => $this->t('Users can customize the operator for adding facets to use font-awesome or some other icon, etc.'),
          '#default_value' => AdvancedSearchForm::getAddOperator(),
        ],
        self::SEARCH_REMOVE_OPERATOR => [
          '#type' => 'textfield',
          '#title' => $this->t('Facet Remove Operator'),
          '#description' => $this->t('Users can customize the operator for removing facets to use font-awesome or some other icon, etc.'),
          '#default_value' => AdvancedSearchForm::getRemoveOperator(),
        ],
      ],
      'facets' => [
        '#type' => 'fieldset',
        '#title' => $this->t('Facets'),
        self::FACET_TRUNCATE => [
          '#type' => 'number',
          '#title' => $this->t('Truncate Facet'),
          '#description' => $this->t('Optionally truncate the length of facets titles in the display. If unspecified they will not be truncated.'),
          '#default_value' => self::getConfig(self::FACET_TRUNCATE, 32),
          '#min' => 1,
        ],
      ],
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Filter out unchecked checkboxes (value = '0') from query_fields.
    $query_fields = $form_state->getValue(self::QUERY_FIELDS);
    $query_fields = is_array($query_fields) ? array_filter($query_fields) : [];

    $config = $this->configFactory->getEditable(self::CONFIG_NAME);
    $config
      ->set(self::SEARCH_QUERY_PARAMETER, $form_state->getValue(self::SEARCH_QUERY_PARAMETER))
      ->set(self::SEARCH_RECURSIVE_PARAMETER, $form_state->getValue(self::SEARCH_RECURSIVE_PARAMETER))
      ->set(self::SEARCH_ADD_OPERATOR, $form_state->getValue(self::SEARCH_ADD_OPERATOR))
      ->set(self::SEARCH_REMOVE_OPERATOR, $form_state->getValue(self::SEARCH_REMOVE_OPERATOR))
      ->set(self::FACET_TRUNCATE, $form_state->getValue(self::FACET_TRUNCATE))
      ->set(self::EDISMAX_SEARCH_FLAG, $form_state->getValue(self::EDISMAX_SEARCH_FLAG))
      ->set(self::EDISMAX_SEARCH_LABEL, $form_state->getValue(self::EDISMAX_SEARCH_LABEL))
      ->set(self::SEARCH_ALL_FIELDS_FLAG, $form_state->getValue(self::SEARCH_ALL_FIELDS_FLAG))
      ->set(self::DISPLAY_LIST_FLAG, $form_state->getValue(self::DISPLAY_LIST_FLAG))
      ->set(self::DISPLAY_GRID_FLAG, $form_state->getValue(self::DISPLAY_GRID_FLAG))
      ->set(self::DISPLAY_DEFAULT, $form_state->getValue(self::DISPLAY_DEFAULT))
      ->set(self::QUERY_FIELDS, $query_fields)
      ->save();
    parent::submitForm($form, $form_state);
  }
}