<?php

class ControllerAdminQuestions
{
    private $gatewayQuestion;
    private $gatewayChapter;
    private $gatewayAnswer;

    private $twig;
    private $vues;

    function __construct()
    {
        global $dns, $user, $pass, $vues, $twig;
        session_start();
        try {
            if($_SESSION["idAdminConnected"] != null){
                $this->twig =$twig;
                $this->vues = $vues;

                $con = new Connection($dns, $user, $pass);

                $this->gatewayQuestion = new GatewayQuestion($con);
                $this->gatewayAnswer = new GatewayAnswer($con);
                $this->gatewayChapter = new GatewayChapter($con);

                $questions = $this->gatewayQuestion->getQuestions();
                $chapters = $this->gatewayChapter->getChapters();

                echo $twig->render($vues["adminQuestions"], [
                    'questions' => $questions,
                    'chapters' => $chapters,
                ]);
            }
            else {
                header("Location:/login");
            }
        } catch (PDOException $e) {
            // Gérez les erreurs PDO ici
        } catch (Exception $e2) {
            // Gérez d'autres erreurs ici
        }
    }

    function delete($param) {
        $this->gatewayQuestion->deleteQuestionByID($param["id"]);
        header("Location:/admin/questions");
    }

    function add($param) {
        $content = $_POST['content'];
        $idChapter = intval($_POST['idChapter']);
        $AnswersPost = array();
        $AnswersPost[1] = $_POST['answer1'];
        $AnswersPost[2] = $_POST['answer2'];
        $AnswersPost[3] = $_POST['answer3'];
        $AnswersPost[4] = $_POST['answer4'];
        $correctAnswer = intval($_POST['correctAnswer']);

        $Question = new Question($content,$idChapter);

        $questionId = intval($this->gatewayQuestion->addQuestion($Question));

        $Answers = array();
        $Answer[1] = new Answer($AnswersPost[1],$questionId);
        $Answer[2] = new Answer($AnswersPost[2],$questionId);
        $Answer[3] = new Answer($AnswersPost[3],$questionId);
        $Answer[4] = new Answer($AnswersPost[4],$questionId);

        $answersId = array();
        $answersId[1] = $this->gatewayAnswer->addAnswer($Answer[1]);
        $answersId[2] = $this->gatewayAnswer->addAnswer($Answer[2]);
        $answersId[3] = $this->gatewayAnswer->addAnswer($Answer[3]);
        $answersId[4] = $this->gatewayAnswer->addAnswer($Answer[4]);

        $Question = new Question($content,$idChapter,$answersId[$correctAnswer]);

        $this->gatewayQuestion->updateQuestion($questionId,$Question);
        
        header("Location:/admin/questions");
    }

    function updatemodal($param) {

        $question = $this->gatewayQuestion->getQuestionByID($param["id"]);

        $answers = array();
        $answers = $this->gatewayAnswer->getAnswersByIDQuestions($param["id"]);

        $chapters = $this->gatewayChapter->getChapters();

        echo $this->twig->render($this->vues["adminQuestionsModal"], [
            'question' => $question,
            'chapters' => $chapters,
            'answers' => $answers,
        ]);
    }

    function update($param) {

        $id = $_POST['id'];
        $content = $_POST['content'];
        $idChapter = intval($_POST['idChapter']);

        $correctAnswer = intval($_POST['correctAnswer']);

        $answersId = array();
        $answersId[1] = intval($_POST['IdAnswer1']);
        $answersId[2] = intval($_POST['IdAnswer2']);
        $answersId[3] = intval($_POST['IdAnswer3']);
        $answersId[4] = intval($_POST['IdAnswer4']);

        $answers = array();
        $answers[1] = $_POST['answer1'];
        $answers[2] = $_POST['answer2'];
        $answers[3] = $_POST['answer3'];
        $answers[4] = $_POST['answer4'];

        $Question = new Question($content, $idChapter, $answersId[$correctAnswer]);

        $this->gatewayQuestion->updateQuestion($id, $Question);

        $Answer1 = new Answer($answers[1],$id);
        $Answer2 = new Answer($answers[2],$id);
        $Answer3 = new Answer($answers[3],$id);
        $Answer4 = new Answer($answers[4],$id);

        $this->gatewayAnswer->updateAnswer($answersId[1],$Answer1);
        $this->gatewayAnswer->updateAnswer($answersId[2],$Answer2);
        $this->gatewayAnswer->updateAnswer($answersId[3],$Answer3);
        $this->gatewayAnswer->updateAnswer($answersId[4],$Answer4);

        header("Location:/admin/questions");
    }
}
