<?php

namespace Drupal\support_dar\Form;

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

/**
 * Configuration form.
 */
class SettingsForm extends ConfigFormBase {

  /**
   * Config key to save settings.
   */
  public const CONFIG_KEY = 'support_dar.settings';

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['site_key'] = [
      '#type' => 'markup',
      '#markup' => $this->t('Your site key: @key, give it to us to register your domain', ['@key' => LicenseManager::getSiteKey()]),
    ];
    $config = $this->config(self::CONFIG_KEY);
    $form['lms_api_token'] = [
      '#type' => 'textfield',
      '#title' => $this->t('License Manager System API token'),
      '#default_value' => $config->get('lms_api_token'),
      '#maxlength' => 1024,
    ];

    $form['lms_license_id'] = [
      '#type' => 'textfield',
      '#title' => $this->t('License ID in Manager System'),
      '#default_value' => $config->get('lms_license_id'),
    ];

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config(self::CONFIG_KEY)
      ->set('lms_api_token', $form_state->getValue('lms_api_token'))
      ->set('lms_license_id', $form_state->getValue('lms_license_id'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}
