<?php

namespace Drupal\acquia_contenthub_publisher;

use Drupal\acquia_contenthub\Libs\Common\ContentHubQueueBase;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Queue\QueueWorkerManager;
use Drupal\Core\Queue\SuspendQueueException;
use Drupal\Core\Render\RendererInterface;

/**
 * Implements an Export Queue for Content Hub.
 */
class ContentHubExportQueue extends ContentHubQueueBase {

  /**
   * Name of the export queue.
   */
  public const QUEUE_NAME = 'acquia_contenthub_publish_export';

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * {@inheritdoc}
   */
  public function __construct(QueueFactory $queue_factory, QueueWorkerManager $queue_manager, MessengerInterface $messenger, RendererInterface $renderer) {
    parent::__construct($queue_factory, $queue_manager, $messenger);
    $this->renderer = $renderer;
  }

  /**
   * {@inheritdoc}
   */
  public static function batchProcess(string $queue_name, array &$context): void {
    $queue_factory = \Drupal::service('queue');
    $queue_manager = \Drupal::service('plugin.manager.queue_worker');
    $queue = $queue_factory->get($queue_name);

    $queue_worker = $queue_manager->createInstance($queue_name);
    /** @var \stdClass $item */
    $item = $queue->claimItem();
    if (!$item) {
      return;
    }

    try {
      // Generating a list of entities.
      $msg_label = t('(@entity_type, @entity_id)', [
        '@entity_type' => $item->data->type,
        '@entity_id' => $item->data->uuid,
      ]);

      // Process item.
      $entities_processed = $queue_worker->processItem($item->data);
      if ($entities_processed == FALSE) {
        // Indicate that the item could not be processed.
        if ($entities_processed === FALSE) {
          $message = t('There was an error processing entities: @entities and their dependencies. The item has been sent back to the queue to be processed again later. Check your logs for more info.', [
            '@entities' => $msg_label,
          ]);
        }
        else {
          $message = t('No processing was done for entities: @entities and their dependencies. The item has been sent back to the queue to be processed again later. Check your logs for more info.', [
            '@entities' => $msg_label,
          ]);
        }
        $context['message'] = $message->jsonSerialize();
        $context['results'][] = $message->jsonSerialize();
      }
      else {
        // If everything was correct, delete processed item from the queue.
        $queue->deleteItem($item);

        // Creating a text message to present to the user.
        $message = t('Processed entities: @entities and their dependencies (@count @label sent).', [
          '@entities' => $msg_label,
          '@count' => $entities_processed,
          '@label' => $entities_processed == 1 ? t('entity') : t('entities'),
        ]);
        $context['message'] = $message->jsonSerialize();
        $context['results'][] = $message->jsonSerialize();
      }
    }
    catch (SuspendQueueException $e) {
      // If there was an Exception thrown because of an error
      // Releases the item that the worker could not process.
      // Another worker can come and process it.
      $queue->releaseItem($item);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function batchFinished(bool $success, array $results, array $operations): void {
    parent::batchFinished($success, $results, $operations);

    // Providing a report on the items processed by the queue.
    $elements = [
      '#theme' => 'item_list',
      '#type' => 'ul',
      '#items' => $results,
    ];
    $renderer = \Drupal::service('renderer');
    $messenger = \Drupal::messenger();
    $queue_report = $renderer->render($elements);
    $messenger->addMessage($queue_report);
  }

  /**
   * {@inheritdoc}
   */
  protected function getQueueName(): string {
    return self::QUEUE_NAME;
  }

}
