<?php

namespace Modules\Additional\Specifique\Client37\Html;

use Helpers\Trad\ITrad;
use Modules\Additional\Specifique\Client37\Modules\Signature\Service\WorkflowSpecifique;
use Modules\Panier\Service\ValidationPanierService;
use Psr\Container\ContainerInterface;
use TwigExtension\FormExtension;
use Skeleton\Router;

class HtmlExtension extends \Twig\Extension\AbstractExtension
{
    private Router $router;
    private ContainerInterface $contenaire;
    private \PDO $pdo;
    private ITrad $trad;
    
    public function __construct(Router $router, ContainerInterface $contenaire, \PDO $pdo)
    {
        $this->router = $router;
        $this->pdo = $pdo;
        $this->contenaire = $contenaire;
        $this->trad = $contenaire->get(ITrad::class);
    }

    public function getFunctions()
    {
        return [
            new \Twig\TwigFunction('specifiqueBouygueSignature', [
                $this,
                'specifiqueBouygueSignature'
            ], [
                'is_safe' => [
                    'html'
                ]
            ]),
            new \Twig\TwigFunction('specifiqueBouygueSignaturePanier', [
                $this,
                'specifiqueBouygueSignaturePanier'
            ], [
                'is_safe' => [
                    'html'
                ]
            ])
        ];
    }
    
    /**
     * @param array<mixed> $array
     * @return string
     */
    public function specifiqueBouygueSignature($array):string
    {
        $alogSpecif = json_decode($array['algoSpecif']);
        $listAnomalie = $array['listAnomalie'];
        $html = '';

        $listeUser = (new WorkflowSpecifique())->getLstSignataire($alogSpecif);

        if (!$listeUser || empty($listAnomalie)) {
            return '';
        } elseif (count($listeUser) > 1) {
            $listeUser[''] = $this->trad->t('Votre choix');
        }
        ksort($listeUser);

        $html .= "<div class='row largeLabel'>";
        /** @phpstan-ignore-next-line */
        $formExtension = new FormExtension($this->router, $this->pdo, $this->contenaire);
        $html .= $formExtension->inputAdvance('SELECT', [
            'name'          => ($array['docId']?'NvSignataire['.$array['docId'].']':'NvSignataire'),
            'titre'         => $this->trad->t('Votre seuil de signature est insuffisant vous devez choisir un autre signataire'),
            'listeValue'    => $listeUser,
        ]);

        $html .= "</div>";

        if (isset($array['modeValidation']) && $array['modeValidation']) {
            $html .= "<input type='hidden' name='algoSpecif' value='".json_decode($array['algoSpecif'])."' />";
        }

        return $html;
    }

    /**
     * Summary of specifiqueBouygueSignaturePanier
     * @param array<mixed> $array
     * @return string
     */
    public function specifiqueBouygueSignaturePanier($array):string
    {
        $html = '';
        $alogSpecif = json_decode($array['algoSpecif']);
        $post = $array['post'];

        $listeUser = (new WorkflowSpecifique())->getLstSignataire($alogSpecif);

        if (!$listeUser) {
            return '';
        } elseif (count($listeUser) > 1) {
            $listeUser[''] = $this->trad->t('Votre choix');
        }
        ksort($listeUser);

        $html .= "<div class='row largeLabel'>";
        /** @phpstan-ignore-next-line */
        $formExtension = new FormExtension($this->router, $this->pdo, $this->contenaire);

        $critere = ($array['typeDoc'] == ValidationPanierService::CDE)? $array['critere']:$array['typeDoc'];

        if ($array['typeDoc'] != ValidationPanierService::CDE) {
            $value = $post[$array['typeDoc']]['sign_spec']??'';
        } else {
            $value = $post[$array['typeDoc']][$array['critere']]['sign_spec']??'';
        }

        $html .= $formExtension->inputAdvance('SELECT', [
            'name'          => 'sign_spec_'.$critere,
            'titre'         => $this->trad->t('Signataires'),
            'listeValue'    => $listeUser,
            'value'         => $value,

            'inputAttr' => [
                'data-updatevalue-std' => $this->router->generateUri('Panier.validationPanier.updateValue'),
                'data-updatevalue-typedoc' => $array['typeDoc'],
                'data-updatevalue-critere' => $array['critere'],
            ]
        ]);

        $html .= "</div>";

        return $html;
    }
}
