<?php
namespace nur\io\fsv;

use IteratorAggregate;
use nur\A;
use nur\b\ICloseable;
use nur\b\io\EOFException;
use nur\b\io\IReader;
use nur\b\io\Tfilter;
use nur\b\params\Parametrable;
use nur\b\params\Tparametrable;
use nur\b\ValueException;
use nur\io\Tencoding;
use nur\reader;
use nur\str;

/**
 * Class FsvReader: produit un flux de données à partir d'une source au format
 * FSV.
 *
 * --autogen-properties-and-methods--
 * @method int getSkipLines()
 * @method setInput($value)
 * @method array setSchema(array $value)
 * @method string|null setInputEncoding(?string $value)
 * @method string|null setOutputEncoding(?string $value)
 * @method string|null setMapEmpty(?string $value)
 * @method bool setOutputSeq(bool $value)
 * @method int setSkipLines(int $value)
 */
class FsvReader extends Parametrable implements IteratorAggregate, ICloseable {
  use Tparametrable, Tfilter;

  function __construct($input=null, ?array $params=null) {
    A::set_nn($params, "input", $input);
    $this->schema = new FsvSchema();
    parent::__construct($params);
  }

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "input" => [null, null, "fichier en entrée"],
    "schema" => ["array", null, "schéma des données"],
    "input_encoding" => ["?string", null, "encoding en entrée"],
    "output_encoding" => ["?string", null, "encoding en sortie"],
    "map_empty" => ["?string", null, "valeur adoptée pour les chaines vides"],
    "output_seq" => ["bool", null, "faut-il générer en-têtes et données en tableaux séquentiels?"],
    "skip_lines" => ["int", 0, "nombre de lignes à sauter en entrée"],
  ];

  protected $ppInput;

  function pp_setInput($input): void {
    if ($input instanceof IReader) $this->reader = $input;
    else $this->ppInput = $input;
  }

  /** @var FsvSchema */
  protected $schema;

  function pp_setSchema($schema): void {
    if ($schema instanceof FsvSchema) $this->schema = $schema;
    elseif (is_array($schema)) $this->schema->setSchema($schema);
    elseif ($schema !== null) throw ValueException::invalid_value($schema, "schema");
  }
  function pp_setInputEncoding($inputEncoding): void {
    $this->schema->setInputEncoding($inputEncoding);
  }
  function pp_setOutputEncoding($outputEncoding): void {
    $this->schema->setDataEncoding($outputEncoding);
  }
  function pp_setMapEmpty($mapEmpty): void {
    $this->schema->setMapEmpty($mapEmpty);
  }
  function pp_setOutputSeq(bool $outputSeq): void {
    $this->schema->setOutputSeq($outputSeq);
  }

  /** @var int */
  protected $ppSkipLines;

  /** @var IReader */
  protected $reader;

  protected function ensureOpen(): IReader {
    if ($this->reader === null) {
      $this->reader = reader::with($this->ppInput);
      $this->_rwAppendFilters($this->reader);
    }
    return $this->reader;
  }

  protected function _parseLine(): bool {
    if ($this->ppSkipLines > 0) {
      $this->ppSkipLines--;
      return false;
    }
    return true;
  }

  protected function _outputKeys(bool $reset=true): bool {
    return $this->schema->_outputKeys($reset);
  }

  protected function _getKeys(): array {
    return $this->schema->_getKeys();
  }

  protected function _parseColumns(string $line): array {
    return $this->schema->parseRow($line);
  }

  function getIterator() {
    $reader = $this->ensureOpen();
    try {
      $resource = $reader->getResource();
      if ($resource !== null) {
        while (!$this->_parseLine()) {
          if (fgets($resource) === false) break;
        }
        if ($this->_outputKeys()) yield $this->_getKeys();
        while (($line = fgets($resource)) !== false) {
          $line = str::strip_nl($line);
          yield $this->_parseColumns($line);
        }
      } else {
        while (true) {
          try {
            $line = $reader->readLine();
          } catch (EOFException $e) {
            break;
          }
          if (!$this->_parseLine()) continue;
          if ($this->_outputKeys()) yield $this->_getKeys();
          yield $this->_parseColumns($line);
        }
      }
    } finally {
      $this->close();
    }
  }

  function close(): void {
    if ($this->reader !== null) {
      $this->reader->close();
      $this->reader = null;
    }
  }

  #############################################################################
  const _AUTOGEN_CONSTS = [
    "" => [self::class, "_AUTOGEN_CONSTS", self::class],
  ];
  const _AUTOGEN_LITERALS = /*autogen*/[
    [
      \nur\b\params\parametrable_utils::class,
      '\\nur\\b\\params\\parametrable_utils::class',
    ],
    [self::class, 'self::class'],
    [
      self::PARAMETRABLE_PARAMS_SCHEMA,
      'self::PARAMETRABLE_PARAMS_SCHEMA',
    ],
  ];
  const _AUTOGEN_METHODS = /*autogen*/[
    [
      \nur\b\params\parametrable_utils::class,
      '_autogen_methods_getters',
      self::PARAMETRABLE_PARAMS_SCHEMA,
      self::class,
    ],
    [
      \nur\b\params\parametrable_utils::class,
      '_autogen_methods_setters',
      self::PARAMETRABLE_PARAMS_SCHEMA,
      self::class,
    ],
  ];
  const _AUTO_GETTERS = /*autogen*/[
    'getSkipLines' => 'skip_lines',
  ];
  const _AUTO_SETTERS = /*autogen*/[
    'setInput' => 'input',
    'setSchema' => 'schema',
    'setInputEncoding' => 'input_encoding',
    'setOutputEncoding' => 'output_encoding',
    'setMapEmpty' => 'map_empty',
    'setOutputSeq' => 'output_seq',
    'setSkipLines' => 'skip_lines',
  ];
  #--autogen-dynamic--
}