<?php

namespace Drupal\azure_ad\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides the manual provisioning form for Azure AD.
 */
class MoManualProvisioning extends FormBase {
  /**
   * Configuration object for user provisioning settings.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  private ImmutableConfig $config;

  /**
   * Constructs a new MoManualProvisioning object.
   */
  public function __construct(ConfigFactoryInterface $config_factory, MessengerInterface $messenger) {
    $this->setConfigFactory($config_factory);
    $this->setMessenger($messenger);
    $this->config = $config_factory->get('azure_ad.settings');
  }

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

  /**
   * Returns the form ID.
   *
   * @return string
   *   The form ID.
   */
  public function getFormId() {
    return 'mo_azure_review_config';
  }

  /**
   * Builds the form.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   *
   * @return array
   *   The form array.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['#prefix'] = '<div id="modal_support_form">';
    $form['#suffix'] = '</div>';
    $form['status_messages'] = [
      '#type' => 'status_messages',
      '#weight' => -10,
    ];

    $form['mo_azure_ad_to_drupal_library'] = [
      '#attached' => [
        'library' => [
          'azure_ad/azure_ad.admin',
          'core/drupal.dialog.ajax',
        ],
      ],
    ];

    $form['mo_azure_user_operations'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Manual Provisioning Configurations'),
    ];

    $form['mo_azure_user_operations']['read_user_azure_ad_manual'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Read user&nbsp;'),
      '#default_value' => 1,
      '#disabled' => TRUE,
    ];

    $form['mo_azure_user_operations']['create_user_azure_ad_manual'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Create user&nbsp;'),
      '#default_value' => $this->config->get('azure_ad_manual_provisioning_checkbox'),
    ];

    $url = Url::fromRoute('azure_ad.azure_upgrade_plans')->toString();
    $mo_premium_tag = '<a href="' . $url . '" class="premium-badge">' . $this->t('PREMIUM') . '</a>';
    $form['mo_azure_user_operations']['update_user_azure_ad'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Update user&nbsp;') . $mo_premium_tag,
      '#disabled' => TRUE,
    ];

    $form['mo_azure_user_operations']['deactivate_user_azure_ad'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Deactivate user&nbsp;') . $mo_premium_tag,
      '#disabled' => TRUE,
    ];

    $form['mo_azure_user_operations']['delete_user_azure_ad'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Delete user&nbsp;') . $mo_premium_tag,
      '#disabled' => TRUE,

    ];

    $form['actions'] = ['#type' => 'actions'];
    $form['actions']['send'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save'),
      '#attributes' => [
        'class' => [
          'button--primary',
        ],
      ],
      '#ajax' => [
        'callback' => [$this, 'submitModalFormAjax'],
        'event' => 'click',
      ],
    ];
    return $form;
  }

  /**
   * Handles AJAX submission for the manual provisioning form.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   The AJAX response containing form replacement or redirect commands.
   */
  public function submitModalFormAjax(array $form, FormStateInterface $form_state) {

    $response = new AjaxResponse();
    // If there are any form errors, AJAX replace the form.
    if ($form_state->hasAnyErrors()) {
      $response->addCommand(new ReplaceCommand('#modal_support_form', $form));
    }
    else {

      $form_values = $form_state->getValues();
      $manual_create_user = $form_values['create_user_azure_ad_manual'];

      $this->configFactory->getEditable('azure_ad.settings')
        ->set('azure_ad_manual_provisioning_checkbox', $manual_create_user)
        ->save();
      $this->messenger->addStatus($this->t('Configurations saved successfully.'));

      $response->addCommand(new RedirectCommand(Url::fromRoute('azure_ad.overview')->toString()));
    }
    return $response;
  }

  /**
   * Submits the form.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {}

}
