<?php
namespace Controleur;

use Entity\QuestionEntity;
use Gateway\Connection;
use Gateway\QuestionGateway;
use Gateway\QuizGateway;
use Gateway\QuizQuestionGateway;
use Model\QuestionModel;
use Model\QuizModel;
use Model\QuizQuestionModel;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Loader\FilesystemLoader;

class QuizControler
{

    private Connection $co;

    private QuestionGateway $gw;

    private QuestionModel $mdl;


    /**
     * @throws SyntaxError
     * @throws RuntimeError
     * @throws LoaderError
     */
    public function __construct(Connection $co, ?string $action = null)
    {
        $this -> co = $co;
        $this -> gw = new QuestionGateway($this -> co);
        $this -> mdl = new QuestionModel($this -> gw);
        global $num;
        global $loader;

        $action = $_REQUEST['action'] ?? null;

        $id = (int) explode('/', $_SERVER['REQUEST_URI'])[2] ?? null;
        echo $id;
        $nb_questions = $this->getNumberOfQuestion($id);

        switch ($action) {
            case 'canswer':
                if ($this->CorrectAnswer())
                    $_SESSION['score'] = isset($_SESSION['score']) ? ($_SESSION['score'] + 1) : 1;
                $this->continueQuiz($id, $nb_questions);
                break;
            default:
                switch($id)
                {
                    case null:
                        // page erreur
                        break;
                    default:
                        $_SESSION['score'] = $_SESSION['score'] ?? 0;
                        $this->showQuestion($id, $_SESSION['no_question'] ?? 0);
                        break;
                }
        }

    }

    

}