<?php

namespace App\Helper;


use App\Entity\Product;
use App\Entity\Stock;
use App\Entity\Warehouse;
use App\Repository\ProductRepository;
use App\Repository\StockRepository;
use App\Repository\WarehouseRepository;
use Exception;
use Psr\Log\LoggerInterface;
use RuntimeException;
use SplFileObject;
use Symfony\Component\Finder\Finder;

/**
 * Class HiltesImport
 *
 * This class is responsible for importing data from files into the application.
 * It uses Symfony's Finder component to locate the files and then processes them line by line.
 * The data is then saved into the database using the repositories for the Product, Stock and Warehouse entities.
 */
class HiltesImport
{
    protected $currentDirPath;
    protected $cachedWarehouse;
    protected $arrData = array();
    private $productRepository;
    private $stockRepository;
    private $warehouseRepository;
    private $logger;
    private $cachedProdIds;
    private $cachedStockIds;
    private $rootPath;

    private $deleteFiles = false;

    /**
     * HiltesImport constructor.
     *
     * @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;

        $this->currentDirPath = $this->rootPath;
    }


    /**
     * Starts the import process.
     *
     * @param bool $delta
     * @return array
     * @throws RuntimeException
     */
    public function startImport(bool $delta = false): array
    {
        #*** Holt alle Dateien
        if ($this->getFiles($delta)) {
            #*** Holt alle Stocks und setzt ein Array **************
            $this->getStocks();

            if (!empty($this->arrData['orgFiles']['data']) && count($this->arrData['orgFiles']['data'])) {
                foreach ($this->arrData['orgFiles']['data'] as $file) {

                    if (is_file($file['realPath'])) {
                        $this->loadFiles($file['realPath']);
                    } else {
                        throw new RuntimeException("Error: File not found - " . $file['realPath']);
                    }
                }

                return $this->cachedProdIds;
            } else {
                $this->logger->info('No Files');
            }
        } else {
            throw new RuntimeException('No Files to Import');
        }
        return [];
    }

    /**
     * Retrieves the files to be imported.
     *
     * @param bool $delta
     * @return bool
     */
    protected function getFiles(bool $delta = false): bool
    {
        $finder = Finder::create()->in($this->currentDirPath . '/hiltes/h2c/')->depth(0);
        $delta ? $finder->name('/_D/') : $finder->notName('/_D/');

        if ($finder->hasResults()) {
            foreach ($finder as $file) {
                if ($file->getExtension() != 'Ende') {
                    //prüfen ob Ende Datei vorhanden ist
                    if (file_exists($this->currentDirPath . '/hiltes/h2c/' . $file->getRelativePathname() . '.Ende')) {
                        $this->arrData['orgFiles']['data'][] = array(
                            'realPath' => $file->getRealPath(),
                            'fileSize' => $file->getFileInfo()->getSize(),
                            'onlyFileName' => $file->getRelativePathname(),
                        );
                    }
                }
            }
        } else {
            return false;
        }

        return true;
    }

    protected function getStocks(): void
    {
        $stocks = $this->stockRepository->findAll();

        foreach ($stocks as $stock) {
            $this->cachedStockIds[$stock->getProductId()][$stock->getWarehouse()->getId()] = $stock;
        }
    }

    /**
     * Loads the files and processes them line by line.
     *
     * @param $srcFile
     * @return void
     * @throws Exception
     */
    protected function loadFiles($srcFile): void
    {
        try {
            $file = new SplFileObject($srcFile);

            $this->logger->info('Starte Import von ' . $file->getRealPath());
            $count = 0;

            while (!$file->eof()) {
                $this->processLine($file->fgets(), "product");
                $count++;
            }

            $this->cachedProdIds = $this->productRepository->saveAll();

            //Setzte den Zeiger wieder auf den Anfang
            $file->seek(0);

            while (!$file->eof()) {
                $this->processLine($file->fgets(), "stock");
                $count++;
            }

            //Save Stocks
            $this->stockRepository->saveAll();

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

        if ($this->deleteFiles) {
            unlink($srcFile);
            unlink($srcFile . '.Ende');
        }

        $this->logger->info($count . ' Datensätze importiert');
    }

    protected function processLine($line, $type = "stock"): void
    {
        $data = str_getcsv($line, ';', '"');

        // $this->logger->info($data[0] . ' ' . $data[1] . ' ' . $data[3]);
        if (empty($data[0])) {
            //$this->logger->warning('Keine Daten in Zeile' . $line);
            return;
        }

        if ($type == "stock") {
            $this->saveData($data);
        } else {
            $this->processProduct($data);
        }
    }

    /**
     * Processes product data.
     *
     * @param $prodData
     */
    private function processProduct(array $prodData): void
    {

        $gtin = substr($prodData[0], 1);

        $product = $this->productRepository->findOneBy(['gtin' => $gtin]);
        if (empty($product)) {
            $product = new Product();
            $product->setGtin($gtin);
        }
        $this->productRepository->add($product);

    }

    /**
     * Trims all elements of an array.
     *
     * @param array $arr
     * @return void
     */
    private function trimArray(array &$arr): void
    {
        foreach ($arr as $k => $v) {
            $arr[$k] = trim($v);
        }
    }

    /**
     * Saves the data into the database.
     *
     * @param array $prodData
     * @return void
     */
    protected function saveData(array $prodData): void
    {

        $warehouseNumber = trim($prodData[3]);
        if (empty($prodData[1])) {
            $this->logger->info('Kein Bestand für ' . $prodData[0]);
            return;
        }
        $inStock = (int)$prodData[1] / 100;
        $gtin = substr($prodData[0], 1);


        //Prüfe Lager
        $warehouse = $this->checkWarehouseName($warehouseNumber);

        //Hole Produkt Id bzw lege Produkt an
        $product_id = $this->checkProduct($gtin);

        if (!empty($warehouse) && !empty($this->cachedStockIds[$product_id][$warehouse->getId()])) {

            $stock = $this->cachedStockIds[$product_id][$warehouse->getId()];

        } else {
            $stock = new Stock();
            $stock->setProductId($product_id);
            $stock->setWarehouse($warehouse);
        }

        $stock->setInstock($inStock);
        $this->stockRepository->add($stock);

    }

    /**
     * Checks the warehouse name and returns the corresponding warehouse.
     *
     * @param string $warehouseName
     * @return int
     */
    private function checkWarehouseName(string $warehouseName)
    {
        $warehouseName = ltrim($warehouseName, 0);

        if (empty($this->cachedWarehouse[$warehouseName])) {
            $warehouse = $this->warehouseRepository->findOneBy(['name' => $warehouseName]);

            //Wenn kein Lager gefunden wurde, dann lege es an
            if (empty($warehouse)) {
                $warehouse = new Warehouse();
                $warehouse->setName($warehouseName);
                $warehouseId = $this->warehouseRepository->save($warehouse, true);
                $newWarehouse = $this->warehouseRepository->findOneBy(['id' => $warehouseId]);

                $this->cachedWarehouse[$warehouseName] = $newWarehouse;
            } else {
                $this->cachedWarehouse[$warehouseName] = $warehouse;
            }
        }
        return $this->cachedWarehouse[$warehouseName];
    }

    /**
     * Checks the product and returns the corresponding product id.
     *
     * @param string $gtin
     * @return false|int|mixed|null
     */
    private function checkProduct(string $gtin)
    {

        #*** WEnn keine geCached Id Vorhanden
        if (empty($this->cachedProdIds[$gtin])) {
            $product = $this->productRepository->findOneBy(['gtin' => $gtin]);

            if (empty($product)) {
                $product = new Product();
                $product->setGtin($gtin);
                //$this->productRepository->add($product);
                $this->cachedProdIds[$gtin] = $this->productRepository->save($product, true);
            } else {
                $this->cachedProdIds[$gtin] = $product->getId();
            }
        }

        if (!empty($this->cachedProdIds[$gtin])) {
            return $this->cachedProdIds[$gtin];
        }

        return false;
    }
}