<?php

namespace Drupal\ufc\Plugin\QueueWorker;

use Drupal\Core\Annotation\QueueWorker;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\ufc\Fighter;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Custom Queue Worker.
*
* @QueueWorker(
*   id = "fighter_import",
*   title = @Translation("Fighter Import Queue"),
*   cron = {"time" = 2400}
* )
*/
final class FighterQueueWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {

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

  /**
   * Main constructor.
   *
   * @param array $configuration
   *   Configuration array.
   * @param mixed $plugin_id
   *   The plugin id.
   * @param mixed $plugin_definition
   *   The plugin definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    EntityTypeManagerInterface $entityTypeManager
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * Used to grab functionality from the container.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The container.
   * @param array $configuration
   *   Configuration array.
   * @param mixed $plugin_id
   *   The plugin id.
   * @param mixed $plugin_definition
   *   The plugin definition.
   * @return static
   */
  public static function create(
    ContainerInterface $container,
    array $configuration,
    $plugin_id,
    $plugin_definition
  ) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('entity_type.manager'),
      $container->get('database'),
    );
  }

  /**
   * Processes an item in the queue.
   *
   * @param mixed $data
   *   The queue item data.
   *
   */
  public function processItem($data): void {
    $this->doProcess($data);
  }

  /**
   * Do the actual processing of an item.
   *
   * @param $item
   *   The item to process.
   */
  private function doProcess($item): bool {
    // @todo:
    // Item->id will be the ID of the fighter.
    // Need to fetch fighter list from cache.
    // Then get the corred fighter based on the id.
    // Then update or create the fighter node, including any media.
    // these values will need to be retrieves from the existing data.
    /* $node_lookup = reset(\Drupal::entityTypeManager()->getStorage('node')->loadByProperties(['title' => $title])); */
    /* if (!empty($node_lookup)) { */
    /*   // Update instead of create. */
    /*   $fighter->updatePlayerNode($node_lookup->id()); */
    /*   \Drupal::logger('ufc')->notice("$title updated successfully."); */
    /* } */
    /* else { */
    /*   \Drupal::logger('ufc')->warning("No existing player found for $title...creating"); */
    /*   $fighter->createPlayerNode(); */
    /* } */
    return TRUE;
  }

}
