<?php
namespace nur\io\csv;

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\data\types\Metadata;
use nur\io\Tencoding;
use nur\reader;
use nur\ref\ref_csv;

/**
 * Class CsvReader: produit un flux de données à partir d'une source au format
 * CSV.
 *
 * --autogen-properties-and-methods--
 * @method string|null getInputEncoding()
 * @method string|null getOutputEncoding()
 * @method setInput($value)
 * @method string|null setInputEncoding(?string $value)
 * @method string|null setOutputEncoding(?string $value)
 * @method string|null setFlavour(?string $value)
 * @method bool setMultiSchema(bool $value)
 * @method int setSkipLines(int $value)
 * @method bool|null setParseHeaders(?bool $value)
 * @method array|null setHeaders(?array $value)
 * @method setHeaderMappings($value)
 * @method string|null setMapEmpty(?string $value)
 */
class CsvReader extends Parametrable implements IteratorAggregate, ICloseable {
  use Tparametrable, Tencoding, Tfilter;

  const FLAVOUR = ref_csv::OO_FLAVOUR;

  function __construct($input=null, ?array $params=null) {
    A::set_nn($params, "input", $input);
    self::set_parametrable_params_defaults($params, [
      "flavour" => static::FLAVOUR,
    ]);
    $this->helper = new Csv2AssocHelper();
    parent::__construct($params);
  }

  function setEncodingFilter(string $from, string $to="utf-8"): void {
    $this->_setEncodingFilter($from, $to);
  }

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "input" => [null, null, "fichier en entrée"],
    "input_encoding" => ["?string", null, "encoding en entrée"],
    "output_encoding" => ["?string", null, "encoding en sortie"],
    "flavour" => ["?string", null, "type de fichier CSV"],
    "multi_schema" => ["bool", false, "les flux multi-schémas sont-ils supportés?"],
    "skip_lines" => ["int", 0, "nombre de lignes à sauter en entrée"],
    "parse_headers" => ["?bool", null, "faut-il analyser la première ligne pour calculer les en-têtes?"],
    "headers" => ["?array", null, "liste et ordre des en-têtes en entrée"],
    "header_mappings" => [null /*string|array*/, null, "mappings des en-têtes"],
    "map_empty" => ["?string", null, "valeur de remplacement pour les chaines vides"],
  ];

  protected $ppInput;

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

  protected $ppFlavour;

  function pp_setFlavour(string $flavour): void {
    $this->ppFlavour = flavours::verifix($flavour);
  }

  /** @var Csv2AssocHelper */
  protected $helper;
  function pp_setMultiSchema(bool $multiSchema) { $this->helper->setMultiSchema($multiSchema); }
  function pp_setSkipLines(int $skipLines) { $this->helper->setSkipLines($skipLines); }
  function pp_setParseHeaders(bool $parseHeaders) { $this->helper->setParseHeaders($parseHeaders); }
  function pp_setHeaders(?array $headers) { $this->helper->setHeaders($headers); }
  function pp_setHeaderMappings($headerMappings) { $this->helper->setHeaderMappings($headerMappings); }
  function pp_setMapEmpty($mapEmpty) { $this->helper->setMapEmpty($mapEmpty); }

  protected function afterSetParametrableParams(array $modifiedKeys, ?Metadata $md=null): void {
    if (!in_array("input_encoding", $modifiedKeys)
      && in_array("flavour", $modifiedKeys)) {
      $this->ppInputEncoding = flavours::get_encoding($this->ppFlavour);
      $modifiedKeys[] = "input_encoding";
    }
    $this->encodingInput__afterSetParametrableParams($modifiedKeys);
  }

  /** @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;
  }

  function getIterator() {
    $helper = $this->helper;
    $reader = $this->ensureOpen();
    try {
      [$separator, $enclosure, $escape] = flavours::get_params($this->ppFlavour);
      $resource = $reader->getResource();
      if ($resource !== null) {
        while (!$helper->_parseLine()) {
          if (fgets($resource) === false) break;
        }
        while (($values = fgetcsv($resource, 0, $separator, $enclosure, $escape)) !== false) {
          if (!$helper->checkHeaders($values)) {
            yield $helper->mapRow($values);
          }
        }
      } else {
        while (true) {
          try {
            $line = $reader->readLine();
          } catch (EOFException $e) {
            break;
          }
          if (!$helper->_parseLine()) continue;
          $values = str_getcsv($line, $separator, $enclosure, $escape);
          if (!$helper->checkHeaders($values)) {
            yield $helper->mapRow($values);
          }
        }
      }
    } 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*/[
    'getInputEncoding' => 'input_encoding',
    'getOutputEncoding' => 'output_encoding',
  ];
  const _AUTO_SETTERS = /*autogen*/[
    'setInput' => 'input',
    'setInputEncoding' => 'input_encoding',
    'setOutputEncoding' => 'output_encoding',
    'setFlavour' => 'flavour',
    'setMultiSchema' => 'multi_schema',
    'setSkipLines' => 'skip_lines',
    'setParseHeaders' => 'parse_headers',
    'setHeaders' => 'headers',
    'setHeaderMappings' => 'header_mappings',
    'setMapEmpty' => 'map_empty',
  ];
  #--autogen-dynamic--
}
