<?php

declare(strict_types=1);

namespace Drupal\farm_map_google\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a google maps settings form.
 */
class GoogleMapsSettingsForm extends ConfigFormbase {

  /**
   * Config settings.
   *
   * @var string
   */
  const SETTINGS = 'farm_map_google.settings';

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config(static::SETTINGS);

    // Add api_key field.
    $form['api_key'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Google Map Tiles API Key'),
      '#description' => $this->t('Enter your Google Map Tiles API key.'),
      '#default_value' => $config->get('api_key'),
    ];

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->configFactory->getEditable(static::SETTINGS)
      ->set('api_key', $form_state->getValue('api_key'))
      ->save();
    drupal_flush_all_caches();

    parent::submitForm($form, $form_state);
  }

}
