<?php

namespace Drupal\trash\Routing;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\trash\TrashManagerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
 * Subscriber for Trash routes.
 */
class RouteSubscriber extends RouteSubscriberBase {

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

  /**
   * The trash manager service.
   *
   * @var \Drupal\trash\TrashManagerInterface
   */
  protected $trashManager;

  /**
   * Constructs a RouteSubscriber object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, TrashManagerInterface $trash_manager) {
    $this->entityTypeManager = $entity_type_manager;
    $this->trashManager = $trash_manager;
  }

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
      if ($this->trashManager->isEntityTypeEnabled($entity_type)) {
        if ($entity_type->hasLinkTemplate('canonical')) {
          $base_path = $entity_type->getLinkTemplate('canonical');
        }
        else {
          $base_path = "/admin/content/trash/$entity_type_id/{" . $entity_type_id . '}';
        }

        $parameters = [
          $entity_type_id => [
            'type' => "entity:$entity_type_id",
          ],
        ];

        // Add a route for the restore form.
        $route = new Route($base_path . '/restore');
        $route
          ->addDefaults([
            '_entity_form' => "{$entity_type_id}.restore",
            'entity_type_id' => $entity_type_id,
          ])
          ->setRequirements([
            '_permission' => 'access trash',
          ])
          ->setOption('parameters', $parameters)
          ->setOption('_admin_route', TRUE)
          ->setOption('_trash_route', TRUE);
        $collection->add("entity.$entity_type_id.restore", $route);

        // Add a route for the purge form.
        $route = new Route($base_path . '/purge');
        $route
          ->addDefaults([
            '_entity_form' => "{$entity_type_id}.purge",
            'entity_type_id' => $entity_type_id,
          ])
          ->setRequirements([
            '_permission' => 'access trash',
          ])
          ->setOption('parameters', $parameters)
          ->setOption('_admin_route', TRUE)
          ->setOption('_trash_route', TRUE);
        $collection->add("entity.$entity_type_id.purge", $route);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents(): array {
    $events = parent::getSubscribedEvents();
    $events[RoutingEvents::ALTER] = ['onAlterRoutes', -120];
    return $events;
  }

}