<?php
namespace Controleur;

use Gateway\Connection;

Class FrontControler{

    private $listAction;

    private Connection $co;

    /**
     * @throws \Exception
     */
    public function __construct($co){
        global $twig,$racine;

        $this->listAction = ['visitor' => array('accueil','search','quote','login','signin','validlogin','validsignin'),
                             'user' => array('quiz','randomQuiz','commentary','logout','addComment','favorite','profil','addFav','supFav','changedata', 'submit', 'validsubmit', 'add', 'accueilQuiz', 'deleteAccount'),
                             'admin' => array('null')];

        $dVueEreur = [];

        $router = new \AltoRouter();

        $router->setBasePath($racine); ///~kekentin/WF/WF-Website

        $router->map('GET', '/', 'VisitorControler','accueil');

        /*
        'i'  => '[0-9]++'
        'a'  => '[0-9A-Za-z]++'
        'h'  => '[0-9A-Fa-f]++'
        '*'  => '.+?'
        '**' => '.++'
        ''   => '[^/\.]++'
        */


        $router->map('GET|POST', '/quote/[i:idQuote]', 'VisitorControler','quote');
        $router->map('GET|POST', '/addComment', 'UserControler','addComment');
        $router->map('GET|POST', '/quiz/[i:id]', 'UserControler','quiz');
        $router->map('GET|POST', '/favorite', 'UserControler','favorite');
        $router->map('GET|POST', '/search', 'VisitorControler','search');
        $router->map('GET|POST', '/profil', 'UserControler','profil');
        $router->map('GET|POST', '/login', 'VisitorControler','login');
        $router->map('GET|POST', '/unlog', 'UserControler','unlog');
        $router->map('GET|POST', '/signin', 'VisitorControler','signin');
        $router->map('GET|POST', '/validlogin', 'VisitorControler','validlogin');
	    $router->map('GET|POST', '/validsignin', 'VisitorControler','validsignin');
        $router->map('GET|POST', '/addFav/[i:id]', 'UserControler','addFav');
        $router->map('GET|POST', '/supFav/[i:id]', 'UserControler','supFav');
        $router->map('GET|POST', '/changedata', 'UserControler','changedata');
        $router->map('GET|POST', '/submit', 'UserControler', 'submit');
        $router->map('GET|POST', '/validsubmit', 'UserControler','validsubmit');
        $router->map('GET|POST', '/add', 'UserControler', 'add');
        $router->map('GET|POST', '/accueilQuiz', 'UserControler', 'accueilQuiz');
        $router->map('GET|POST', '/error', 'VisitorControler','error');
        $router->map('GET|POST', '/randomQuiz', 'UserControler','randomQuiz');
        $router->map('GET|POST', '/deleteAccount', 'UserControler', 'deleteAccount');



        $match = $router->match();
        $action = NULL;

        if(!$match){
            $dVueEreur = [404,"Page non trouvée"];
            $this->vueErreur($dVueEreur);
        }
        else{
            $controller=$match['target'] ?? null;

            $action = $match['name'];
            
            //Si existe, on l’appelle
            if(!$this->ifExisteAction($action)){
                $dVueEreur = [404,"Action non trouvée"];
                $this->vueErreur($dVueEreur);
            }

            else if(!$this->verifDroit($action)){
                global $racine;
                header("Location:". $racine ."/login");
                // $action='login';
                // $controller='VisitorControler';
            }

            $controller = '\\Controleur\\' . $controller;
            $controller = new $controller($co);
            if (is_callable(array($controller, $action))) {
                call_user_func_array(array($controller, $action),
                array($match['params'])); 
            
            }
    
        }


    }

    private function ifExisteAction(string $action):bool {
        if( in_array($action , $this->listAction['admin']) ||
            in_array($action , $this->listAction['user']) ||
            in_array($action , $this->listAction['visitor']) ) {
                return true;
            }
        
        return false;
    }

    private function verifDroit(string $action):bool {
        if( in_array($action , $this->listAction['admin']) && $_SESSION["role"] == 'admin') return true;
        elseif( in_array($action , $this->listAction['user']) && ($_SESSION["role"] == 'admin' || $_SESSION["role"] == 'user') ) return true;
        elseif(in_array($action , $this->listAction['visitor']) && ($_SESSION["role"] == 'admin'|| $_SESSION["role"] == 'user'|| $_SESSION["role"] == 'visitor')) return true;
        return false;
    }

    private function vueErreur(array $erreur){
        global $vues;
        require_once $vues['erreur'];
    }

}

