<?php

use Drupal\image\Entity\ImageStyle;
use Drupal\Core\Entity\EntityInterface;
use Drupal\file\FileInterface;

// /**
//  * Implements hook_entity_insert().
//  * Generate all image styles once an Image is uploaded.
//  */
// function images_styles_gen_entity_insert(EntityInterface $entity) {
//   /** @var \Drupal\file\Entity\File $entity */
//   if ($entity instanceof FileInterface) {
//     $image = \Drupal::service('image.factory')->get($entity->getFileUri());
//     /** @var \Drupal\Core\Image\Image $image */
//     if ($image->isValid()) {
//       $styles = ImageStyle::loadMultiple();
//       $image_uri = $entity->getFileUri();
//       /** @var \Drupal\image\Entity\ImageStyle $style */
//       foreach ($styles as $style) {
//         $destination = $style->buildUri($image_uri);
//         $style->createDerivative($image_uri, $destination);
//       }
//     }
//   }
// }

/**
 * Implements hook_entity_insert().
 * Queue generation of all image styles once an Image is uploaded.
 */
// function images_styles_gen_entity_insert(EntityInterface $entity) {
//   // TODO: IS THIS EVEN WORKING ????
//   /** @var \Drupal\file\Entity\File $entity */
//   if ($entity instanceof FileInterface) {
//     $image = \Drupal::service('image.factory')->get($entity->getFileUri());
//     /** @var \Drupal\Core\Image\Image $image */
//     if ($image->isValid()) {
//       $queue = \Drupal::queue('img_styles_gen');
//       $item = new \stdClass();
//       $item->fid = $entity->id();
//       $queue->createItem($item);
//       // $data = ['entity' => $entity];
//       // $queue->createItem($data);
//     }
//   }
// }

// function images_styles_gen_cron_DISABLED(){
//   $file_storage = \Drupal::entityTypeManager()->getStorage('file');
//   $query = $file_storage->getQuery()
//     ->condition('filemime', 'image/%', 'LIKE')
//     ->accessCheck(TRUE);
//   $results = $query->execute();
//
//   // $styles_storage = \Drupal::entityTypeManager()->getStorage('image_style');
//   // $styles = $styles_storage->loadMultiple();
//
//   /** @var QueueFactory $queue_factory */
//   $queue_factory = \Drupal::service('queue');
//   /** @var QueueInterface $queue */
//   $queue = $queue_factory->get('img_styles_gen');
//
//   foreach ($results as $fid) {
//     $item = new \stdClass();
//     $item->fid = $fid;
//     $queue->createItem($item);
//
//     // $queue->createItem(["fid" => $fid]);
//
//
//     // /** @var \Drupal\file\FileInterface|null $file*/
//     // $file_entity = \Drupal::entityTypeManager()->getStorage('file')->load($fid);
//     // $image = \Drupal::service('image.factory')->get($file_entity->getFileUri());
//     //
//     // if ($image->isValid()) {
//     //   $image_uri = $file_entity->getFileUri();
//     //   /** @var \Drupal\image\Entity\ImageStyle $style */
//     //   foreach ($styles as $style) {
//     //     $destination = $style->buildUri($image_uri);
//     //     // if destination exists skip
//     //     if (file_exists($destination)) continue; // skip existing files
//     //
//     //     $data = [
//     //       'style' => $style,
//     //       'image_uri' => $image_uri,
//     //       'destination' => $destination
//     //     ];
//     //     $queue->createItem($data);
//     //     \Drupal::logger('images_styles_gen')->notice("created queue from $image_uri to $destination");
//     //   }
//
//     // }
//   }
// }