<?php
namespace nur\mapper\csv;

use nur\A;
use nur\b\io\Tfilter;
use nur\b\params\Tparametrable;
use nur\data\types\Metadata;
use nur\mapper\base\Consumer;
use nur\mapper\base\encoding_utils;
use nur\mapper\base\Tencoding;
use nur\mapper\base\Ttmpwriter;

/**
 * Class CsvWriter: écrire un flux de données au format CSV dans un fichier
 * destination
 *
 * --autogen-properties-and-methods--
 * @method string|null getInputEncoding()
 * @method string|null getOutputEncoding()
 * @method setOutput($value)
 * @method string|null setInputEncoding(?string $value)
 * @method string|null setOutputEncoding(?string $value)
 * @method setCsvFlavour($value)
 * @method bool setMultiSchema(bool $value)
 * @method array setHeaders(array $value)
 * @method bool setOutputHeaders(bool $value)
 */
class CsvWriter extends Consumer {
  use Tparametrable, Ttmpwriter, Tencoding, Tfilter;

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

  function __construct($output=null) {
    $this->assoc2csv = new Assoc2CsvMapper();
    $this->assoc2csv->setCsvFlavour([static::SEPARATOR, static::ENCLOSURE, static::ESCAPE]);
    parent::__construct();
    $this->pp_setOutput($output);
  }

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

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "output" => [null, null, "fichier en sortie"],
  ];

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

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

  /** @var Assoc2CsvMapper */
  protected $assoc2csv;
  private $csvFlavourName;
  function pp_setCsvFlavour($csvFlavour): void {
    if (is_string($csvFlavour)) $this->csvFlavourName = $csvFlavour;
    $this->assoc2csv->setCsvFlavour($csvFlavour);
  }
  function pp_setMultiSchema(bool $multiSchema=true): void {
    $this->assoc2csv->setMultiSchema($multiSchema);
  }
  function pp_setHeaders(array $headers): void {
    $this->assoc2csv->setHeaders($headers);
  }
  function pp_setOutputHeaders(bool $outputHeaders=true): void {
    $this->assoc2csv->setOutputHeaders($outputHeaders);
  }
  
  protected function setup(): void {
    parent::setup();
    $this->setupWriter(true);
    $this->assoc2csv->ensureSetup();
  }

  protected function teardown(): void {
    $this->assoc2csv->close();
    $this->teardownWriter(true);
  }

  function _consume(iterable $items): void {
    $mapper = $this->assoc2csv;
    [$separator, $enclosure, $escape] = $mapper->getCsvFlavour();
    $writer = $this->getWriter();
    $resource = $writer->getResource();
    if ($resource !== null) {
      [$skipLine, $outputHeaders, $headers] = $mapper->_checkHeaders(null);
      if ($skipLine) fwrite($resource, "\n");
      if ($outputHeaders) {
        $cols = $mapper->_cookHeaders($headers);
        fputcsv($resource, $cols, $separator, $enclosure, $escape);
        $mapper->_setOutputHeaders($headers);
      }
      foreach ($items as $value) {
        $row = A::with($value);
        [$skipLine, $outputHeaders, $headers] = $mapper->_checkHeaders($row);
        if ($skipLine) fwrite($resource, "\n");
        if ($outputHeaders) {
          $cols = $mapper->_cookHeaders($headers);
          fputcsv($resource, $cols, $separator, $enclosure, $escape);
          $mapper->_setOutputHeaders($headers);
        }
        $cols = $mapper->_cookValues($headers, $row);
        fputcsv($resource, $cols, $separator, $enclosure, $escape);
      }
      fflush($resource);
    } else {
      [$skipLine, $outputHeaders, $headers] = $mapper->_checkHeaders(null);
      if ($skipLine) $writer->wnl();
      if ($outputHeaders) {
        $cols = $mapper->_cookHeaders($headers);
        $writer->write($mapper->getLine($cols, false));
        $mapper->_setOutputHeaders($headers);
      }
      foreach ($items as $value) {
        $row = A::with($value);
        [$skipLine, $outputHeaders, $headers] = $mapper->_checkHeaders($row);
        if ($skipLine) $writer->wnl();
        if ($outputHeaders) {
          $cols = $mapper->_cookHeaders($headers);
          $writer->write($mapper->getLine($cols, false));
          $mapper->_setOutputHeaders($headers);
        }
        $cols = $mapper->_cookValues($headers, $row);
        $writer->write($mapper->getLine($cols, false));
      }
    }
  }

  #############################################################################
  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*/[
    'getOutput' => 'output',
    'getInputEncoding' => 'input_encoding',
    'getOutputEncoding' => 'output_encoding',
    'getCsvFlavour' => 'csv_flavour',
    'isMultiSchema' => 'multi_schema',
    'getHeaders' => 'headers',
    'isOutputHeaders' => 'output_headers',
  ];
  const _AUTO_SETTERS = /*autogen*/[
    'setOutput' => 'output',
    'setInputEncoding' => 'input_encoding',
    'setOutputEncoding' => 'output_encoding',
    'setCsvFlavour' => 'csv_flavour',
    'setMultiSchema' => 'multi_schema',
    'setHeaders' => 'headers',
    'setOutputHeaders' => 'output_headers',
  ];
  #--autogen-dynamic--
}
