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.HashMap;
import java.util.List;

@Component
public class QuizHandler {

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

    public final String IA_INCORRECT_ANSWERS = "incorrectAnswer";
    public final 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 = this.convertQuizNugget(quizNugget);
            myQuizNuggets.add(myQuizNugget);
        }
        return myQuizNuggets;
    }

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

        return myQuizNugget;
    }

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

        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(this.convertIncorrectAnswer(incorrectAnswer));

        return myIncorrectAnswers;
    }

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

        return myIncorrectAnswer;
    }
}

@@ -639,32 +639,39 @@
    public final 
+static 
 String QN_ID = %22
@@ -684,32 +684,39 @@
    public final 
+static 
 String QN_QUIZ_R
@@ -740,32 +740,39 @@
    public final 
+static 
 String QN_QUESTI
@@ -797,32 +797,39 @@
    public final 
+static 
 String QN_CORREC
@@ -869,24 +869,31 @@
 ublic final 
+static 
 String QN_IN
@@ -940,32 +940,39 @@
    public final 
+static 
 String IA_INCORR
@@ -1021,16 +1021,23 @@
 c final 
+static 
 String I
@@ -1590,37 +1590,32 @@
  myQuizNugget = 
-this.
 convertQuizNugge
@@ -1838,21 +1838,16 @@
 ugget = 
-this.
 convertQ
@@ -1856,21 +1856,16 @@
 zNugget(
-this.
 quizNugg
@@ -1918,37 +1918,32 @@
 yQuizNugget.put(
-this.
 QN_INCORRECT_ANS
@@ -1940,37 +1940,32 @@
 ORRECT_ANSWERS, 
-this.
 getIncorrectAnsw
@@ -2184,29 +2184,24 @@
 zNugget.put(
-this.
 QN_ID, quizN
@@ -2237,29 +2237,24 @@
 zNugget.put(
-this.
 QN_QUIZ_REF,
@@ -2310,21 +2310,16 @@
 get.put(
-this.
 QN_QUEST
@@ -2375,21 +2375,16 @@
 get.put(
-this.
 QN_CORRE
@@ -2451,21 +2451,16 @@
 get.put(
-this.
 QN_INCOR
@@ -2473,21 +2473,16 @@
 NSWERS, 
-this.
 getIncor
@@ -2924,21 +2924,16 @@
 ers.add(
-this.
 convertI
@@ -3200,29 +3200,24 @@
 tAnswer.put(
-this.
 IA_ID, incor
@@ -3272,13 +3272,8 @@
 put(
-this.
 IA_I

remove this keyword and make constants static