<?php

namespace Drupal\uw_cfg_common\Form;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Form class for the content access form.
 */
class UwContentModerationForm extends ConfirmFormBase {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The nid (node id).
   *
   * @var int
   */
  protected $nid;

  /**
   * The vid (version id).
   *
   * @var int
   */
  protected $vid;

  /**
   * Class constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    // Instantiates this form class.
    return new static(
      $container->get('entity_type.manager')
    );
  }


  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'uw_content_moderation_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $nid = NULL, $vid = NULL) {

    // Set the node and version ids.
    $this->nid = $nid;
    $this->vid = $vid;

    // Return the form from the parent (confirm form).
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Set the message that the permissions have been saved.
    $this->messenger()->addStatus($this->t('The changes have been saved.'));
  }

  /**
   * Returns the question to ask the user.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   *   The form question. The page title will be set to this value.
   */
  public function getQuestion() {

    // Get the node object.
    $node = $this->entityTypeManager->getStorage('node')->load($this->nid);

    // Return the question to see if they want to publish the node.
    return t('Are you sure you want to unpublish %node_title?', ['%node_title' => $node->getTitle()]);
  }

  /**
   * Returns the route to go to if the user cancels the action.
   *
   * @return \Drupal\Core\Url
   *   A URL object.
   */
  public function getCancelUrl() {

    // Set the options for the URL.
    $options = ['absolute' => TRUE];

    // Return the URL back to the node.
    return Url::fromRoute('entity.node.canonical', ['node' => $this->nid], $options);
  }
}