<?php

namespace Drupal\Core\Entity;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\Url;
use Drupal\Component\Serialization\Json;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\Query\QueryInterface;

/**
 * Defines a generic implementation to build a listing of entities.
 *
 * @ingroup entity_api
 */
class EntityListBuilder extends EntityHandlerBase implements EntityListBuilderInterface, EntityHandlerInterface {

  use MessengerTrait;
  use RedirectDestinationTrait;

  /**
   * The entity storage class.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $storage;

  /**
   * The entity type ID.
   *
   * @var string
   */
  protected $entityTypeId;

  /**
   * Information about the entity type.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface
   */
  protected $entityType;

  /**
   * The number of entities to list per page, or FALSE to list all entities.
   *
   * For example, set this to FALSE if the list uses client-side filters that
   * require all entities to be listed (like the views overview).
   *
   * @var int|false
   */
  protected $limit = 50;

  /**
   * The entity key to sort the list by.
   *
   * This can be overridden in child classes to sort the list by a different
   * entity key.
   */
  protected const SORT_KEY = 'id';

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static(
      $entity_type,
      $container->get('entity_type.manager')->getStorage($entity_type->id())
    );
  }

  /**
   * Constructs a new EntityListBuilder object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage) {
    $this->entityTypeId = $entity_type->id();
    $this->storage = $storage;
    $this->entityType = $entity_type;
  }

  /**
   * {@inheritdoc}
   */
  public function getStorage() {
    return $this->storage;
  }

  /**
   * {@inheritdoc}
   */
  public function load() {
    $entity_ids = $this->getEntityIds();
    return $this->storage->loadMultiple($entity_ids);
  }

  /**
   * Loads entity IDs using a pager sorted by the entity id.
   *
   * @return array
   *   An array of entity IDs.
   */
  protected function getEntityIds() {
    return $this->getEntityListQuery()->execute();
  }

  /**
   * Returns a query object for loading entity IDs from the storage.
   *
   * @return \Drupal\Core\Entity\Query\QueryInterface
   *   A query object used to load entity IDs.
   */
  protected function getEntityListQuery(): QueryInterface {
    $query = $this->getStorage()->getQuery()
      ->accessCheck(TRUE)
      ->sort($this->entityType->getKey(static::SORT_KEY));

    // Only add the pager if a limit is specified.
    if ($this->limit) {
      $query->pager($this->limit);
    }
    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function getOperations(EntityInterface $entity/* , ?CacheableMetadata $cacheability = NULL */) {
    $args = func_get_args();
    $cacheability = $args[1] ?? new CacheableMetadata();
    $operations = $this->getDefaultOperations($entity, $cacheability);
    $operations += $this->moduleHandler()->invokeAll('entity_operation', [$entity, $cacheability]);
    $this->moduleHandler->alter('entity_operation', $operations, $entity, $cacheability);
    uasort($operations, '\Drupal\Component\Utility\SortArray::sortByWeightElement');

    return $operations;
  }

  /**
   * Gets this list's default operations.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity the operations are for.
   * phpcs:disable Drupal.Commenting
   * @todo Uncomment new method parameters before drupal:12.0.0.
   * @see https://www.drupal.org/project/drupal/issues/3533078
   * @param \Drupal\Core\Cache\CacheableMetadata|null $cacheability
   *   The cacheable metadata to add to if your operations vary by or depend on
   *   something.
   * phpcs:enable
   *
   * @return array
   *   The array structure is identical to the return value of
   *   self::getOperations().
   */
  protected function getDefaultOperations(EntityInterface $entity/* , ?CacheableMetadata $cacheability = NULL */) {
    $args = func_get_args();
    $cacheability = $args[1] ?? new CacheableMetadata();
    $operations = [];
    $variables = [
      '@entity_label' => $entity->label() ?? '',
      '@entity_bundle' => $entity->bundle(),
      '@entity_id' => $entity->id(),
    ];

    $update_access = $entity->access('update', return_as_object: TRUE);
    $cacheability->addCacheableDependency($update_access);
    if ($update_access->isAllowed() && $entity->hasLinkTemplate('edit-form')) {
      $edit_url = $this->ensureDestination($entity->toUrl('edit-form'));
      $label = $entity->label()
        ? $this->t('Edit @entity_label', $variables)
        : $this->t('Edit @entity_bundle @entity_id', $variables);
      $attributes = $edit_url->getOption('attributes') ?: [];
      $attributes += ['aria-label' => $label];
      $edit_url->setOption('attributes', $attributes);

      $operations['edit'] = [
        'title' => $this->t('Edit'),
        'weight' => 10,
        'url' => $edit_url,
      ];
    }

    $delete_access = $entity->access('delete', return_as_object: TRUE);
    $cacheability->addCacheableDependency($delete_access);
    if ($delete_access->isAllowed() && $entity->hasLinkTemplate('delete-form')) {
      $delete_url = $this->ensureDestination($entity->toUrl('delete-form'));
      $label = $entity->label()
        ? $this->t('Delete @entity_label', $variables)
        : $this->t('Delete @entity_bundle @entity_id', $variables);
      $attributes = $delete_url->getOption('attributes') ?: [];
      $attributes += ['aria-label' => $label];
      $delete_url->setOption('attributes', $attributes);

      $operations['delete'] = [
        'title' => $this->t('Delete'),
        'weight' => 100,
        'attributes' => [
          'class' => ['use-ajax'],
          'data-dialog-type' => 'modal',
          'data-dialog-options' => Json::encode([
            'width' => 880,
          ]),
        ],
        'url' => $delete_url,
      ];
    }

    $view_access = $entity->access('view', return_as_object: TRUE);
    $cacheability->addCacheableDependency($view_access);
    if ($view_access->isAllowed() && $entity->hasLinkTemplate('canonical')) {
      $view_url = $entity->toUrl('canonical');
      $label = $entity->label()
        ? $this->t('View @entity_label', $variables)
        : $this->t('View @entity_bundle @entity_id', $variables);
      $attributes = $view_url->getOption('attributes') ?: [];
      $attributes += ['aria-label' => $label];
      $view_url->setOption('attributes', $attributes);

      $operations['view'] = [
        'title' => $this->t('View'),
        'url' => $view_url,
        'weight' => 200,
      ];
    }

    return $operations;
  }

  /**
   * Builds the header row for the entity listing.
   *
   * @return array
   *   A render array structure of header strings.
   *
   * @see \Drupal\Core\Entity\EntityListBuilder::render()
   */
  public function buildHeader() {
    $row['operations'] = $this->t('Operations');
    return $row;
  }

  /**
   * Builds a row for an entity in the entity listing.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity for this row of the list.
   *
   * @return array
   *   A render array structure of fields for this entity.
   *
   * @see \Drupal\Core\Entity\EntityListBuilder::render()
   */
  public function buildRow(EntityInterface $entity) {
    $row['operations']['data'] = $this->buildOperations($entity);
    return $row;
  }

  /**
   * Builds a renderable list of operation links for the entity.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity on which the linked operations will be performed.
   *
   * @return array
   *   A renderable array of operation links.
   *
   * @see \Drupal\Core\Entity\EntityListBuilder::buildRow()
   */
  public function buildOperations(EntityInterface $entity) {
    $cacheability = new CacheableMetadata();
    $build = [
      '#type' => 'operations',
      '#links' => $this->getOperations($entity, $cacheability),
      // Allow links to use modals.
      '#attached' => [
        'library' => ['core/drupal.dialog.ajax'],
      ],
    ];
    $cacheability->applyTo($build);
    return $build;
  }

  /**
   * {@inheritdoc}
   *
   * Builds the entity listing as renderable array for table.html.twig.
   *
   * @todo Add a link to add a new item to the #empty text.
   */
  public function render() {
    $build['table'] = [
      '#type' => 'table',
      '#header' => $this->buildHeader(),
      '#title' => $this->getTitle(),
      '#rows' => [],
      '#empty' => $this->t('There are no @label yet.', ['@label' => $this->entityType->getPluralLabel()]),
      '#cache' => [
        'contexts' => $this->entityType->getListCacheContexts(),
        'tags' => $this->entityType->getListCacheTags(),
      ],
    ];
    foreach ($this->load() as $entity) {
      if ($row = $this->buildRow($entity)) {
        $build['table']['#rows'][$entity->id()] = $row;
      }
    }

    // Only add the pager if a limit is specified.
    if ($this->limit) {
      $build['pager'] = [
        '#type' => 'pager',
      ];
    }
    return $build;
  }

  /**
   * Gets the title of the page.
   */
  protected function getTitle() {}

  /**
   * Ensures that a destination is present on the given URL.
   *
   * @param \Drupal\Core\Url $url
   *   The URL object to which the destination should be added.
   *
   * @return \Drupal\Core\Url
   *   The updated URL object.
   */
  protected function ensureDestination(Url $url) {
    return $url->mergeOptions(['query' => $this->getRedirectDestination()->getAsArray()]);
  }

}
