<?php
namespace nur\io\csv;

use nur\A;
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\io\Ttmpwriter;
use nur\ref\ref_csv;

/**
 * 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 string|null setFlavour(?string $value)
 * @method bool setMultiSchema(bool $value)
 * @method array|null setHeaders(?array $value)
 * @method bool setOutputHeaders(bool $value)
 * @method setHeaderMappings($value)
 */
class CsvWriter extends Parametrable {
  use Tparametrable, Ttmpwriter, Tencoding, Tfilter;

  const FLAVOUR = ref_csv::OO_FLAVOUR;

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

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

  const PARAMETRABLE_PARAMS_SCHEMA = [
    "output" => [null, null, "fichier en sortie"],
    "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?"],
    "headers" => ["?array", null, "liste et ordre des en-têtes en entrée"],
    "output_headers" => ["bool", true, "faut-il afficher les en-têtes en sortie?"],
    "header_mappings" => [null /*string|array*/, null, "mappings des en-têtes",
      "desc" => "le mapping peut-être fourni comme un tableau [include, exclude => null, dest => source] ou une chaine 'include,=exclude,dest=source'"
    ],
  ];

  protected $ppFlavour;

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

  /** @var Assoc2CsvHelper */
  protected $helper;
  function pp_setMultiSchema(bool $multiSchema) { $this->helper->setMultiSchema($multiSchema); }
  function pp_setHeaders(array $headers) { $this->helper->setHeaders($headers); }
  function pp_setOutputHeaders(bool $outputHeaders) { $this->helper->setOutputHeaders($outputHeaders); }
  function pp_setHeaderMappings($headerMappings) { $this->helper->setHeaderMappings($headerMappings); }

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

  private $inSession;

  function write(array $row, bool $flush=false): self {
    $flavour = $this->ppFlavour;
    $helper = $this->helper;
    $writer = $this->ensureWriter(true);
    $resource = $writer->getResource();
    if ($resource !== null) {
      [$separator, $enclosure, $escape] = flavours::get_params($flavour);
      if ($this->inSession == null) {
        $this->inSession = true;
        [$skipLine, $outputHeaders, $headers] = $helper->checkHeaders(null);
        if ($skipLine) fwrite($resource, "\n");
        if ($outputHeaders) {
          $cols = $helper->cookHeaders($headers);
          fputcsv($resource, $cols, $separator, $enclosure, $escape);
          $helper->resetOutputHeaders($headers);
        }
      }
      [$skipLine, $outputHeaders, $headers] = $helper->checkHeaders($row);
      if ($skipLine) fwrite($resource, "\n");
      if ($outputHeaders) {
        $cols = $helper->cookHeaders($headers);
        fputcsv($resource, $cols, $separator, $enclosure, $escape);
        $helper->resetOutputHeaders($headers);
      }
      $cols = $helper->cookValues($headers, $row);
      fputcsv($resource, $cols, $separator, $enclosure, $escape);
      if ($flush) fflush($resource);
    } else {
      if ($this->inSession == null) {
        $this->inSession = true;
        [$skipLine, $outputHeaders, $headers] = $helper->checkHeaders(null);
        if ($skipLine) $writer->wnl();
        if ($outputHeaders) {
          $cols = $helper->cookHeaders($headers);
          $writer->write($helper->getLine($cols, $flavour, false));
          $helper->resetOutputHeaders($headers);
        }
      }
      [$skipLine, $outputHeaders, $headers] = $helper->checkHeaders($row);
      if ($skipLine) $writer->wnl();
      if ($outputHeaders) {
        $cols = $helper->cookHeaders($headers);
        $writer->write($helper->getLine($cols, $flavour, false));
        $helper->resetOutputHeaders($headers);
      }
      $cols = $helper->cookValues($headers, $row);
      $writer->write($helper->getLine($cols, $flavour, false));
    }
    return $this;
  }

  function writeAll(iterable $items): self {
    foreach ($items as $value) {
      $this->write(A::with($value));
    }
    $resource = $this->ensureWriter(true)->getResource();
    if ($resource !== null) fflush($resource);
    return $this;
  }

  function close(): void {
    $this->teardownWriter(true);
    $this->inSession = 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*/[
    'setOutput' => 'output',
    'setInputEncoding' => 'input_encoding',
    'setOutputEncoding' => 'output_encoding',
    'setFlavour' => 'flavour',
    'setMultiSchema' => 'multi_schema',
    'setHeaders' => 'headers',
    'setOutputHeaders' => 'output_headers',
    'setHeaderMappings' => 'header_mappings',
  ];
  #--autogen-dynamic--
}
