<?php
namespace Modules\RechercheArticles\PunchOut;

use Entity\Model\Fourniss;
use Entity\Model\OxaUserPanierLigne;
use Entity\Model\PFormats;
use Helpers\Outil\Outils;
use Helpers\Trad\ITrad;
use Modules\Panier\Service\PanierLigneService;
use Modules\RechercheArticles\Repository\Punchout as RepositoryPunchout;
use Skeleton\Router;

abstract class PunchOut
{
    // Propriété d'une connexion PunchOut

    /**
     * Identifiant du catalogue de type punchOut
     * @var string $id_cat
     */
    protected $id_cat;

    /**
     * Code du fournisseur proposant le punchOut
     * @var string $code_frs
     */
    protected $code_frs;

    /**
     * Format d'échange du punchOut (CXML ou OCI)
     * @var string $format_po
     */
    protected $format_po;
    
    /**
     * Url de connexion au site du fournisseur proposant le punchOut
     * @var string $url_connexion_po
     */
    protected $url_connexion_po;
    
    /**
     * Url de retour sur l'application Oxaproc
     * @var string $url_retour_oxaproc
     */
    protected $url_retour_oxaproc;
    
    /**
     * Tableau contenant les informations relatives au catalogue PunchOut
     * @var array<mixed> $info_catalogue
     */
    protected $info_catalogue;
    
    /**
     * @var array<mixed> $lexique
     */
    protected $lexique;

    /**
     * @var mixed $punchOuRepo
     */
    protected $punchOuRepo;

    protected \PDO $pdo;
    protected ITrad $trad;

    /**
     * @var array<mixed> $listeLigne
     */
    protected $listeLigne;

    /**
     * @var array<mixed> $error
     */
    protected $error;

    public function __construct(\PDO $pdo, Router $router, ITrad $trad, string $id_cat)
    {
        $this->pdo = $pdo;
        $this->trad = $trad;
        $this->id_cat = $id_cat;
        $this->punchOuRepo = new RepositoryPunchout($pdo);
        $this->error = [];

        $this->info_catalogue = $this->punchOuRepo->getInfoPunchout($this->id_cat);

        $this->code_frs         = $this->info_catalogue['REGRFOUR'];
        $this->format_po        = $this->info_catalogue['SRCDATA'];
        $this->url_connexion_po = $this->info_catalogue['URL_CONNECT'];

        $this->listeLigne       = [];

        $urlRetour = Outils::getRootUrl();
        $urlRetour .= $router->generateUri('RechercheArticles.Punchout.createItems', ['idCat' => $id_cat]);

        $this->url_retour_oxaproc = $urlRetour;
        
        $this->lexique = [
            'PRIXNET'       => 'PX_ART',
            'CODEACDR'      => 'COD_ACD',
            'UNITE'         => 'COD_UNIT',
            'REFARTFOU'     => 'REF_FOURN',
            'DESIGNCRT'     => 'DES_ART',
            'DESIGNLG'      => 'DES_COMP',
            'CODEFOUR'      => 'COD_FOURN',
            'SOUSFAMI'      => 'COD_SFAM',
            'REFARTFAB'     => 'REF_FAB',
            'FABRICANT'     => 'COD_FAB',
            'GAMFABRIC'     => 'COD_GAM',
            'CONDITNT'      => 'CONDITNT',
            'TXTCOND'       => 'TXTCOND',
            'DEVISE_CAT'    => 'COD_DEV',
            'TAUX_TVA'      => 'TAUX_TVA',
            'IMAGE'         => 'IMGART',
            'CODEARTI'      => 'COD_ART',
            'DELAIREA'      => 'DELAI',
            'CARACTER'      => 'CARACT',
            'TYPIMAGE'      => 'TYPIMAGE'
        ];
    }

    public function getDestination() : string
    {
        $pFormat = new PFormats($this->pdo);
        $infoFormat = $pFormat->find([
            'IDFORMAT'      => $this->info_catalogue['IDFORMAT'],
            'NOM_PARAMETRE' => 'target',
            'IDCLE'         => 'PARAM',
        ]);
        return $infoFormat['VALEUR_PARAM']??'';
    }

    /**
     * @param array<string> $base
     * @return void
     */
    public function initLigne(&$base)
    {
        $base['DEVISE']             = '';
        $base['PROVENANCE']         = 'PO'; // La provenance de la ligne est de type PunchOut
        $base['IDLIGNE']            = '';
        $base['NO_SERIALIZATION']   = ''; // Utilisation reference unique information
        $base['DATABASEID']         = ''; // Utilisation reference unique information
        $base['ID_CAT']             = $this->id_cat;
        $base['COD_ART']            = '';
        $base['COD_FOURN']          = $this->code_frs;
        $base['RS_FOURN']           = (new Fourniss($this->pdo))->find($this->code_frs, ['RS'])['RS']??'';
        $base['VILLE_FOURN']        = '';
        $base['COD_FAB']            = '';
        $base['RS_FAB']             = '';
        $base['COD_GAM']            = '';
        $base['LIB_GAM']            = '';
        $base['REF_FAB']            = '';
        $base['CODE_FAM']           = '';
        $base['DESIGNFAM']          = '';
        $base['PX_ART_INIT']        = '';
        $base['DELAI']              = '';
        $base['CARACT']             = '';
        $base['FICHTECH']           = false;
        $base['ARTASSOCIE']         = false;
        $base['ARTASSOCIE']         = false;
        $base['IMGART']             = '';
        $base['TYPIMAGE']           = '';
        $base['COD_ACD']            = '';
        $base['CPOSTAL']            = '';
        $base['VILLE1']             = '';
        $base['COD_GRP']            = '';
        $base['CONDITNT']           = '0';
        $base['TXTCOND']            = '';
        $base['CLASSNUM']           = '';
        $base['TYPECATA']           = '';
        $base['CATPRLST']           = $this->id_cat;
        $base['ID_EXTERNE']         = '';
    }

    public function insertListeArticleFromPunchOut(\PDO $pdoOxaFT, ?string $mode = null):void
    {
        if ($mode == 'edit') {
            (new OxaUserPanierLigne($this->pdo))->delete([
                'id_user'       => $_SESSION['id_user'],
                'id_panier'     => $_SESSION['id_panier'],
                'PROVENANCE'    => 'PO',
                'ID_CAT'        => $this->id_cat,
                'COD_FOURN'     => $this->code_frs
            ]);
        }

        $panierLigneService = new PanierLigneService($this->pdo, $pdoOxaFT, 'PUNCHOUT');
        
        foreach ($this->listeLigne as $ligne) {
            $ligne['type_de_ligne'] = 1;
            $ligne['DEVISE'] = $ligne['COD_DEV'];
            $panierLigneService->addPanier($ligne, $ligne['qte']);
        }
    }

    public function getError():string
    {
        return implode("<br />", $this->error);
    }

    /**
     * @return mixed
     */
    abstract public function getUrlPunchOut();

    /**
     * @param array<mixed> $content
     * @return void
     */
    abstract public function parseRetour($content):void;
}
