<?php

namespace Drupal\user_provisioning\Form;

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

/**
 * Remove account confirmation form for user provisioning.
 *
 * @package Drupal\user_provisioning\Form
 * @description This form is used to remove the account from the user provisioning module.
 */
class MoUserProvisioningRemoveAccount extends FormBase {
  /**
   * Editable module config object.
   *
   * @var Config
   *   Editable config for user provisioning settings.
   */
  protected $configFactory;

  /**
   * This method is used to construct the form.
   */
  public function __construct(
    ConfigFactoryInterface $config_factory,
    MessengerInterface $messenger,
  ) {
    $this->configFactory = $config_factory->getEditable('user_provisioning.settings');
    $this->messenger = $messenger;
  }

  /**
   * Creates the class instance.
   */
  public static function create(ContainerInterface $container): MoUserProvisioningRemoveAccount|static {
    return new static(
      $container->get('config.factory'),
      $container->get('messenger'),
    );
  }

  /**
   * This method is used to get the form id.
   *
   * @return string
   *   The form id.
   */
  public function getFormId() {
    return 'miniorange_user_provisioning_remove_account';
  }

  /**
   * This method is used to build the form.
   *
   * @param array $form
   *   The form array.
   * @param FormStateInterface $form_state
   *   The form state.
   * @param string $options
   *   The options.
   *
   * @return array
   *   The form array.
   */
  public function buildForm(array $form, FormStateInterface $form_state, $options = NULL) {
    $form['mo_user_provision_add_css'] = [
      '#attached' => [
        'library' => [
          'user_provisioning/user_provisioning.admin',
        ],
      ],
    ];

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

    $form['mo_user_provisioning_content'] = [
      '#markup' => $this->t('Are you sure you want to remove account? The configurations saved will not be lost.'),
    ];

    $form['actions'] = ['#type' => 'actions'];
    $form['actions']['send'] = [
      '#type' => 'submit',
      '#value' => $this->t('Confirm'),
      '#attributes' => [
        'class' => [
          'use-ajax',
        ],
      ],
      '#ajax' => [
        'callback' => [$this, 'submitModalFormAjax'],
        'event' => 'click',
      ],
    ];

    $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
    return $form;
  }

  /**
   * This method is used to submit the form.
   *
   * @param array $form
   *   The form array.
   * @param FormStateInterface $form_state
   *   The form state.
   *
   * @return AjaxResponse
   *   The AJAX response.
   */
  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_example_form', $form));
    }
    else {
      $this->configFactory->clear('mo_user_provisioning_customer_email')
        ->clear('mo_user_provisioning_customer_phone')
        ->clear('mo_user_provisioning_customer_id')
        ->clear('mo_user_provisioning_customer_api_key')
        ->clear('mo_user_provisioning_customer_token')
        ->set('mo_user_provisioning_status', 'CUSTOMER_SETUP')
        ->save();

      $this->messenger->addMessage($this->t('Your account has been removed successfully!'), 'status');
      $response->addCommand(new RedirectCommand(Url::fromRoute('user_provisioning.customer_setup')->toString()));
    }
    return $response;
  }

  /**
   * This method is used to validate the form.
   *
   * @param array $form
   *   The form array.
   * @param FormStateInterface $form_state
   *   The form state.
   *
   * @return void
   *   The form validation.
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {}

  /**
   * This method is used to submit the form.
   *
   * @param array $form
   *   The form array.
   * @param FormStateInterface $form_state
   *   The form state.
   *
   * @return void
   *   The form submission.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {}

}
