<?php

namespace App\Command;
use App\Entity\DinAsset;
use App\Entity\DinAssetBenchmark;
use Doctrine\ORM\EntityManagerInterface;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\KernelInterface;


/**
 * Command to import excel that with answers of specifications
 *
 * Class CmdImportAll
 * @package App\Command
 */
class CmdImportBenchmark extends Command
{
    protected static $defaultName = 'app:import-benchmark';

    private $em;
    private $container;
    private $kernel;

    public function __construct(EntityManagerInterface $em, ContainerInterface $container, KernelInterface $kernel)
    {
        $this->em = $em;
        $this->container = $container;
        $this->kernel = $kernel;
        parent::__construct();
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int|null|void
     * @throws \Exception
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        ini_set('memory_limit', '4096M');
        $projectRoot = $this->kernel->getProjectDir();
        $excelFile = $projectRoot. '/import/mpp_import_benchmark.xlsx';
        $reader = IOFactory::createReaderForFile($excelFile);
        $reader->setReadDataOnly(true);
        $spreadsheet = $reader->load($excelFile);

        $worksheet = $spreadsheet->getSheetByName('Preisbenchmarking Daten');

        if (null === $worksheet) {
            $output->write('No benchmark worksheet found.');
            return Command::FAILURE;
        }

        $this->em->getConnection()->beginTransaction();

        try {
            $this->em->createQuery('DELETE FROM App\Entity\DinAssetBenchmark')->execute();

            $dinAssetsByDinAndName = [];
            $dinAssets = $this->em->getRepository(DinAsset::class)->findAll();
            /** @var DinAsset $dinAsset */
            foreach ($dinAssets as $dinAsset) {
                $dinAssetsByDinAndName[$dinAsset->getDinNumber().$dinAsset->getName()] = $dinAsset;
            }

            $highestRow = $worksheet->getHighestRow();
            $highestColumn = $worksheet->getHighestColumn();

            $startRow = 2;
            $startCol = 'A';

            for ($row = $startRow; $row <= $highestRow; $row++) {
                $rowData = [];
                for ($col = $startCol; $col <= $highestColumn; $col++) {
                    $cell = $worksheet->getCell($col . $row);
                    $rowData[] = $cell->getValue();
                }
                $dinNumber = $rowData[0];
                $name = $rowData[5];
                $key = $dinNumber.$name;

                if (!array_key_exists($key, $dinAssetsByDinAndName)) {
                    $output->writeln("No din asset found: ". $key);
                } else {
                    /** @var DinAsset $dinAsset */
                    $dinAsset = $dinAssetsByDinAndName[$key];

                    $dinBenchmark = new DinAssetBenchmark();
                    $dinBenchmark->setDinAsset($dinAsset);
                    $dinBenchmark->setComponent($rowData[7]);
                    $dinBenchmark->setType($rowData[9]);
                    $dinBenchmark->setBandwidth($rowData[11]);
                    $dinBenchmark->setDeviation($rowData[16]);
                    $dinBenchmark->setAverageCosts($rowData[17]);
                    $dinBenchmark->setMinAverageCosts($rowData[18]);
                    $dinBenchmark->setMaxAverageCosts($rowData[19]);


                    $this->em->persist($dinBenchmark);
                }
            }
            $this->em->flush();
            $this->em->getConnection()->commit();
        } catch (\Exception $e) {
            $this->em->getConnection()->rollBack();
        }

        return Command::SUCCESS;
    }

}