<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Writer\CSV;

use DropshippingExportFreeVendor\League\Csv\Writer;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Writer\Generator;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Writer\ColumnDelimeter;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Collection\ProductFieldCollection;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldCsv;
class CustomCsvWriter implements Generator, ColumnDelimeter
{
    /**
     * @var ProductFieldCollection[]
     */
    private $batch = [];
    const COLUMN_NAME = 'name';
    const COLUMN_VALUE = 'value';
    const DEFAULT_DELIMETER = ';';
    const SUPPORTED_EXTENSION = 'CSV';
    private $delimeter = self::DEFAULT_DELIMETER;
    public function set_delimeter(string $delimeter): void
    {
        $this->delimeter = $delimeter;
    }
    public function add_to_batch(ProductFieldCollection $product_fields): void
    {
        $this->batch[] = $product_fields;
    }
    private function get_data(ProductFieldCollection $product_fields, bool $create_new_file): array
    {
        $data = [];
        foreach ($product_fields as $field) {
            if ($field instanceof ProductFieldCsv) {
                $data = $field->map_csv_value($data);
            }
        }
        $new_data = [];
        $header = [];
        $raw_data = [];
        foreach ($data as $key => $val) {
            if ($create_new_file) {
                $header[] = $key;
            }
            $raw_data[] = $val;
        }
        if (!empty($header)) {
            $new_data[] = $header;
        }
        $new_data[] = $raw_data;
        return $new_data;
    }
    private function get_delimeter(): string
    {
        return $this->delimeter;
    }
    public function flush_batch(string $file_path, bool $create_new_file): void
    {
        if (empty($this->batch)) {
            if ($create_new_file) {
                file_put_contents($file_path, Writer::BOM_UTF8);
            }
            return;
        }
        foreach ($this->batch as $product_fields) {
            $data = $this->get_data($product_fields, $create_new_file);
            $mode = $create_new_file ? 'w' : 'a';
            $csv = Writer::createFromPath($file_path, $mode);
            $csv->setDelimiter($this->get_delimeter());
            $csv->setNewline("\n");
            $csv->setOutputBOM(Writer::BOM_UTF8);
            $csv->insertAll($data);
            $create_new_file = \false;
        }
        $this->clear_batch();
    }
    public static function get_supported_extension(): string
    {
        return self::SUPPORTED_EXTENSION;
    }
    public function has_batch(): bool
    {
        return !empty($this->batch);
    }
    public function get_batch_count(): int
    {
        return count($this->batch);
    }
    public function clear_batch(): void
    {
        $this->batch = [];
    }
}
