<?php

namespace Drupal\layout_paragraphs_theme_extension\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Configure Layout Paragraphs Theme Extension settings.
 */
class SettingsForm extends ConfigFormBase {

  /**
   * The typed configuration manager.
   *
   * @var \Drupal\Core\Config\TypedConfigManagerInterface
   */
  protected TypedConfigManagerInterface $typedConfigManager;

  /**
   * Constructs a SettingsForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager
   *   The typed config manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typed_config_manager) {
    parent::__construct($config_factory, $typed_config_manager);
    $this->typedConfigManager = $typed_config_manager;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getFormId(): string {
    return 'layout_paragraphs_theme_extension_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames(): array {
    return [
      'layout_paragraphs_theme_extension.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state): array {
    $config = $this->config('layout_paragraphs_theme_extension.settings');
    $config_schema = $this->typedConfigManager->getDefinition('layout_paragraphs_theme_extension.settings')['mapping'] ?? [];

    $form['display_default_theme'] = [
      '#type' => 'checkbox',
      '#title' => $config_schema['display_default_theme']['label'] ?? $this->t('Display default theme in admin'),
      '#description' => $config_schema['display_default_theme']['description'] ?? '',
      '#default_value' => $config->get('display_default_theme'),
    ];

    $form['default_theme_library'] = [
      '#type' => 'textfield',
      '#title' => $config_schema['default_theme_library']['label'] ?? $this->t('Default theme library'),
      '#description' => $config_schema['default_theme_library']['description'] ?? '',
      '#default_value' => $config->get('default_theme_library'),
    ];

    $form['additional_templates'] = [
      '#type' => 'textarea',
      '#rows' => 10,
      '#title' => $config_schema['additional_templates']['label'] ?? $this->t('Additional templates to load'),
      '#description' => $config_schema['additional_templates']['description'] ?? '',
      '#default_value' => $config->get('additional_templates'),
    ];

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $this->config('layout_paragraphs_theme_extension.settings')
      ->set('display_default_theme', $form_state->getValue('display_default_theme'))
      ->set('default_theme_library', $form_state->getValue('default_theme_library'))
      ->set('additional_templates', $form_state->getValue('additional_templates'))
      ->save();

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

}
