<?php

namespace Traits\Validator;

use Helpers\CaracCompl\CaracCompl as CaracComplCaracCompl;
use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Outil\Date;
use Helpers\Outil\Sql;
use Helpers\Trad\ITrad;
use Helpers\Trad\Trad;
use Helpers\Validator\Validator;

trait CaracCompl
{

    abstract public function getTrad(): ITrad;

    /**
     * @param array<mixed> $post
     * @return array<mixed>
     */
    public function validatorCC(\PDO $pdo, &$post, string $idPost = '')
    {
        $array = json_decode(($post['saveValidatorCC' . $idPost] ?? ''), true);

        $init      = $array['init'] ?? '';
        $typeDoc   = $array['typeDoc'] ?? '';
        $idDoc     = $array['idDoc'] ?? '';
        $idLigne   = $array['idLigne'] ?? '';
        $idPanier  = $array['idPanier'] ?? '0';
        $article   = $array['article'] ?? '';
        $ssFamille = $array['ssFamille'] ?? '';
        $famille   = $array['famille'] ?? '';
        $societe   = $array['societe'] ?? '';

        if ($typeDoc == 'CDE') {
            $typeDoc = 'CA';
        }

        $Validator = new Validator($pdo, $this->getTrad());

        $trad = $this->getTrad();

        $caracCompl = new CaracComplCaracCompl($pdo, $article, $ssFamille, $famille);
        if ($init) {
            $listeCC = $caracCompl->getListeCCInit();
        } else {
            $listeCC = $caracCompl->getCCDocc($typeDoc, $idDoc, $idLigne, $idPanier);
        }


        $returnCS = [];
        foreach ($listeCC as $cc) {
            if (sql::boolSql($cc['CC_IS_TITLE'])) {
                continue;
            }

            $key = 'CC_' . $cc['CC_ID'];

            if ($cc['FORMAT'] == 'L') {
                $value = null;
                if (isset($post[$key])) {
                    $value = $post[$key];
                }
            } else {
                $value = $post[$key] ?? '';
            }

            if (($cc['DOC_TYPE'] ?? '') === 'BRT') {
                $value = $cc['VALEUR'];

                if ($cc['FORMAT'] === 'D') {
                    $value = Date::dateConverte($value);
                }
            }

            $obli = (Sql::toBool($cc['CC_REQUIRED']) && (($typeDoc == 'BR' || $typeDoc == 'BRT') || ($typeDoc != 'BR' && $typeDoc != 'BRT' && !SQL::boolSql($cc['CC_UPD_ONLY_RECEIPT'])))) ? 1 : 0;

            $array = ChampSupp::getListeTableStd();
            $zoom  = [];
            foreach ($array as $k => $valeur) {
                if ($k == $cc['CC_TYPE']) {
                    $zoom = $array[$k];
                    break;
                }
            }

            if (sql::boolSql($cc['TBLSECTO'])) {
                if ($societe == '') {
                    $societe = $_SESSION['Session_SocUsr'];
                }

                $codeSte = $societe;
            } else {
                $codeSte = '';
            }

            if (sql::boolSql($cc['CONTROLE']) && !sql::boolSql($cc['CTRL_STD'])) {
                $Validator->validatorTSDON($trad->t('Caractéristique complémentaire :') . $cc['NOM'], $value, $cc['CC_TYPE'], $pdo, !$obli, $codeSte);
            } elseif ($zoom) {
                if (key_exists(2, $zoom)) {
                    $where = [$zoom[2] => 'T'];
                } else {
                    $where = array();
                }

                $Validator->zoom($zoom[0], $value, $trad->t('Caractéristique complémentaire :') . $cc['NOM'], $pdo, !$obli, $where);
            } elseif ($cc['FORMAT'] == 'D') {
                $Validator->isDate($value, '', '', !$obli, $trad->t('Caractéristique complémentaire :') . $cc['NOM']);

                $post[$key] = $value;
            } elseif ($cc['FORMAT'] == 'N' || $cc['FORMAT'] == '1') {
                $value      = str_replace(' ', '', $value);
                $post[$key] = $value;
                $Validator->isFloat($trad->t('Caractéristique complémentaire :') . $cc['NOM'], $value, $cc['PE_LONG'] . '.' . $cc['PD_LONG'], $obli);
                $post[$key] = $value;
            } elseif ($cc['FORMAT'] == 'L') {
                $Validator->isCheck($value);
                $post[$key] = isset($this->valueForWSDL) && $this->valueForWSDL
                    ? ($value ? 'O' : 'N')
                    : $value;
                // if (isset($this->valueForWSDL) && $this->valueForWSDL) { // TODO : move this logic inside WSDL
                //     $post[$key] = $value ? 'O' : 'N';
                // } else {
                //     $post[$key] = $value;
                // }
            } elseif ($cc['FORMAT'] == 'M') {
                $Validator->isStringAvecEspace($trad->t('Caractéristique complémentaire :') . $cc['NOM'], $value, $obli, 3000);
            } elseif ($cc['FORMAT'] == 'H') {
                $Validator->isHourWithSecondeCC($trad->t('Caractéristique complémentaire :') . $cc['NOM'], $value);
            } elseif ($cc['FORMAT'] == 'E') {
                $Validator->adresseMail($trad->t('Caractéristique complémentaire :') . $cc['NOM'], $value, $obli, 100, true);
            } elseif ($cc['FORMAT'] == 'U') {
                $Validator->isUrlValide($trad->t('Caractéristique complémentaire :') . $cc['NOM'], $value, $obli);
            } else {
                $Validator->isStringAvecEspace(
                    $trad->t('Caractéristique complémentaire :') . $cc['NOM'],
                    $value,
                    $obli,
                    ($cc['ALP_LONG'] == '0' ? '' : $cc['ALP_LONG'])
                );
            }
            $returnCS[$cc['CC_ID']] = $post[$key];
        }

        $this->validate += $Validator->validate;
        $this->error .= $Validator->error;

        return $returnCS;
    }
}
