<?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;
        $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;
                }
        }

    }

    /**
     * @throws SyntaxError
     * @throws RuntimeError
     * @throws LoaderError
     */
    public function continueQuiz(int $id_quiz, int $total_questions) : void
    {
        $score = $_SESSION['score'];
        $_SESSION['no_question'] = isset($_SESSION['no_question']) ? ($_SESSION['no_question'] + 1) : 1;
        if ($_SESSION['no_question'] >= $total_questions) {
            session_destroy();
            $this->endQuiz($id_quiz, $score);
        }
        else header("Location: http://localhost:8000/quiz/$id_quiz");

    }

    /**
     * @throws SyntaxError
     * @throws RuntimeError
     * @throws LoaderError
     */
    public function endQuiz(int $id_quiz, int $score) : void
    {
        global $twig;

        $gw = new QuizGateway($this->co);
        $mdl = new QuizModel($gw);

        if ($mdl->getQuiz($id_quiz + 1))
            echo $twig->render('endQuiz.html.twig', ['score' => $score, 'nextquiz' => $id_quiz + 1]);
        echo $twig->render('endQuiz.html.twig', ['score' => $score]);
    }

    public function CorrectAnswer() : bool
    {
        $answera = $_POST['answera'] ?? null;
        $answerb = $_POST['answerb'] ?? null;
        $answerc = $_POST['answerc'] ?? null;
        $answerd = $_POST['answerd'] ?? null;

        $id= null;
        $answer = null;

        if ($answera) {
            $answer = explode('-', $answera)[0];
            $id = (int) explode('-', $answera)[1];
        } elseif ($answerb) {
            $answer = explode('-', $answerb)[0];
            $id = (int) explode('-', $answerb)[1];
        } elseif ($answerc) {
            $answer = explode('-', $answerc)[0];
            $id = (int) explode('-', $answerc)[1];
        } elseif ($answerd) {
            $answer = explode('-', $answerd)[0];
            $id = (int) explode('-', $answerd)[1];
        }

        $res = $this->mdl->getQuestion($id);
        return $answer == $res->getCanswer();
    }

    public function GetQuestion(int $id): array
    {
        $gw = new QuizQuestionGateway($this->co);
        $mdl = new QuizQuestionModel($gw);
        return $mdl->getAllQuestionByQuiz($id, $this->co);
    }

    /**
     * @throws RuntimeError
     * @throws SyntaxError
     * @throws LoaderError
     */
    public function showQuestion(int $id, int $num) : void
    {
        global $twig;
        $q = $this->GetQuestion($id);
        $question = $q[$num] ?? $q[0];
        $idquestion = $question->getIdQuestion();
        echo $twig->render('quiz.html.twig', ['question' => $question,'id'=>$idquestion]);
    }


    public function getNumberOfQuestion(int $id) : int
    {
        $gw = new QuizGateway($this->co);
        $mdl = new QuizModel($gw);
        return $mdl->getQuiz($id)->getNbQuestions();
    }

}