<?php

namespace Drupal\mymodule\Commands;

use Drush\Commands\DrushCommands;
use Drupal\mymodule\Batch\Import;

/**
 * A Drush command file.
 *
 * @package Drupal\mymodule\Commands
 */
class BatchCommands extends DrushCommands {
  /**
   * Drush command that displays the given text.
   *
   * @param string $time
   * @command mymodule:custom
   * @aliases custom
   * @usage drush custom $time
   */
  public function custom($time = '01.01.1970') {
    $import = new Import();
    $import->getNodes();
    batch_set($import->batch);
    drush_backend_batch_process();

    /*$api = \Drupal::service('mymodule.custom_services');
    $result_api = $api->getResponse('https://swapi.dev/api');
    $queue_api = \Drupal::queue('mymodule_swapi_drush');
    $queue_api->deleteQueue();
    $count_create_node = 0;
    $count_queue = 0;

    foreach($result_api as $item) {
      $bundle = $api->getTypeNode($item);
      if ($api->checkBundle($bundle)) {
        $uri = $item;
        while (!empty($uri)) {
          $result = $api->getResponse($uri);
          foreach($result['results'] as $value) {
            $node = $api->getNodeByUrl($value['url']);
            if (!empty($node)) {
              $swapi_edited = date_parse($value['edited']);
              $swapi_edited = $swapi_edited['day'] . "." . $swapi_edited['month'] . "." . $swapi_edited['year'];
              $swapi_edited = strtotime($swapi_edited);
              $time = strtotime($time);

              if ($time < $swapi_edited) {
                $queue_api->createItem($value);
                $count_queue++;
              }
            } else {
              $api->createNode($value);
              $title = !empty($value['title']) ? $value['title'] : $value['name'];
              $this->output()->writeln("Страница $title создана");
              $count_create_node++;
              $queue_api->createItem($value);
            }
          }
          $uri = $result['next'];
        }
      } else {
        $this->output()->writeln("Content type $bundle не создан");
      }
    }

    while ($item = $queue_api->claimItem()) {
      if (!empty($item->data['url'])) {
        $node = $api->getNodeByUrl($item->data['url']);
        $api->edited($item->data, $node);

        $title = !empty($item->data['title']) ? $item->data['title'] : $item->data['name'];
        $message = "Страница $title обновлена";
        $this->output()->writeln($message);
        $queue_api->deleteItem($item);
      }
    }

    $this->output()->writeln("Страниц обновлено " . $count_queue . ", страниц создано " . $count_create_node);*/
  }
}
