<?php

namespace Drupal\dkan_datastore\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\dkan_datastore\Controller\QueryController;
use Drupal\dkan_metastore\SchemaPropertiesHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Datastore settings form.
 *
 * @package Drupal\dkan_datastore\Form
 * @codeCoverageIgnore
 */
class DatastoreSettingsForm extends ConfigFormBase {

  /**
   * SchemaPropertiesHelper service.
   *
   * @var \Drupal\dkan_metastore\SchemaPropertiesHelper
   */
  private $schemaHelper;

  /**
   * Constructs form.
   *
   * @param \Drupal\dkan_metastore\SchemaPropertiesHelper $schemaHelper
   *   The schema properties helper service.
   */
  public function __construct(SchemaPropertiesHelper $schemaHelper) {
    $this->schemaHelper = $schemaHelper;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('dkan.metastore.schema_properties_helper'),
    );
  }

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

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['dkan_datastore.settings'];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['rows_limit'] = [
      '#type' => 'number',
      '#min' => 1,
      '#title' => $this->t('Rows limit'),
      '#default_value' => $this->config('dkan_datastore.settings')->get('rows_limit'),
      '#description' => $this->t('Maximum number of rows the datastore endpoints can return
        in a single request. Caution: setting too high can lead to timeouts or memory issues.
        Default 500; values above 20,000 not recommended.'),
    ];

    $form['response_stream_max_age'] = [
      '#type' => 'number',
      '#title' => $this->t('Response Stream Max-Age'),
      '#default_value' => $this->config('dkan_datastore.settings')->get('response_stream_max_age'),
      '#min' => 0,
      '#description' => $this->t('Set the cache max-age for streaming CSV responses, in seconds. Default: 3600 (1 hour).'),
    ];

    $form['triggering_properties'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Datastore triggering properties'),
      '#description' => $this->t('Metadata properties whose change will trigger a re-import of
        an associated resource to the datastore.'),
      '#options' => $this->schemaHelper->retrieveSchemaProperties('dataset'),
      '#default_value' => $this->config('dkan_datastore.settings')->get('triggering_properties'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config('dkan_datastore.settings')
      ->set('rows_limit', $form_state->getValue('rows_limit') ?: QueryController::DEFAULT_ROWS_LIMIT)
      ->set('response_stream_max_age', $form_state->getValue('response_stream_max_age'))
      ->set('triggering_properties', $form_state->getValue('triggering_properties'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}
