<?php
namespace Modules\Parametrage\Evaluation\Controller;

use Entity\Model\ParamDico;
use GuzzleHttp\Psr7\Response;
use Helpers\Ariane\Ariane;
use Modules\Evaluation\Service\ResultatService;
use Modules\Parametrage\Evaluation\Service\ParamEvaluationService;
use Skeleton\Controller\Controller;
use Skeleton\Renderer\RendererInterface;
use Skeleton\Router;
use Psr\Container\ContainerInterface;
use PDO;

class ParametrageEvaluationController extends Controller
{
    /**
     * @var string $menuActive
     */
    protected $menuActive;
    /**
     * @var string $menuLeft
     */
    protected $menuLeft;
    /**
     * @var int $indexAriane
     */
    protected $indexAriane;

    protected ParamDico $paramDico;
    protected ParamEvaluationService $paramEvaluationService;
    
    public function init() : void
    {
        if (($_SESSION['ariane'][0]['path']??null) == $this->router->generateUri('Parametrage.Parametrage.index')) {
            $this->indexAriane = 1;
            $this->menuActive = 'Parametrage';
            $this->menuLeft = 'Parametrage';
        } else {
            $this->indexAriane = 0;
            $this->menuActive = 'Evaluation';
            $this->menuLeft = 'Parametrage';
        }
    }

    public function __construct(
        RendererInterface $renderer,
        PDO $pdo,
        Router $router,
        ContainerInterface $contenaire
    ) {
        parent::__construct($renderer, $pdo, $router, $contenaire);
    
        $this->paramEvaluationService = new ParamEvaluationService();
    }

    /**
     * @return Response|string
     */
    public function evaluationAction()
    {
        $this->paramDico = new ParamDico($this->pdo);
        $erreur = '';
        $trad = $this->getTranslationService();

        // Get params
        $appParam = (new ResultatService($this->pdo))->getEvalParamdico();

        $appParam = $this->paramEvaluationService->getFormFromParamDico($appParam);

        Ariane::generate($this->indexAriane, $trad->t('Paramétrage des évaluations'), []);

        return $this->renderer->render('@ParamEval/ParamEvaluationForm', [
            'menuActive' => $this->menuActive,
            'menuLeft' => $this->menuLeft,
            'appParam' => $appParam,
            'erreur' => $erreur,
            'listeValue' => array_combine(range(0, 10), range(0, 10))
        ]);
    }

    public function evaluationUpdateAction() :Response
    {
        $post = $this->request->getParsedBody();
        $trad = $this->getTranslationService();
        $this->paramDico = new ParamDico($this->pdo);

        $validator = $this->createValidator();

        // Validate
        foreach ($post as $key => $value) {
            if (strpos($key, 'EVAL_TEXT') === 0) {
                $validator->isStringAvecEspace(
                    $trad->t('Libellé'),
                    $value,
                    0,
                    255
                );
            } elseif (strpos($key, 'EVAL_MAX') === 0) {
                if (!in_array($value, range(0, 10))) {
                    $validator->setError($trad->t('Les notes doivent être comprises entre 0 et 10.'));
                }
            }
        }

        if ($post['EVAL_MAX_SEG_2'] <= $post['EVAL_MAX_SEG_1']) {
            $validator->setError($trad->t('La note max statut 2 doit être supérieure à la note max statut 1.'));
        }

        if ($post['EVAL_MAX_SEG_3'] <= $post['EVAL_MAX_SEG_2']) {
            $validator->setError($trad->t('La note max statut 3 doit être supérieure à la note max statut 2.'));
        }

        if (!$validator->isValid()) {
            return new Response(400, [], $validator->getError());
        }

        //___________________
        // Update DB
        
        $arrayUpdate = $this->paramEvaluationService->getParamDicoFromForm($post);

        if ($this->paramDico->find([
            'TYPEDICO' => 'EVAL_FOURN',
            'ID1' => 'NOTATION'
        ])) {
            $this->paramDico->update($arrayUpdate, [
                'TYPEDICO' => 'EVAL_FOURN',
                'ID1' => 'NOTATION'
            ]);
        } else {
            $this->paramDico->insert($arrayUpdate + [
                'TYPEDICO' => 'EVAL_FOURN',
                'ID1' => 'NOTATION'
            ]);
        }
        
        return new Response();
    }
}
