<?php

//require 'usages/AltoRouter.php';

/*class FrontController
{
    
    function __construct()
    {
        global $dsn, $user, $pass, $vues;
        $listeAction_Acceuil = array('goToSolo', 'goToMulti', 'goToAdmin');
        try {
            $action = "";
            
            if (isset($_REQUEST['action'])) {
                $action = $_REQUEST['action'];
            }
            
            if (in_array($action, $listeAction_Acceuil)) {
                
            } 
        } catch (Exception $e) {
            header("Location:" . $vues["erreur"]);
        }
    }
}*/

class FrontController
{
    function __construct()
    {
        global $dsn, $user, $pass, $vues;
        $listeAction_Acceuil = array('goToSolo', 'goToMulti', 'goToAdmin');
        
        try {
            $action = "";
            if (isset($_REQUEST['action'])) {
                $action = $_REQUEST['action'];
            }

            // Initialize AltoRouter
            $router = new AltoRouter();

            //$router->setBasePath('/');

            // Define routes
            $router->map('GET', '/', 'ControllerAccueil'); // Route pour la page d'accueil
            $router->map('GET', '/solo', 'ControllerSolo');
            $router->map('GET', '/multi', 'ControllerMulti');
            $router->map('GET', '/admin', 'ControllerAdminAdministrators');
            $router->map('GET', '/login', 'ControllerLoginAdmin');
            $router->map('POST', '/login/[a:action]', 'ControllerLoginAdmin');
            $router->map('GET', '/admin/chapters', 'ControllerAdminChapters');
            $router->map('POST', '/admin/chapters/[a:action]', 'ControllerAdminChapters');
            $router->map('GET', '/admin/chapters/[a:action]/[i:id]', 'ControllerAdminChapters');
            $router->map('GET', '/admin/administrators', 'ControllerAdminAdministrators');
            $router->map('POST', '/admin/administrators/[a:action]', 'ControllerAdminAdministrators');
            $router->map('GET', '/admin/administrators/[a:action]/[i:id]', 'ControllerAdminAdministrators');
            $router->map('GET', '/admin/questions', 'ControllerAdminQuestions');
            $router->map('POST', '/admin/questions/[a:action]', 'ControllerAdminQuestions');
            $router->map('GET', '/admin/questions/[a:action]/[i:id]', 'ControllerAdminQuestions');


            // Match the current request
            $match = $router->match();

            if (!$match) {
                echo "404"; // Redirige vers une page d'erreur 404
                die;
            }

            if ($match) {
                $controller = $match['target'];
                $action=$match['params']['action'];
                $id=$match['params']['id'];

                $controller	= new $controller;

                if	(is_callable(array($controller,	$action)))	{
                    call_user_func_array(array($controller,	$action), array($match['params']));	
                }
            }
        }
        catch (Exception $e) {
            header("Location:" . $vues["erreur"]);
        }
    }
}
