<?php

namespace Drupal\azure_ad\Form;

use Drupal\azure_ad\Helper\moAzureADHelper;
use Drupal\azure_ad\moAzureConstants;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;
use Drupal\user_provisioning\moUserProvisioningConstants;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides the upgrade plans form for Azure AD.
 */
class MoAzureUpgradePlans extends FormBase {

  /**
   * Constructs the form.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack service.
   */
  public function __construct(RequestStack $request_stack) {
    $this->requestStack = $request_stack;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
          $container->get('request_stack')
      );
  }

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

  /**
   * 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['markup_library'] = [
      '#attached' => [
        'library' => [
          "user_provisioning/user_provisioning.admin",
          'azure_ad/azure_ad.admin',
          'core/drupal.dialog.ajax',
        ],
      ],
    ];

    $baseUrl = Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString();

    $request = $this->requestStack->getCurrentRequest();
    $referer = $request->headers->get('referer');
    $referer = !empty($referer) ? $referer : $baseUrl . '/azure_ad/overview';

    $form['mo_user_provisioning_back_button'] = [
      '#type' => 'link',
      '#title' => $this->t('&#11164; &nbsp;Back to Previous tab'),
      '#url' => Url::fromUri($referer),
      '#attributes' => ['class' => ['button', 'button--danger']],
    ];

    $form['mo_user_provisioning_header'] = [
      '#markup' => '<div><div><br><div class="container-inline mo_user_provisioning_upgrade_background_note"><h1>&nbsp; ' . $this->t('UPGRADE PLANS&nbsp;') . '</h1></div><br><br>',
    ];
    $url = 'mailto:' . moUserProvisioningConstants::SUPPORT_EMAIL . moAzureConstants::SUPPORT_QUERY_CONTENT;
    $features = [
      [
        Markup::create('<br><h1>' . $this->t('FREE') . '</h1><a class="button" disabled>' . $this->t('You are on this Plan') . '</a><br><br>'),
        Markup::create($this->t(
            '<br><h1>PREMIUM</h1>
   <a class="button button--primary" target="_blank" href=":url">✉ Contact Us</a>
   <br><br>',
            [
              ':url' => $url,
            ]
        )),
      ],
      [
        Markup::create('<h4>' . $this->t('FEATURE LIST') . '</h4>'), Markup::create('<h4>' . $this->t('FEATURE LIST') . '</h4>'),
      ],
      [
              // Features of Free version.
        Markup::create($this->t(
            '<div class="mo_user_provisioning_feature_list">
    <ul class="checkmark">
      <li>@item2</li>
      <li>@item3</li>
      <li>@item4</li>
    </ul>
  </div>',
            [
              '@item2' => 'Automatic / Real Time provisioning for Creating Users from Drupal to Azure AD',
              '@item3' => 'Manual / On-demand provisioning for Creating Users from Drupal to Azure AD',
              '@item4' => 'Audits and Logs',
            ]
        )),

              // Features of Premium version.
        Markup::create($this->t(
            '<br><h3>@title</h3><h2> + </h2> <br>
   <div class="mo_user_provisioning_feature_list">
    <ul class="checkmark">
      <li>@item1</li>
      <li>@item2</li>
      <li>@item3</li>
      <li>@item4</li>
      <li>@item5</li>
      <li>@item6</li>
      <li>@item7</li>
    </ul>
   </div>',
            [
              '@title' => $this->t('ALL THE FEATURES OF FREE'),
              '@item1' => $this->t('Automatic / Real Time Provisioning for all CRUD operations from Drupal to Azure AD'),
              '@item2' => $this->t('Manual / On-demand provisioning for all CRUD operations from Drupal to Azure AD'),
              '@item3' => $this->t('Scheduler / Cron based Provisioning for all CRUD operations from Drupal to Azure AD'),
              '@item4' => $this->t('Basic and Advanced User Attribute Mapping'),
              '@item5' => $this->t('Profile Picture Sync from Azure AD to Drupal'),
              '@item6' => $this->t('Manual / On-demand provisioning for all CRUD operations from Azure AD to Drupal'),
              '@item7' => $this->t('Scheduler / Cron based Provisioning for all CRUD operations from Azure AD to Drupal'),
            ]
        )),
      ],
    ];

    $form['miniorange_oauth_login_feature_list'] = [
      '#type' => 'table',
      '#responsive' => TRUE,
      '#rows' => $features,
      '#size' => 3,
      '#attributes' => ['class' => 'mo_upgrade_plans_features mo_user_prov_feature_table'],
    ];

    $form['mo_user_provisioning_instance_note'] = [
      '#type' => 'fieldset',
      '#prefix' => '<br>',
    ];

    $form['mo_user_provisioning_instance_note']['miniorange_oauth_client_instance_based'] = [
      '#markup' => '<div class="mo_instance_note"><b>*</b>' . $this->t('&nbsp;This module follows an&nbsp;') . '<b>' . $this->t('Instance and Subscription Based') . '</b>' . $this->t('&nbsp;licensing structure. The listed prices are for purchase of a single instance. If you are planning to use the module on multiple instances, you can check out the bulk purchase discount on our website.') . '</div><br>
                                        <div class="mo_user_provisioning_highlight_background"><b><u>' . $this->t('What is an Instance:') . '</u></b>' . $this->t('&nbsp;A Drupal instance refers to a single installation of a Drupal site. It refers to each individual website where the module is active. In the case of multisite/subsite Drupal setup, each site with a separate database will be counted as a single instance. For eg. If you have the dev-staging-prod type of environment then you will require 3 licenses of the module (with additional discounts applicable on pre-production environments).') . '</div>',
    ];

    $azure_ad_helper = new moAzureADHelper();
    $azure_ad_helper->moAzureShowCustomerSupportIcon($form, $form_state);

    return $form;
  }

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

}
