<?php

namespace Modules\Evaluation\MenuContextuel;

use Helpers\Outil\MenuContextuel;
use Helpers\Trad\ITrad;
use Helpers\Trad\Trad;
use Helpers\User\User;
use Skeleton\Router;

class CampaignMenuContextuel extends MenuContextuel
{
    /**
     * @var array<mixed> droit
     */
    public $droits;

    /**
     * __construct
     *
     * @param  \PDO $pdo
     * @param  Router $router
     * @param  ITrad $trad
     * @param  array<mixed> $droits
     * @return void
     */
    public function __construct(\PDO $pdo, Router $router, ITrad $trad, array $droits = [])
    {
        parent::__construct($pdo, $router, $trad);

        $this->droits = $droits;
    }

    /**
     * @param string $id
     * @param string $supplierCode
     * @param string $supplierName
     * @return array<mixed>
     */
    public function generate(string $id, string $supplierCode, string $supplierName): array
    {
        $menuContextuel = [];

        $menuContextuel['listeSecondaire'][] = [
            'lien' => '',
            'libelle' => $this->trad->t('Supprimer'),
            'lienJs' => $this->popupConfirm(
                false,
                $this->router->generateUri(
                    'Evaluation.Campaign.supplierRemove',
                    ['id' => $id, 'supplier' => $supplierCode]
                ),
                Trad::strBuild($this->trad->t('Voulez-vous retirer le fournisseur @1@ de la campagne ?'), $supplierName),
                false,
                'refreshFrs',
            ),
        ];

        return $menuContextuel;
    }

    /**
     * generateHeading
     *
     * @param  array<mixed> $campaign
     * @return array<mixed>
     */
    public function generateHeading(array $campaign): array
    {
        $menuContextuel = [];
        if ($this->droits['HA_CEVALCM']) {
            if ($campaign['STATUT'] == 'P') {
                $menuContextuel['listeSecondaire'][] = [
                    'lien' => '',
                    'libelle' => $this->trad->t('Valider'),
                    'lienJs' => $this->popupConfirmCampaign(
                        $this->router->generateUri(
                            'Evaluation.Campaign.validation',
                            [
                                'id' => $campaign['ID_CAMP'],
                            ]
                        ),
                        $this->router->generateUri(
                            'Evaluation.Campaign.validation',
                            [
                                'id' => $campaign['ID_CAMP'],
                            ]
                        ),
                        $this->trad->t('Voulez-vous valider la préparation de cette campagne ?'),
                    ),
                    ''
                ];
                $menuContextuel['listeSecondaire'][] = [
                    'lien' => $this->router->generateUri(
                        'Evaluation.Campaign.createdit',
                        ['id' => $campaign['ID_CAMP']]
                    ),
                    'libelle' => $this->trad->t('Modifier'),
                ];
                if ($this->droits['HA_CEVASUP']) {
                    $menuContextuel['listeSecondaire'][] = [
                        'lien' => '',
                        'libelle' => $this->trad->t('Supprimer'),
                        'lienJs' => $this->popupConfirm(
                            false,
                            $this->router->generateUri(
                                'Evaluation.Campaign.suppression',
                                ['id' => $campaign['ID_CAMP']]
                            ),
                            Trad::strBuild($this->trad->t('Voulez-vous supprimer la campagne @1@ ?'), $campaign['LIBELLE']),
                            false,
                            'redirect',
                        ),
                    ];
                }
            } elseif (in_array($campaign['STATUT'], ['E', 'D', 'N'])) {
                $menuContextuel['listeSecondaire'][] = [
                    'lien' => '',
                    'libelle' => $this->trad->t('Dévalider'),
                    'lienJs' => "popupConfirmation(
                        '" . $this->router->generateUri(
                        'Evaluation.Campaign.popupConfirmationDevalidation',
                    ) . "', '', 
                        '" . $this->router->generateUri(
                        'Evaluation.Campaign.devalidation',
                        ['id' => $campaign['ID_CAMP']]
                    ) . "',  
                        '" . $this->trad->t('Voulez-vous dévalider cette campagne ?') . "', 
                        'main-content', 
                        '', '', '', '', '', '', '', '', '', '" . $this->trad->t('Dévalidation campagne') . "')",
                ];
                $menuContextuel['listeSecondaire'][] = [
                    'lien' => '',
                    'libelle' => $this->trad->t('Clôturer'),
                    'lienJs' => "popupConfirmation(
                        '" . $this->router->generateUri('Evaluation.Campaign.cloture', ['id' => $campaign['ID_CAMP']]) . "', 
                        'reload', 
                        '" . $this->router->generateUri('Evaluation.Campaign.cloture', ['id' => $campaign['ID_CAMP']]) . "', 
                        '" . Trad::strBuild($this->trad->t('Voulez-vous clôturer la campagne @1@ ?'), $campaign['ID_CAMP']) . "', 
                        'page', '', '', '', '', '', '', '', '', '', 
                        '" . $this->trad->t('Clôture campagne') . "')",
                ];
            }
            $menuContextuel['listeSecondaire'][] = [
                'lien' => $this->router->generateUri(
                    'Evaluation.Campaign.duplicate',
                    ['id' => $campaign['ID_CAMP']]
                ),
                'libelle' => $this->trad->t('Dupliquer'),
            ];
        }

        if (($campaign['STATUT'] == 'D') && (User::getUser()->getCodeUser() == $campaign["CREEEPAR"])) {
            $menuContextuel['listeSecondaire'][] = [
                'lien' => $this->router->generateUri('Evaluation.Evaluation.notationDepouilleur', ['idCamp' => $campaign['ID_CAMP']]),
                'libelle' => $this->trad->t('Notation dépouilleur'),
            ];
        }

        $menuContextuel['listeSecondaire'][] = [
            'lien' => $this->router->generateUri(
                'Evaluation.CampaignEvent.monitoring',
                ['id' => $campaign['ID_CAMP']]
            ),
            'libelle' => $this->trad->t('Suivi des évènements'),
        ];
        return $menuContextuel;
    }

    protected function popupConfirmCampaign(string $urlPopup, string $path, string $message, bool $popup = false, string $div = '', string $option = '', bool $loader = false, string $form = ''): string
    {
        $message = urlencode($message);

        return "popupConfirmation('" . $urlPopup . "', '','" . ($path) . "', '" . $message . "', '" . $div . "', '" . $option . "', '" . $loader . "', '" . ($popup ? 'true' : 'false') . "', '" . $form . "')";
    }
}
