<?php
namespace nur\mapper\csv;

use nur\b\io\EOFException;
use nur\b\io\IReader;
use nur\b\io\Tfilter;
use nur\b\params\Tparametrable;
use nur\data\types\Metadata;
use nur\mapper\base\encoding_utils;
use nur\mapper\base\Producer;
use nur\mapper\base\Tencoding;
use nur\reader;

/**
 * Class CsvReader: produit un flux de données à partir d'une source au format
 * CSV.
 *
 * NB: cette classe supporte les fichiers CSV multi-lignes, contrairement à
 * {@link Csv2AssocMapper}
 *
 * --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 setCsvFlavour($value)
 * @method bool setMultiSchema(bool $value)
 * @method bool|null setParseHeaders(?bool $value)
 * @method array|null setHeaders(?array $value)
 * @method int setSkipLines(int $value)
 * @method string|null setMapEmpty(?string $value)
 */
class CsvReader extends Producer {
  use Tparametrable, Tencoding, Tfilter;

  const SEPARATOR = csv_defaults::OO_SEPARATOR;
  const ENCLOSURE = csv_defaults::OO_ENCLOSURE;
  const ESCAPE = csv_defaults::OO_ESCAPE;

  function __construct($input=null) {
    $this->csv2assoc = new Csv2AssocMapper();
    $this->csv2assoc->setCsvFlavour([static::SEPARATOR, static::ENCLOSURE, static::ESCAPE]);
    $params = null;
    if ($input !== null) $params["input"] = $input;
    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"],
  ];

  static function _get_parametrable_params_schema(): array {
    return array_merge(self::PARAMETRABLE_PARAMS_SCHEMA
      , encoding_utils::PARAMETRABLE_PARAMS_SCHEMA
      , AbstractCsvMapper::PARAMETRABLE_PARAMS_SCHEMA
      , Csv2AssocMapper::PARAMETRABLE_PARAMS_SCHEMA);
  }

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

  protected $ppInput;

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

  /** @var Csv2AssocMapper */
  protected $csv2assoc;
  /** @var string */
  private $csvFlavourName;
  function pp_setCsvFlavour($csvFlavour): void {
    if (is_string($csvFlavour)) $this->csvFlavourName = $csvFlavour;
    $this->csv2assoc->setCsvFlavour($csvFlavour);
  }
  function pp_setMultiSchema(bool $multiSchema): void {
    $this->csv2assoc->setMultiSchema($multiSchema);
  }
  function pp_setParseHeaders(?bool $parseHeaders): void {
    $this->csv2assoc->setParseHeaders($parseHeaders);
  }
  function pp_setHeaders(?array $headers): void {
    $this->csv2assoc->setHeaders($headers);
  }
  function pp_setSkipLines(int $skipLines): void {
    $this->csv2assoc->setSkipLines($skipLines);
  }
  function pp_setMapEmpty($mapEmpty=null): void {
    $this->csv2assoc->setMapEmpty($mapEmpty);
  }

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

  protected function setup(): void {
    if ($this->reader === null) {
      $this->reader = reader::with($this->ppInput);
      $this->_rwAppendFilters($this->reader);
    }
    $this->csv2assoc->ensureSetup();
  }

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

  function producer() {
    $parser = $this->csv2assoc;
    [$separator, $enclosure, $escape] = $parser->getCsvFlavour();
    $reader = $this->reader;
    $resource = $reader->getResource();
    if ($resource !== null) {
      while (!$parser->_parseLine()) {
        if (fgets($resource) === false) break;
      }
      while (($values = fgetcsv($resource, 0, $separator, $enclosure, $escape)) !== false) {
        if (!$parser->_checkHeader($values)) {
          yield $parser->_mapRow($values);
        }
      }
    } else {
      while (true) {
        try {
          $line = $reader->readLine();
        } catch (EOFException $e) {
          break;
        }
        if (!$parser->_parseLine()) continue;
        $values = str_getcsv($line, $separator, $enclosure, $escape);
        if (!$parser->_checkHeader($values)) {
          yield $parser->_mapRow($values);
        }
      }
    }
  }

  #############################################################################
  const _AUTOGEN_CONSTS = [
    "" => [self::class, "_autogen_consts", true],
  ];
  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*/[
    'getInput' => 'input',
    'getInputEncoding' => 'input_encoding',
    'getOutputEncoding' => 'output_encoding',
    'getCsvFlavour' => 'csv_flavour',
    'isMultiSchema' => 'multi_schema',
    'getParseHeaders' => 'parse_headers',
    'getHeaders' => 'headers',
    'getSkipLines' => 'skip_lines',
    'getMapEmpty' => 'map_empty',
  ];
  const _AUTO_SETTERS = /*autogen*/[
    'setInput' => 'input',
    'setInputEncoding' => 'input_encoding',
    'setOutputEncoding' => 'output_encoding',
    'setCsvFlavour' => 'csv_flavour',
    'setMultiSchema' => 'multi_schema',
    'setParseHeaders' => 'parse_headers',
    'setHeaders' => 'headers',
    'setSkipLines' => 'skip_lines',
    'setMapEmpty' => 'map_empty',
  ];
  #--autogen-dynamic--
}
