package se.kits.gakusei.util;

import org.springframework.stereotype.Component;
import se.kits.gakusei.content.model.Nugget;
import se.kits.gakusei.dto.QuestionDTO;

import java.util.*;

@Component
public class QuestionHandler {

    public QuestionDTO getQuestion(List<Nugget> nuggets, String questionType, String answerType) {
        Random random = new Random();
        Nugget nugget = nuggets.get(random.nextInt(nuggets.size()));
        return createQuestion(nugget, nuggets, questionType, answerType);
    }

    public List<QuestionDTO> getQuestions(List<Nugget> nuggets, String questionType, String answerType) {
        List<QuestionDTO> questions = new ArrayList<>();
        nuggets.forEach(n -> questions.add(createQuestion(n, nuggets, questionType, answerType)));
        Collections.shuffle(questions);
        return questions;
    }

    private QuestionDTO createQuestion(Nugget nugget, List<Nugget> nuggets, String questionType, String answerType) {
        List<Nugget> shuffledNuggets = new LinkedList<>(nuggets);
        Collections.shuffle(shuffledNuggets);
        shuffledNuggets.remove(nugget);
        List<Nugget> alternativesNuggets = shuffledNuggets.subList(0, 4);
        List<String> alternatives = new ArrayList<>();
        QuestionDTO question = new QuestionDTO();

        if (shuffledNuggets.size() >= 3) {
            question.setQuestion(nugget.getFacts().stream().filter(f -> f.getType()
                    .equals(questionType)).findFirst().orElse(null).getData());

            question.setCorrectAlternative(nugget.getFacts().stream().filter(f -> f.getType()
                    .equals(answerType)).findFirst().orElse(null).getData());

            alternativesNuggets.forEach(n -> alternatives.add(n.getFacts().stream().filter(f -> f.getType()
                    .equals(answerType)).findFirst().orElse(null).getData()));
            question.setAlternative1(alternatives.get(0));
            question.setAlternative2(alternatives.get(1));
            question.setAlternative3(alternatives.get(2));
            return question;
        } else {
            return null;
        }
    }
}
@@ -178,16 +178,52 @@
 .util.*;
+%0Aimport java.util.stream.Collectors;
 %0A%0A@Compo
@@ -691,33 +691,31 @@
 ions = n
-ew ArrayList%3C%3E
+uggets.stream
 ()
-;
 %0A       
@@ -719,43 +719,26 @@
     
-nuggets.forEach(n -%3E questions.add(
+        .map(n -%3E 
 crea
@@ -786,18 +786,144 @@
 erType))
-);
+%0A                .collect(Collectors.toList());%0A        if (questions.get(0) == null) %7B%0A            questions.clear();%0A        %7D
 %0A       
@@ -1098,32 +1098,71 @@
 g answerType) %7B%0A
+        if (nuggets.size() %3E= 4) %7B%0A    
         List%3CNug
@@ -1211,32 +1211,36 @@
 ggets);%0A        
+ 
+   
 Collections.shuf
@@ -1269,16 +1269,20 @@
         
+    
 shuffled
@@ -1305,32 +1305,36 @@
 ugget);%0A        
+  
+  
 List%3CNugget%3E alt
@@ -1387,24 +1387,28 @@
 4);%0A        
+    
 List%3CString%3E
@@ -1450,16 +1450,20 @@
         
+   
+ 
 Question
@@ -1501,51 +1501,8 @@
 );%0A%0A
-        if (shuffledNuggets.size() %3E= 3) %7B%0A
     

Fix bug for http status code