<?php
namespace TwigExtension;

use Entity\Model\Pays;
use Entity\Model\TypeAchat;
use Helpers\Trad\ITrad;
use Modules\Ajax\Repository\Listes;
use Modules\Commande\Repository\ModificationDetailCommande;
use Psr\Container\ContainerInterface;
use Skeleton\Router;
use Helpers\Outil\Roles;
use Helpers\Trad\Trad;

class ListeValueExtension extends \Twig\Extension\AbstractExtension
{

    /** @phpstan-ignore-next-line */
    private Router $router;
    private \Pdo $pdo;
    
    /** @phpstan-ignore-next-line */
    private ITrad $trad;
    
    public function __construct(Router $router, \PDO $pdo, ContainerInterface $container)
    {
        $this->router = $router;
        $this->pdo = $pdo;
        $this->trad = $container->get('translationService');
    }

    /**
     * @return array<mixed>
     */
    public function getFunctions()
    {
        return [
            new \Twig\TwigFunction('listePays', [
                $this,
                'listePays'
            ], [
                'is_safe' => [
                    'html'
                ]
            ]),
            new \Twig\TwigFunction('listePeriodicite', [
                $this,
                'listePeriodicite'
            ], [
                'is_safe' => [
                    'html'
                ]
            ]),
            new \Twig\TwigFunction('listeTypeAchat', [
                $this,
                'listeTypeAchat'
            ], [
                'is_safe' => [
                    'html'
                ]
            ]),
            new \Twig\TwigFunction('listeModeTransport', [
                $this,
                'listeModeTransport'
            ], [
                'is_safe' => [
                    'html'
                ]
            ]),
            new \Twig\TwigFunction('listeJours', [
                $this,
                'listeJours'
            ], [
                'is_safe' => [
                    'html'
                ]
            ]),
        ];
    }

    /**
     * @return array<mixed>
     */
    public function listePays()
    {
        $listePaysTmp = (new Pays($this->pdo))->fetchAll([], ['tri_LIBELLE' => 'asc']);
        $listePaysTmp[''] = [
            'CODE_PAYS' => '',
            'LIBELLE' => '',
        ];
        return array_column($listePaysTmp, 'LIBELLE', 'CODE_PAYS');
    }

    /**
     * @return array<mixed>|string
     */
    public function listePeriodicite(string $code = null)
    {
        $return = [
            'M' => $this->trad->t('Mensuelle'),
            'T' => $this->trad->t('Trimestrielle'),
            'S' => $this->trad->t('Semestrielle'),
            'A' => $this->trad->t('Annuelle'),
        ];

        if ($code) {
            return $return[$code];
        } else {
            return $return;
        }
    }

    /**
     * @return array<mixed>
     */
    public function listeTypeAchat()
    {
        $listeTypeAchatTmp = (new Listes($this->pdo))->typeAchat([]);
        return array_column($listeTypeAchatTmp, 'LIB_TAC', 'TYP_ACH');
    }

    /**
     * @return array<mixed>
     */
    public function listeModeTransport()
    {
        $listeModeTransport = (new ModificationDetailCommande($this->pdo))->getListeModeTransport();

        $tabModeTransport = [];
        foreach ($listeModeTransport as $modeTransport) {
            if ($modeTransport['DES_ART']) {
                $libelle = $modeTransport['DES_ART'];
            } else {
                $libelle = $modeTransport['MOD_TRS'];
            }
            
            $tabModeTransport[$modeTransport['MOD_TRS']] = $libelle;
        }

        return $tabModeTransport;
    }

    /**
     * @return array<mixed>
     */
    public function listeJours()
    {
        $liste = [];

        for ($i=1; $i<32; $i++) {
            $liste[$i] = $i;
        }

        return $liste;
    }
}
