<?php
namespace Controleur;
use Model\CommentaryModel;
use Gateway\CommentaryGateway;
use Model\UserModel;
use Gateway\UserGateway;
use Model\QuoteModel;
use Gateway\QuoteGateway;

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 UserControler {

    private CommentaryModel $cMod;
    private UserModel $uMod;
    private QuoteModel $qMod;
    private QuestionModel $mdl;

    public function __construct() {
        global $co;
        $this->cMod = new CommentaryModel(new CommentaryGateway($co));
        $this->uMod = new UserModel(new UserGateway($co));
        $this->qMod = new QuoteModel(new QuoteGateway($co));
        $this -> mdl = new QuestionModel(new QuestionGateway($co));
    }
    
    public function profil() {

        global $vues;
        $p = $this->uMod->getUsername($_SESSION["user"]);
        require_once $vues['profil'];

    }


    public function addComment(){
        $id = $_POST['idQuote'];
        $this->cMod->createComment($_POST['content'],$_POST['idQuote'],$this->uMod->getIdByUsername($_SESSION['user']));
        header("Location: /~kekentin/WF/WF-Website/quote/$id");
    }
    

    public function favorite(array $args) {
        global $vues;
        
        $userId = $this->uMod->getIdByUsername($_SESSION["user"]);

        $favorites = $this->qMod->getFavorites($userId);

        require_once $vues['favorite'];
    }

    public function unlog(){
        session_unset();
        session_destroy();
        $_SESSION = array();
        header("Location: /~kekentin/WF/WF-Website/");
    }

    public function quiz(array $args){
        global $vues;
        $id=$args['id'];
        $nb_questions = $this->getNumberOfQuestion($id);

        $action = $_REQUEST['action'] ?? null;
        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: /~kekentin/WF/WF-Website/quiz/$id_quiz");

    }

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

        $gw = new QuizGateway($co);
        $mdl = new QuizModel($gw);

        if ($mdl->getQuiz($id_quiz + 1)){
            require_once $vues['endQuiz'];
        }
        require_once $vues['endQuiz'];
    }

    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
    {
        global $co;
        $gw = new QuizQuestionGateway($co);
        $mdl = new QuizQuestionModel($gw);
        return $mdl->getAllQuestionByQuiz($id, $co);
    }

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

        //echo $twig->render('quiz.html.twig', ['question' => $question,'id'=>$idquestion]);
    }


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