<?php

namespace common\logic\Distribution\Distribution\Service;

use common\logic\AbstractManager;
use common\logic\Distribution\Distribution\Export\DistributionReport2CsvGenerator;
use common\logic\Distribution\Distribution\Export\DistributionReportCsvGenerator;
use common\logic\Distribution\Distribution\Export\DistributionReportGridPdfGenerator;
use common\logic\Distribution\Distribution\Export\DistributionReportPdfGenerator;
use common\logic\Distribution\Distribution\Export\DistributionReportTotalProductCsvGenerator;
use common\logic\Distribution\Distribution\Export\DistributionShoppingCartLabelsPdfGenerator;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Producer\Producer\Model\Producer;
use yii\base\ErrorException;
use yii\widgets\ActiveForm;

class ExportManager extends AbstractManager
{
    const ORDERS_PDF = 'orders_pdf';
    const ORDERS_GRID_PDF = 'orders_grid_pdf';
    const ORDERS1_CSV = 'orders1_csv';
    const ORDERS2_CSV = 'orders2_csv';
    const TOTAL_PRODUCTS_CSV = 'total_products_csv';
    const SHOPPING_CART_LABELS_PDF = 'shopping_cart_labels_pdf';

    public function getAll(): array
    {
        return [
            self::ORDERS_PDF => [
                'Commandes (PDF)',
                DistributionReportPdfGenerator::class,
            ],
            self::ORDERS_GRID_PDF => [
                'Commandes grille (PDF)',
                DistributionReportGridPdfGenerator::class,
            ],
            self::ORDERS1_CSV => [
                'Commandes #1 (CSV)',
                DistributionReportCsvGenerator::class,
            ],
            self::ORDERS2_CSV => [
                'Commandes #2 (CSV)',
                DistributionReport2CsvGenerator::class,
            ],
            self::TOTAL_PRODUCTS_CSV => [
                'Totaux produits (CSV)',
                DistributionReportTotalProductCsvGenerator::class,
            ],
            self::SHOPPING_CART_LABELS_PDF => [
                'Étiquettes (PDF)',
                DistributionShoppingCartLabelsPdfGenerator::class,
            ]
        ];
    }

    public function getAllEnabled(): array
    {
        $exportsArray = $this->getAll();
        $exportsEnabledArray = [];

        foreach($exportsArray as $name => $export) {
            if($this->isEnabled($name)) {
                $exportsEnabledArray[$name] = $export;
            }
        }

        return $exportsEnabledArray;
    }

    public function getExport(string $exportName): array
    {
        $exportsArray = $this->getAll();
        return $exportsArray[$exportName];
    }

    public function getExportLabel(string $exportName): string
    {
        $export = $this->getExport($exportName);
        return $export[0];
    }

    public function generate(string $exportName, Distribution $distribution, bool $save = false)
    {
        return $this->getGenerator($exportName)->generate($distribution, $save);
    }

    public function getGenerator(string $exportName)
    {
        if($this->exist($exportName)) {
            $export = $this->getExport($exportName);
            $classGenerator = $export[1];
            return $classGenerator::getInstance();
        }
        else {
            throw new ErrorException("Cet export n'existe pas.");
        }
    }

    public function exist(string $exportName): bool
    {
        return array_key_exists($exportName, $this->getAll());
    }

    public function isEnabled(string $exportName): bool
    {
        return $this->exist($exportName) && $this->producerHasOptionExportEnabled($exportName);
    }

    public function getAjaxArray(Distribution $distribution): array
    {
        $datas = [];
        $exportsArray = $this->getAll();

        foreach($exportsArray as $exportName => $export) {
            if($this->isEnabled($exportName)) {
                $datas[] = $this->buildAjaxArray($distribution, $exportName, $export);
            }
        }

        return $datas;
    }

    public function buildAjaxArray(Distribution $distribution, string $exportName, array $export): array
    {
        return [
            'name' => $exportName,
            'label' => $export[0],
            'url' => \Yii::$app->urlManagerBackend->createUrl([
                'distribution/export',
                'date' => $distribution->date,
                'name' => $exportName
            ])
        ];
    }

    private function producerHasOptionExportEnabled(string $exportName): ?bool
    {
        $producer = $this->getProducerContext();
        $fieldOptionExport = $this->getProducerFieldOptionExport($exportName);
        return $producer->$fieldOptionExport;
    }

    private function getProducerFieldOptionExport(string $exportName): string
    {
        return 'option_distribution_export_'.$exportName;
    }

    public function getProducerFormCheckboxes(ActiveForm $form, Producer $producer): string
    {
        $html = '';
        $exportsArray = $this->getAll();

        foreach($exportsArray as $exportName => $export) {
            $html .= $this->getProducerFormCheckbox($form, $producer, $exportName);
        }

        return $html;
    }

    public function getProducerFormCheckbox(ActiveForm $form, Producer $producer, string $exportName): string
    {
        return $form->field($producer, $this->getProducerFieldOptionExport($exportName))
            ->checkbox(['label' => $this->getExportLabel($exportName)]);
    }
}