<?php

namespace App\Classes\Modules\HelpMenu\Processors;

use App\Classes\Modules\HelpMenu\Services\FetchesHelpMenuQuestion;
use App\Classes\Modules\HelpMenu\Services\FetchesHelpMenuUserAnswerSelected;
use App\Classes\Modules\HelpMenu\Services\ListsHelpMenuUserAnswerSelected;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Classes\ValueObjects\Constants\QAType;

class FetchNextQuestionQAProcessor
{
    /** @var FetchesHelpMenuQuestion */
    private $fetchesHelpMenuQuestion;

    /** @var FetchesHelpMenuUserAnswerSelected */
    private $fetchesHelpMenuUserAnswerSelected;

    /** @var ListsHelpMenuUserAnswerSelected */
    private $listsHelpMenuUserAnswerSelected;
    /**
     * FetchNextQuestionQAProcessor constructor.
     * @param FetchesHelpMenuQuestion $fetchesHelpMenuQuestion
     * @param FetchesHelpMenuUserAnswerSelected $fetchesHelpMenuUserAnswerSelected
     * @param ListsHelpMenuUserAnswerSelected $listsHelpMenuUserAnswerSelected
     */
    public function __construct(FetchesHelpMenuQuestion $fetchesHelpMenuQuestion, FetchesHelpMenuUserAnswerSelected $fetchesHelpMenuUserAnswerSelected, ListsHelpMenuUserAnswerSelected $listsHelpMenuUserAnswerSelected)
    {
        $this->fetchesHelpMenuQuestion = $fetchesHelpMenuQuestion;
        $this->fetchesHelpMenuUserAnswerSelected = $fetchesHelpMenuUserAnswerSelected;
        $this->listsHelpMenuUserAnswerSelected = $listsHelpMenuUserAnswerSelected;
    }

    public function execute(Request $request){
        $isEnd = 0;
        $questionId = 0;
        $nextQuestionNumber = 0;
        $nextNestedQuestion = 0;
        $nextMainQuestion= 0;
        $questionnaireSetId = 0;
        $currentQuestion = $request->question;
        $questionType = QAType::DEFAULT;
        $returnQuestion = null;

        if ($currentQuestion && array_key_exists('id', $currentQuestion)) {
            $questionId = $currentQuestion['id'];
            $isEnd = $currentQuestion['is_end'];
            $questionType = $currentQuestion['question_type'];
            $nextNestedQuestion = $currentQuestion['next_nested_question'];
            $nextMainQuestion = $currentQuestion['next_main_question'];
            $questionnaireSetId = $currentQuestion['questionnaire_set_id'];
        }

        if ($request->has('answerObj')) {
            $nextQuestionNumber = $request->answerObj['next_question_number'];
        }

        if ($request->has('isPrevious')) {
            $userId = Auth::user()->id;
            $previousAnswer = $this->listsHelpMenuUserAnswerSelected->execute(['user_id' => $userId, 'order_by' => (object)['column' => 'id','DESC' => true]])[0];

            //Get previous question
            $previousQuestion = $this->fetchesHelpMenuQuestion->execute(['questionnaire_set_id' => $questionnaireSetId, 'id' => $previousAnswer['question_id']]);
            $returnQuestion = $previousQuestion;
        }
        else{
            //Get next question
            $nextQuestion = null;
            $question_number = 0;
            if($nextQuestionNumber != 0){
                $question_number = $nextQuestionNumber;
            }
            else if($nextNestedQuestion != 0){
                $question_number = $nextNestedQuestion;
            }
            else {
                $question_number = $nextMainQuestion;
            }

            if($isEnd){
                $returnQuestion = null;
            }
            else if($question_number != 0){
                $nextQuestion = $this->fetchesHelpMenuQuestion->execute(['questionnaire_set_id' => $questionnaireSetId, 'question_number' => $question_number]);
                $returnQuestion =  $nextQuestion;
            }
            else if (array_key_exists('id', $request->input('question'))) {
                $nextQuestion = $this->fetchesHelpMenuQuestion->execute(['questionnaire_set_id' => $questionnaireSetId, 'id_next' => $questionId, 'next_main_question' => null]);
                $returnQuestion = $nextQuestion;
            }
        }

        return $returnQuestion;
    }
}
