<?php

namespace Drupal\user_provisioning\Form;

use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Application selection form for provisioning setup.
 */
class MoUserProvisioningChooseApp extends FormBase {

  /**
   * The site base URL.
   *
   * @var string
   *   The site base URL.
   */
  private string $baseUrl;

  /**
   * The module image path URL.
   *
   * @var string
   *   The module image path URL.
   */
  private string $urlPath;

  /**
   * The current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   *   The current request object.
   */
  private Request $request;

  /**
   * The module extension list service.
   *
   * @var \Drupal\Core\Extension\ModuleExtensionList
   */
  protected $moduleExtensionList;

  /**
   * Constructs the choose app form.
   */
  public function __construct(RequestStack $request_stack, ModuleExtensionList $module_extension_list) {
    $this->requestStack = $request_stack;
    $this->request = $this->requestStack->getCurrentRequest();
    $this->baseUrl = $this->request->getSchemeAndHttpHost() . $this->request->getBaseUrl();
    $this->moduleExtensionList = $module_extension_list;
    $this->urlPath = $this->baseUrl . '/' . $this->moduleExtensionList->getPath('user_provisioning') . '/images';
  }

  /**
   * Creates the class instance.
   */
  public static function create(ContainerInterface $container): MoUserProvisioningChooseApp|static {
    return new static(
          $container->get('request_stack'),
          $container->get('extension.list.module'),
      );
  }

  /**
   * {@inheritDoc}
   */
  public function getFormId(): string {
    return 'mo_provisioning_choose_app';
  }

  /**
   * {@inheritDoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $app_name = NULL) {
    $form['#prefix'] = '<div id="modal_example_form">';
    $form['#suffix'] = '</div>';
    $form['status_messages'] = [
      '#type' => 'status_messages',
      '#weight' => -10,
    ];

    $app = $this->appDisplayName($app_name);

    $rows = [
      [
        Markup::create('<div class="mo_azure_step_1_img mo_user_provisioning_text_center"><h4>Drupal to ' . $app . ' Sync</h4>'),
        Markup::create('<div class="mo_azure_step_1_img mo_user_provisioning_text_center"><h4>' . $app . ' to Drupal Sync</h4>'),
      ],
      [
        Markup::create('<a href="?app_name=' . htmlspecialchars($app_name, ENT_QUOTES, 'UTF-8') . '"><div><img class="mo_azure_sync_gif" src="' . $this->urlPath . '/drupal_to_scim.gif" alt="Drupal to SCIM Sync"></div></a>'),
        Markup::create('<a href="?app_name=scim_server"><div><img class="mo_azure_sync_gif" src="' . $this->urlPath . '/scim_to_drupal.gif" alt="SCIM to Drupal Sync"></div></a>'),
      ],
    ];

    $form['azure_ad_step1_how_to_sync'] = [
      '#type' => 'table',
      '#responsive' => TRUE,
      '#rows' => $rows,
      '#attributes' => [
        'style' => 'border-collapse: separate;',
        'class' => ['how_to_perform_sync'],
      ],
    ];

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

    return $form;
  }

  /**
   * {@inheritDoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // @todo Implement submitForm() method.
  }

  /**
   * Returns display label for a provider key.
   */
  private function appDisplayName($app_name): string {
    $appDisplay = [
      'aws_sso' => 'AWS SSO',
      'azure_ad' => 'Azure AD',
      'okta' => 'Okta',
      'onelogin' => 'OneLogin',
      'google' => 'Google Apps',
      'cyberark' => 'CyberArk',
      'miniorange' => 'miniOrange',
      'jumpcloud' => 'JumpCloud',
      'ping' => 'PingOne',
      'centrify' => 'Centrify',
      'salesforce' => 'Salesforce',
      'oracle' => 'Oracle',
      'drupal' => 'Drupal',
      'joomla' => 'Joomla',
      'wordpress' => 'WordPress',
      'alexis' => 'AlexisHR Provisioning',
      'anaplan' => 'Anaplan SCIM API',
      'blink' => 'Blink',
      'calendly' => 'Calendly',
      'circlehd' => 'CircleHD',
      'curity' => 'Curity Identity Server',
      'federated_dir' => 'Federated Directory',
      'fusionauth' => 'FusionAuth',
      'github' => 'GitHub Business',
      'idaas' => 'idaas.nl',
      'monokee' => 'Monokee',
      'netiq' => 'NetIQ Identity Manager',
      'workday' => 'Workday Peakon',
      'reward' => 'Reward Gateway',
      'sailpoint' => 'SailPoint',
      'sap' => 'SAP',
      'trello' => 'Trello',
      'wso2' => 'WSO2 Charon',
      'custom' => 'Custom Provider',
    ];

    if (!isset($appDisplay[$app_name])) {
      throw new AccessDeniedHttpException();
    }

    return $appDisplay[$app_name];
  }

}
