<?php
namespace TwigExtension;

use Twig\Extension\AbstractExtension;
use Traits\Menu\Droit;
use PDO;
use Twig\TwigFunction;
use Container\MenuTop\S2C;
use Container\MenuTop\RelationFournisseur;
use Container\MenuTop\PilotageAchat;
use Container\MenuTop\P2P;
use Container\MenuTop\GlobalApplication;
use Entity\Model\Hastutil;
use Helpers\Additional\Additional;
use Helpers\Session\Session;
use Helpers\Trad\ITrad;
use Psr\Container\ContainerInterface;
use Skeleton\Router;
use Helpers\Outil\Roles;
use Helpers\Session\Enums\MenuTopEnum;

class MenuTopExtension extends AbstractExtension
{

    use Droit;
    private ITrad $trad;
    protected Additional $additional;


    
    public function __construct(private Router $router, private ContainerInterface $contenaire, private PDO $pdo)
    {
        $this->trad = $contenaire->get(ITrad::class);
        $this->additional = new Additional($this->pdo, $this->router);
    }


    public function getFunctions()
    {
        return [
            new TwigFunction('menuTop', $this->menuTop(...), [
                'is_safe' => [
                    'html'
                ]
            ])
            
        ];
    }
    
    /**
     * menuTop
     *
     * @param  string $menuActive
     * @param  bool|string $externe
     */
    public function menuTop($menuActive = '', $externe = false) : string
    {
        if (empty($menuActive) && isset($_SESSION['navigation']['menuActive'])) {
            $menuActive = $_SESSION['navigation']['menuActive'];
        }


        $html = "<nav id='menu-principal' class=''>
                    

                    <ul class='bs-row'>";
        $trad = $this->trad;

        // A mettre avant la vérification pour que le lien active ne soit mis que sur le bon lien.
        // Cas d'application unique à la logique de cette extension twig.
        $typeMenuTop = Session::getSessionTypeMenuTop();

        if ($externe == false) {
            if (!$_SESSION['ConnexionEproc'] && $_SESSION["Session_Login"] != 'OXAUSER') {
                $html .="       <li id='menuTopAccueil' class='active'>
                                    <a>
                                        <img class='icon-home' src='".$this->contenaire->get('baseUrl')."IconeV2/Misc/home.svg' title='Home' alt='Home'>
                                    </a>
                                </li>";
            } else {
                $html .="       <li id='menuTopAccueil' class='".(($menuActive == 'Accueil') ?  'active' : '' )."'>
                                    <a href='".$this->router->generateUri('Index.Index.index')."'>
                                        <img class='icon-home' src='".$this->contenaire->get('baseUrl')."IconeV2/Misc/home.svg' title='Home' alt='Home'>
                                    </a>";

                if (Roles::GetALL()) {
                    $html .= "     <div>
                                        <ul>
                                            <li ".(Session::verifySessionTypeMenuTop(MenuTopEnum::tryFrom('P2P')) ? "class='active'":"")."><a onClick=\"changeModule('".$this->router->generateUri('Index.Index.changeModule')."','P2P');\">P2P</a></li>
                                            <li ".(Session::verifySessionTypeMenuTop(MenuTopEnum::tryFrom('S2C')) ? "class='active'":"")."><a onClick=\"changeModule('".$this->router->generateUri('Index.Index.changeModule')."','S2C');\">S2C</a></li>
                                            <li ".(Session::verifySessionTypeMenuTop(MenuTopEnum::tryFrom('RelationFournisseurs')) ? "class='active'":"")."><a onClick=\"changeModule('".$this->router->generateUri('Index.Index.changeModule')."','RelationFournisseurs');\">Relation fournisseur</a></li>
                                            <li ".(Session::verifySessionTypeMenuTop(MenuTopEnum::tryFrom('PilotageAchats')) ? "class='active'":"")."><a onClick=\"changeModule('".$this->router->generateUri('Index.Index.changeModule')."','PilotageAchats');\">Pilotage Achat</a></li>
                                        </ul>
                                    </div>";
                }
                
                
                $html .="           </li>";
                // affichage des different type de menu

                switch ($typeMenuTop) {
                    case MenuTopEnum::S2C:
                        $menuTop = new S2C($this->additional);
                        $menuTop->generate($html, $this->router, $menuActive, $this->contenaire, $this->pdo);
                        break;
                
                    case MenuTopEnum::RelationFournisseurs:
                        $menuTop = new RelationFournisseur($this->additional);
                        $menuTop->generate($html, $this->router, $menuActive, $this->contenaire, $this->pdo);
                        break;
                
                    case MenuTopEnum::PilotageAchats:
                        $menuTop = new PilotageAchat($this->additional);
                        $menuTop->generate($html, $this->router, $menuActive, $this->contenaire, $this->pdo);
                        break;
                                    
                    default:
                        $menuTop = new P2P($this->additional);
                        $menuTop->generate($html, $this->router, $menuActive, $this->contenaire, $this->pdo);
                        break;
                }
                
                $menuTop = new GlobalApplication($this->additional);
                $menuTop->generate($html, $this->router, $menuActive, $this->contenaire, $this->pdo);
            }
        }
                
        $html .= "  </ul>
            </nav>
            ";
        
        
        if (isset($_SESSION['MODE_BACKUP']) && $_SESSION['MODE_BACKUP']==1) {
            $nom = (new Hastutil($this->pdo))->find($_SESSION["Session_Login"])['NOM']??'';
            
            $html.= "<div id='connexion_backup'>".$trad->t('Vous êtes en mode backup pour le compte de :')." <span class='gras'>".$nom."</span></div>";
        }

        return $html;
    }
}
