<?php

namespace Modules\Commande\Service;

use Entity\Model\Hastutil;
use Entity\Model\Paravrac;
use Helpers\Outil\Date;
use Helpers\Outil\Outils;
use Helpers\Phpspreadsheet\SpreadsheetHelper;
use Helpers\Trad\ITrad;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class ImportLigneService
{
    private \PDO $pdo;
    private Itrad $trad;

    private Worksheet $sheet;
    
    public function __construct(\PDO $pdo, ITrad $trad)
    {
        $this->pdo = $pdo;
        $this->trad = $trad;
    }

    /**
     * @return array<mixed>
     */
    public function getArchitecture(string $codeSte)
    {

        $cgParId = \Helpers\Outil\Roles::droit($this->pdo, 'CODE_GESTION_PAR_ID')? 1:0;

        $paravrac = (new Paravrac($this->pdo));
        $configAxeImp = $paravrac->getCommandeAxeImpConfig($codeSte);

        /*$array[] = [
            'code_bdd'          => 'NO_DA',
            'Libelle'           => $this->trad->t('DA'),
            'format'            => 'A / 9',

        ];

        $array[] = [
            'code_bdd'          => 'NO_PO_DA',
            'Libelle'           => $this->trad->t('Ligne DA'),
            'format'            => 'A / 3',

        ];*/

        $array[] = [
            'code_bdd'          => 'CODE_ART',
            'Libelle'           => $this->trad->t('Code article'),
            'format'            => 'A',
            'formatMask'        => 13,
        ];

        $array[] = [
            'code_bdd'          => 'DES_SOMM',
            'Libelle'           => $this->trad->t('Désign. Article'),
            'format'            => 'A',
            'formatMask'        => 60,

        ];

        $array[] = [
            'code_bdd'          => 'QTE_COMM',
            'Libelle'           => $this->trad->t('Quantité'),
            'format'            => 'N',
            'formatMask'        => '8.3',
            'obligatoire'       => true,
        ];

        $array[] = [
            'code_bdd'          => 'UNITE',
            'Libelle'           => $this->trad->t('Unité'),
            'format'            => 'A',
            'formatMask'        => 7,
            'obligatoire'       => true,
        ];

        $array[] = [
            'code_bdd'          => 'PU',
            'Libelle'           => $this->trad->t('Prix unitaire'),
            'format'            => 'N',
            'formatMask'        => '10.5',
            'obligatoire'       => true,
        ];

        $array[] = [
            'code_bdd'          => 'DTELIVCON',
            'Libelle'           => $this->trad->t('Délai'),
            'format'            => 'D',
            'formatMask'        => '##/##/####',
            'type'              => 'DATE',
        ];

        $array[] = [
            'code_bdd'          => 'NM_DCOMP',
            'Libelle'           => $this->trad->t('Désignation compl.'),
            'format'            => 'A',
            'formatMask'        => 3000,
        ];

        $array[] = [
            'code_bdd'          => 'REMISE',
            'Libelle'           => $this->trad->t('Remise (%)'),
            'format'            => 'N',
            'formatMask'        => '9.2',

        ];

        $array[] = [
            'code_bdd'          => 'TVA',
            'Libelle'           => $this->trad->t('TVA (%)'),
            'format'            => 'N',
            'formatMask'        => '9.2',
        ];

        $array[] = [
            'code_bdd'          => 'TOLLIVAVA',
            'Libelle'           => $this->trad->t('Livraison avancée tolérée'),
            'format'            => 'N',
            'formatMask'        => '9.0',
        ];

        $array[] = [
            'code_bdd'          => 'SSFAM',
            'Libelle'           => $this->trad->t('Sous-famille'),
            'format'            => 'A',
            'formatMask'        => 7,
        ];

        $array[] = [
            'code_bdd'          => 'SITE_STK',
            'Libelle'           => $this->trad->t('Site de stock'),
            'format'            => 'A',
            'formatMask'        => 7,
        ];

        $array[] = [
            'code_bdd'          => 'ACCORCA',
            'Libelle'           => $this->trad->t('Accord-cadre'),
            'format'            => 'A',
            'formatMask'        => 11,
        ];

        $array[] = [
            'code_bdd'          => 'CTRL_BR',
            'Libelle'           => $this->trad->t('Contrôle qualité'),
            'format'            => 'A',
            'formatMask'        => 3,
        ];

        $array[] = [
            'code_bdd'          => 'SECTIMP',
            'Libelle'           => $this->trad->t('Section d’imputation'),
            'format'            => 'A',
            'formatMask'        => 14,
            'obligatoire'       => !$configAxeImp->SectImp->isBlankAllowed,
        ];

        $array[] = [
            'code_bdd'          => 'CODGES',
            'Libelle'           => $this->trad->t('Code gestion'),
            'format'            => 'A',
            'formatMask'        => $cgParId ? 12:16,
            'functionValidator' => '',
            'paramValidator'    => '',
        ];

        $array[] = [
            'code_bdd'          => 'CODE_COMP',
            'Libelle'           => $this->trad->t('Code complémentaire'),
            'format'            => 'A',
            'formatMask'        => 14,
            'obligatoire'       => !$configAxeImp->CodeCompl->isBlankAllowed,
        ];

        $array[] = [
            'code_bdd'          => 'SSCPTEANA',
            'Libelle'           => $this->trad->t('Sous-compte d’analyse'),
            'format'            => 'A',
            'formatMask'        => 14,
            'obligatoire'       => !$configAxeImp->SousCompteAna->isBlankAllowed,
        ];

        $array[] = [
            'code_bdd'          => 'SAISIEREC',
            'Libelle'           => $this->trad->t('Saisie de Réception ?'),
            'format'            => 'L',

        ];

        $array[] = [
            'code_bdd'          => 'RECEP_AUTO',
            'Libelle'           => $this->trad->t('Réception automatique ?'),
            'format'            => 'L',
        ];

        $array[] = [
            'code_bdd'          => 'DATEDEBCONT',
            'Libelle'           => $this->trad->t('Début contrat'),
            'format'            => 'D',
            'formatMask'        => '##/##/####',
            'type'              => 'DATE',
        ];

        $array[] = [
            'code_bdd'          => 'DATEFINCONT',
            'Libelle'           => $this->trad->t('Fin contrat'),
            'format'            => 'D',
            'formatMask'        => '##/##/####',
            'type'              => 'DATE',
        ];

        $array[] = [
            'code_bdd'          => 'CONTRAT_PERIOD',
            'Libelle'           => $this->trad->t('Périodicité du contrat'),
            'format'            => 'A',
            'formatMask'        => '1 (M/T/S/A)',
        ];

        $array[] = [
            'code_bdd'          => 'FIN_DEB_PER',
            'Libelle'           => $this->trad->t('Type de période'),
            'format'            => 'A',
            'formatMask'        => '1 (D/F)',
        ];

        $array[] = [
            'code_bdd'          => 'CALI_RECEPAUTO_JOUR',
            'Libelle'           => $this->trad->t('Jour d\'Application'),
            'format'            => 'N',
            'formatMask'        => '2.0',
        ];

        $array[] = [
            'code_bdd'          => 'LONGU',
            'Libelle'           => $this->trad->t('Longueur (cm)'),
            'format'            => 'N',
            'formatMask'        => '9.2',
        ];

        $array[] = [
            'code_bdd'          => 'LARG',
            'Libelle'           => $this->trad->t('Largeur (cm)'),
            'format'            => 'N',
            'formatMask'        => '9.2',
        ];

        $array[] = [
            'code_bdd'          => 'HAUT',
            'Libelle'           => $this->trad->t('Hauteur (cm)'),
            'format'            => 'N',
            'formatMask'        => '9.2',
        ];

        $array[] = [
            'code_bdd'          => 'VOLUME',
            'Libelle'           => $this->trad->t('Volume (m3)'),
            'format'            => 'N',
            'formatMask'        => '9.2',
        ];

        $array[] = [
            'code_bdd'          => 'POIDS',
            'Libelle'           => $this->trad->t('Poids brut (kg)'),
            'format'            => 'N',
            'formatMask'        => '9.2',
        ];

        $array[] = [
            'code_bdd'          => 'POIDSNET',
            'Libelle'           => $this->trad->t('Poids net (kg)'),
            'format'            => 'N',
            'formatMask'        => '9.2',
        ];

        $array[] = [
            'code_bdd'          => 'PAY_ORI',
            'Libelle'           => $this->trad->t('Origine'),
            'format'            => 'A',
            'formatMask'        => 2,
        ];

        $array[] = [
            'code_bdd'          => 'DOUANE',
            'Libelle'           => $this->trad->t('Code douane'),
            'format'            => 'A',
            'formatMask'        => 12,
        ];

        $array[] = [
            'code_bdd'          => 'PAY_PRO',
            'Libelle'           => $this->trad->t('Provenance'),
            'format'            => 'A',
            'formatMask'        => 2,
        ];

        $array[] = [
            'code_bdd'          => 'REF_FOUR',
            'Libelle'           => $this->trad->t('Réf. Fournisseur'),
            'format'            => 'A',
            'formatMask'        => 30,

        ];

        $array[] = [
            'code_bdd'          => 'REF_FAB',
            'Libelle'           => $this->trad->t('Réf. Fabricant'),
            'format'            => 'A',
            'formatMask'        => 30,
        ];

        $array[] = [
            'code_bdd'          => 'COD_FAB',
            'Libelle'           => $this->trad->t('Fabricant'),
            'format'            => 'A',
            'formatMask'        => 12,
        ];

        $array[] = [
            'code_bdd'          => 'COD_GAM',
            'Libelle'           => $this->trad->t('Code gamme'),
            'format'            => 'A',
            'formatMask'        => 7,
        ];

        $array[] = [
            'code_bdd'          => 'TITREAV',
            'Libelle'           => $this->trad->t('Titre avant cette ligne'),
            'format'            => 'A',
            'formatMask'        => 3000,
        ];

        $array[] = [
            'code_bdd'          => 'MASQ_QTE',
            'Libelle'           => $this->trad->t('Masquer la quantité ?'),
            'format'            => 'L',
        ];

        $array[] = [
            'code_bdd'          => 'MASQ_UNI',
            'Libelle'           => $this->trad->t('Masquer l\'unité ?'),
            'format'            => 'L',
        ];

        $array[] = [
            'code_bdd'          => 'MASQ_PU',
            'Libelle'           => $this->trad->t('Masquer le PU ?'),
            'format'            => 'L',
        ];

        $array[] = [
            'code_bdd'          => 'MASQ_DS',
            'Libelle'           => $this->trad->t('Masquer la désignation sommaire ?'),
            'format'            => 'L',
        ];

        $array[] = [
            'code_bdd'          => 'SSTOAPR',
            'Libelle'           => $this->trad->t('Imprimer un sous-total après cette ligne ?'),
            'format'            => 'L',
        ];

        $array[] = [
            'code_bdd'          => 'TYPDATE',
            'Libelle'           => $this->trad->t('Présentation date de livraison'),
            'format'            => 'A',
            'formatMask'        => '1 (D/S/M)',
        ];

        return $array;
    }

    /**
     * @return array<mixed>
     */
    public function parseExcel(string $fichier, string $codeSte)
    {
        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($fichier);
        $this->sheet = $spreadsheet->getSheet(0);

        $highestRow = $spreadsheet->getActiveSheet()->getHighestRow();

        $array = [];
        for ($i=0; $highestRow > $i; $i++) {
            $listeChps = $this->getArchitecture($codeSte);
            $ligne = [];

            foreach ($listeChps as $k => $v) {
                $valueCell = $this->parseCell($i+1, $k+1);

                if (isset($v['type']) && $v['type'] == 'DATE') {
                    $valueCell = SpreadsheetHelper::parseDate($valueCell);
                    $valueCell = Date::dateConverte($valueCell);
                }

                if ($valueCell === null) {
                    $valueCell = '';
                }

                $ligne[$v['code_bdd']] = $valueCell;
            }

            $array[] = $ligne;
        }

        return $array;
    }

    /**
     * @param string|int $row
     * @param string|int $column
     * @return mixed
     */
    private function parseCell($row, $column)
    {
        return $this->sheet->getCell([
            intval($column),
            intval($row)
        ])->getValue();
    }
}
