<?php

namespace Modules\Additional\Specifique\Client37\Html;

use Helpers\Trad\ITrad;
use Modules\Additional\Specifique\Client37\Modules\Signature\Service\WorkflowSpecifique;
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('translationService');
    }

    public function getFunctions()
    {
        return [
            new \Twig\TwigFunction('specifiqueBouygueSignature', [
                $this,
                'specifiqueBouygueSignature'
            ], [
                '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;
    }
}
