<?php

namespace Model;
use Gateway\QuestionGateway;
use Entity\QuestionEntity;
use Gateway\Gateway;

class QuestionModel extends Model
{
    public function createQuestion(int $id_question, string $question, string $answerA, string $answerB, string $answerC, string $answerD, string $cAnswer): bool
    {
        return $this -> gateway -> create($id_question, $question, $answerA, $answerB, $answerC, $answerD, $cAnswer);

    }

    public function getQuestion(int $id_question) : ?QuestionEntity
    {
        $q = $this -> gateway -> findById($id_question);
        if ($q)
            return new QuestionEntity(
                $q[0]['id_question'],
                $q[0]['texte'],
                $q[0]['answera'],
                $q[0]['answerb'],
                $q[0]['answerc'],
                $q[0]['answerd'],
                $q[0]['canswer']
            );
        return null;
    }

    public function updateTextQuestion(int $id_question, string $question) : bool
    {
        return $this -> gateway -> updateText($id_question, $question);
    }

    public function updateAnswersQuestion(int $id_question, string $answerA, string $answerB, string $answerC, string $answerD, string $cAnswer): bool
    {
        return $this -> gateway -> updateAnswers($id_question, $answerA, $answerB, $answerC, $answerD, $cAnswer);
    }

    public function deleteQuestion(int $id_question) : bool
    {
        return $this -> gateway -> delete($id_question);
    }

    public function getAllQuestions() : array
    {
        $q = $this -> gateway -> findAll();

        $questions = [];

        foreach ($q as $question) {
            $questions[] = new QuestionEntity(
                $question['id_question'],
                $question['question'],
                $question['answerA'],
                $question['answerB'],
                $question['answerC'],
                $question['answerD'],
                $question['cAnswer']
            );
        }
        return $questions;
    }

    public function getRdmQuestion() : ?QuestionEntity
    {
        $q = $this -> gateway -> findRdmQuestion();
        if ($q)
            return new QuestionEntity(
                $q['id_question'],
                $q['question'],
                $q['answerA'],
                $q['answerB'],
                $q['answerC'],
                $q['answerD'],
                $q['cAnswer']
            );
        return null;
    }

    public function getRandomQuestion(string $quesType, string $repType, string $langue) : ?QuestionEntity{
        switch([$questType,$repType]){
            case ["Quote","Src"]:
                $q = $this -> gateway -> getQuestionQuoteSrc($langue);
                return new QuestionEntity(
                    $q[], //idQuote
                    $q[], //Quote
                    $q[], //SrcJuste
                    $q[], //rndSrc 1
                    $q[], //rndSrc 2
                    $q[], //rndSrc 3
                    $q[], //SrcJuste
                );
            case ["Quote","Charac"]:
                $q = $this -> gateway -> getQuestionQuoteCharac($langue);

            case ["Charac","Src"]:
                $q = $this -> gateway -> getQuestionCharacSrc($langue);

            case ["Charac","Quote"]:
                $q = $this -> gateway -> getQuestionCharacQuote($langue);

            case ["Src","Quote"]:
                $q = $this -> gateway -> getQuestionSrcQuote($langue);

            case ["Src","Charac"]:
                $q = $this -> gateway -> getQuestionSrcCharac($langue);

            case ["Test","Test"]:
                return new QuestionEntity(-1,"1","1","2","3","4","1");
            default :
                return null;
        }
    }
    
    
    /*--------------------------------------- Quiz Aléatoire ---------------------------------------*/
    
    /*----------------------- Question compléte -----------------------*/

    public function QuestionForQuoteToCarac(string $lang) : QuestionEntity
    {
        $questRep = $this->gateway->QuestionToReponceForQuoteToCarac($lang);


        $quest = $questRep[0]['question'];
        $cAnswer = $questRep[0]['reponce'];

        $fake = $this->gateway->FakeReponceForQuoteToCarac($lang,$cAnswer);

        $fakeRep = [$cAnswer];
        foreach ($fake as $rep){
            $fakeRep[]=$rep['reponce'];
        }
        shuffle($fakeRep);


        return new QuestionEntity(-1,$quest,$fakeRep[0],$fakeRep[1],$fakeRep[2],$fakeRep[3],$cAnswer);
    }

    public function QuestionForQuoteToTitle(string $lang) : QuestionEntity
    {
        $questRep = $this->gateway->QuestionToReponceForQuoteToTitle($lang);

        $quest = $questRep['question'];
        $cAnswer = $questRep['reponce'];

        $fake = $this->gateway->FakeReponceForQuoteToTitle($lang,$cAnswer);

        $fakeRep = [$cAnswer];
        foreach ($fake as $rep){
            $fakeRep[]=$rep['reponce'];
        }
        $fakeRep=array_rand($fakeRep);

        return new QuestionEntity(-1,$quest,$fakeRep[0],$fakeRep[1],$fakeRep[2],$fakeRep[3],$cAnswer);
    }
}