<?php

namespace App\Helper;

use App\Entity\Warehouse;
use App\Repository\ProductRepository;
use App\Repository\StockRepository;
use App\Repository\WarehouseRepository;
use ArrayIterator;
use Doctrine\Common\Collections\Criteria;
use Exception;
use League\Csv\Writer;
use Psr\Log\LoggerInterface;


class Jtl
{
    private $productRepository;
    private $stockRepository;
    private $warehouseRepository;
    private $logger;
    private $rootPath;

    /**
     * @var string[] $arrLager
     * Lagernamen aus der JTL Wawi
     */
    private array $arrLager = array(
        1 => 'Lager 1 - Standardlager',
        3 => 'Lager 3 - DLX',
        5 => 'Lager 5 - Verkaufsfilale',
        8 => 'Lager 8 - Standardlager',
        10 => 'Lager 10 - OJ EF',
    );

    /**
     * @param ProductRepository $productRepository
     * @param WarehouseRepository $warehouseRepository
     * @param StockRepository $stockRepository
     * @param LoggerInterface $logger
     * @param string $rootPath
     */
    public function __construct(
        ProductRepository   $productRepository,
        WarehouseRepository $warehouseRepository,
        StockRepository     $stockRepository,
        LoggerInterface     $logger,
        string              $rootPath)
    {
        $this->productRepository = $productRepository;
        $this->warehouseRepository = $warehouseRepository;
        $this->stockRepository = $stockRepository;
        $this->logger = $logger;
        $this->rootPath = $rootPath;
    }

    /**
     * Holt alle Produkte und deren Lagerbestände
     * @param array $warehousesName
     * @return array
     */
    public function getProducts(array $prodIds, array $warehousesName): array
    {
        $data = array();
        if ($prodIds == array(0)) {
            $products = $this->productRepository->findAll();
        } else {
            $products = $this->productRepository->findById($prodIds);
        }


        $warehouse = $this->warehouseRepository->findByWarehouseByName($warehousesName);

        if ($warehouse) {
            foreach ($warehouse as $w) {

                $warehousesName = $w->getName();

                foreach ($products as $p) {
                    $stock = $this->stockRepository->findOneBy(['product_id' => $p->getId(), 'warehouse' => $w]);

                    if ($stock) {
                        $data[$p->getId() . $warehousesName] = [
                            'gtin' => $p->getGtin(),
                            'stock' => $stock->getInstock(),
                            'warehouse' => $this->arrLager[$warehousesName] ?? 'Lager_' . $warehousesName,
                            'lager' => '01'
                        ];
                    } else {
                        //setze Produkt auf 0 wenn kein Bestand vorhanden
                        $data[$p->getId() . $warehousesName] = [
                            'gtin' => $p->getGtin(),
                            'stock' => 0,
                            'warehouse' => $this->arrLager[$warehousesName] ?? 'Lager_' . $warehousesName,
                            'lager' => '01'
                        ];
                    }
                }

            }
        }
        return $data;
    }


    /**
     * @param array $data
     * @param string $warehouse
     * @return void
     */
    public function createExportFile(array $data, string $warehouse): void
    {
        try {

            $file = $this->rootPath . '/jtl/' . $warehouse . '.csv';

            $writer = Writer::createFromPath($file, 'w+');
            $bytes = $writer->insertAll(new ArrayIterator($data));

            if ($bytes) {
                $this->logger->info('Exported ' . $bytes . ' bytes');

                $FTP = new Ftp();
                $FTP->uploadFile($file, $warehouse);
            }

        } catch (Exception $e) {
            $this->logger->error($e->getMessage());
        }
    }
}