package se.kits.gakusei.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thymeleaf.util.ArrayUtils;
import se.kits.gakusei.content.model.IncorrectAnswers;
import se.kits.gakusei.content.model.Quiz;
import se.kits.gakusei.content.model.QuizNugget;
import se.kits.gakusei.content.repository.QuizRepository;
import se.kits.gakusei.content.repository.QuizNuggetRepository;
import se.kits.gakusei.content.repository.IncorrectAnswerRepository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@Component
public class QuizHandler {

    public final static String QN_ID = "id";
    public final static String QN_QUIZ_REF = "quizRef";
    public final static String QN_QUESTION = "question";
    public final static String QN_CORRECT_ANSWER = "correctAnswer";
    public final static String QN_INCORRECT_ANSWERS = "incorrectAnswers";

    public final static String QN_GA_CORRECT = "correctAlternative";
    public final static String QN_GA_ALTERNATIVE1 = "alternative1";
    public final static String QN_GA_ALTERNATIVE2 = "alternative2";
    public final static String QN_GA_ALTERNATIVE3 = "alternative3";

    public final static String IA_INCORRECT_ANSWERS = "incorrectAnswer";
    public final static String IA_ID = "id";

    @Autowired
    protected QuizRepository quizRepository;

    @Autowired
    protected QuizNuggetRepository quizNuggetRepository;

    @Autowired
    protected IncorrectAnswerRepository incorrectAnswerRepository;

    public List<HashMap<String, Object>> getQuizNuggets(Long quizId) {
        List<QuizNugget> quizNuggets = quizNuggetRepository.findByQuiz_Id(quizId);
        List<HashMap<String, Object>> myQuizNuggets = new ArrayList<>();

        for (QuizNugget quizNugget : quizNuggets) {
            HashMap<String, Object> myQuizNugget = convertQuizNugget(quizNugget);
            myQuizNuggets.add(myQuizNugget);
        }
        return myQuizNuggets;
    }

    public List<HashMap<String, Object>> getQuizNuggetsForGakusei(Long quizId) {
        List<QuizNugget> quizNuggets = quizNuggetRepository.findByQuiz_Id(quizId);
        List<HashMap<String, Object>> myQuizNuggets = new ArrayList<>();

        for(QuizNugget qn : quizNuggets){
            HashMap<String, Object> myQuizNugget = convertQuizNuggetForGakusei(qn);
            myQuizNuggets.add(myQuizNugget);
        }

        return myQuizNuggets;
    }

    public HashMap<String, Object> getQuizNugget(Long quizNuggetId) {
        HashMap<String, Object> myQuizNugget = convertQuizNugget(quizNuggetRepository.findOne(quizNuggetId));
        myQuizNugget.put(QN_INCORRECT_ANSWERS, getIncorrectAnswers(quizNuggetId));

        return myQuizNugget;
    }

    public HashMap<String, Object> convertQuizNugget(QuizNugget quizNugget) {
        HashMap<String, Object> myQuizNugget = new HashMap<>();
        myQuizNugget.put(QN_ID, quizNugget.getId());
        myQuizNugget.put(QN_QUIZ_REF, quizNugget.getQuiz().getId());
        myQuizNugget.put(QN_QUESTION, quizNugget.getQuestion());
        myQuizNugget.put(QN_CORRECT_ANSWER, quizNugget.getCorrectAnswer());
        myQuizNugget.put(QN_INCORRECT_ANSWERS, getIncorrectAnswers(quizNugget.getId()));

        return myQuizNugget;
    }

    public HashMap<String, Object> convertQuizNuggetForGakusei(QuizNugget quizNugget){
        HashMap<String, Object> myQuizNugget = new HashMap<>();
        List<IncorrectAnswers> incorrectAnswers = incorrectAnswerRepository.getByQuizNugget_Id(quizNugget.getId());

        myQuizNugget.put(QN_QUESTION, Collections.singletonList(quizNugget.getQuestion()));
        myQuizNugget.put(QN_GA_CORRECT, Collections.singletonList(quizNugget.getCorrectAnswer()));
        myQuizNugget.put(QN_GA_ALTERNATIVE1, Collections.singletonList(incorrectAnswers.get(0).getIncorrectAnswer()));
        myQuizNugget.put(QN_GA_ALTERNATIVE2, Collections.singletonList(incorrectAnswers.get(1).getIncorrectAnswer()));
        myQuizNugget.put(QN_GA_ALTERNATIVE3, Collections.singletonList(incorrectAnswers.get(2).getIncorrectAnswer()));

        return myQuizNugget;
    }

    private List<HashMap<String, Object>> getIncorrectAnswers(Long quizNuggetId) {
        List<HashMap<String, Object>> myIncorrectAnswers = new ArrayList<>();
        List<IncorrectAnswers> incorrectAnswers = incorrectAnswerRepository.getByQuizNugget_Id(quizNuggetId);
        for (IncorrectAnswers incorrectAnswer : incorrectAnswers)
            myIncorrectAnswers.add(convertIncorrectAnswer(incorrectAnswer));

        return myIncorrectAnswers;
    }

    private HashMap<String, Object> convertIncorrectAnswer(IncorrectAnswers incorrectAnswers) {
        HashMap<String, Object> myIncorrectAnswer = new HashMap<>();
        myIncorrectAnswer.put(IA_ID, incorrectAnswers.getId());
        myIncorrectAnswer.put(IA_INCORRECT_ANSWERS, incorrectAnswers.getIncorrectAnswer());

        return myIncorrectAnswer;
    }
}

@@ -3506,33 +3506,39 @@
 orrectAnswers = 
-i
+selectI
 ncorrectAnswerRe
@@ -3539,37 +3539,9 @@
 swer
-Repository.getByQuizNugget_Id
+s
 (qui
@@ -4962,11 +4962,341 @@
 r;%0A    %7D
+%0A%0A    private List%3CIncorrectAnswers%3E selectIncorrectAnswers(Long quizNuggetId)%7B%0A        List%3CIncorrectAnswers%3E allIncorrectAnswers = incorrectAnswerRepository.getByQuizNugget_Id(quizNuggetId);%0A%0A        // Naive randomization%0A        Collections.shuffle(allIncorrectAnswers);%0A%0A        return allIncorrectAnswers.subList(0,3);%0A    %7D
 %0A%7D%0A

Implement method for picking out three incorrect answers from a larger set