<?php

namespace Drupal\azure_ad\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides the manual user sync form.
 */
class MoManualUserSync extends FormBase {

  /**
   * Drupal to Azure sync form service.
   *
   * @var \Drupal\azure_ad\Form\MoDrupalToAzureSync
   */
  private MoDrupalToAzureSync $drupalToAzureSyncForm;

  /**
   * Constructs a new MoManualUserSync object.
   */
  public function __construct(MoDrupalToAzureSync $drupal_to_azure_sync_form) {
    $this->drupalToAzureSyncForm = $drupal_to_azure_sync_form;
  }

  /**
   * {@inheritDoc}
   */
  public static function create(ContainerInterface $container): static {
    return new static(
      $container->get('azure_ad.form.mo_drupal_to_azure_sync')
    );
  }

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

  /**
   * Builds the form.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   * @param array $options
   *   The options.
   *
   * @return array
   *   The form array.
   */
  public function buildForm(array $form, FormStateInterface $form_state, $options = NULL) {
    $form['#prefix'] = '<div id="modal_support_form">';
    $form['#suffix'] = '</div>';
    $form['status_messages'] = [
      '#type' => 'status_messages',
      '#weight' => -10,
    ];

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

    $form['mo_azure_ad_manual_provisioning_fieldset']['manual_provisioning_choose_operations']['create_user_fieldset'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Sync Users'),
    ];

    $form['mo_azure_ad_manual_provisioning_fieldset']['manual_provisioning_choose_operations']['create_user_fieldset']['mo_azure_drupal_username'] = [
      '#type' => 'entity_autocomplete',
      '#target_type' => 'user',
      '#attributes' => ['placeholder' => 'Search Drupal username of user to sync'],
      '#required' => TRUE,
      '#prefix' => '<p class="mo_azure_highlight_background"><strong>Note:</strong> Search the username of user in Drupal to sync (create) it to the Active Directory.</p><div class="container-inline">',
    ];

    $form['mo_azure_ad_manual_provisioning_fieldset']['manual_provisioning_choose_operations']['create_user_fieldset']['mo_azure_sync_button'] = [
      '#type' => 'submit',
      '#value' => $this->t('Sync'),
      '#button_type' => 'primary',
      '#attributes' => [
        'class' => [
          'use-ajax',
          'button--primary',
          'sync_button',
        ],
      ],
      '#ajax' => [
        'callback' => [$this, 'moAzureManualSync'],
        'event' => 'click',
      ],
    ];

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

  /**
   * Handles form submission.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   */
  public function submitModalFormAjax(array $form, FormStateInterface $form_state) {

  }

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

  /**
   * Handles manual user sync.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   The AJAX response.
   */
  public function moAzureManualSync(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 {
      $this->drupalToAzureSyncForm->moAzureManualSync($form, $form_state);
      $response->addCommand(new ReplaceCommand('#modal_support_form', $form));
    }
    return $response;
  }

}
