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

@Component
public class QuestionHandler {

    public QuestionDTO createQuestion(List<Nugget> nuggets, String questionType, String answerType) {
        Random random = new Random();
        QuestionDTO question = new QuestionDTO();
        List<String> alternatives = new ArrayList<>();

        if (nuggets.size() >= 4) {
            while (alternatives.size() < 5) {
                Nugget tempNugget = nuggets.remove(random.nextInt(nuggets.size()));
                //if first, use as a question
                if (alternatives.size() == 0) {
                    question.setQuestion(tempNugget.getFacts().stream().filter(f -> f.getType().equals(questionType))
                            .findFirst().orElse(null).getData());
                }
                alternatives.add(tempNugget.getFacts().stream().filter(f -> f.getType().equals(answerType))
                        .findFirst().orElse(null).getData());
            }
        } else {
            return null;
        }

        question.setCorrectAlternative(alternatives.get(0));
        question.setAlternative1(alternatives.get(1));
        question.setAlternative2(alternatives.get(2));
        question.setAlternative3(alternatives.get(3));

        return question;
    }
}

@@ -180,65 +180,9 @@
 til.
-ArrayList;%0Aimport java.util.List;%0Aimport java.util.Random
+*
 ;%0A%0A@
@@ -337,85 +337,168 @@
     
-Random random = new Random();%0A        QuestionDTO question = new QuestionDTO(
+List%3CNugget%3E shuffledList = new LinkedList%3C%3E(nuggets);%0A        Collections.shuffle(shuffledList);%0A        List%3CNugget%3E randomNuggets = shuffledList.subList(0, 5
 );%0A 
@@ -551,25 +551,24 @@
 st%3C%3E();%0A
-%0A
         
 if (nugg
@@ -563,219 +563,51 @@
     
-if (nuggets.size() %3E= 4) %7B%0A            while (alternatives.size() %3C 5) %7B%0A                Nugget tempNugget = nuggets.remove(random.nextInt(nuggets.size()));%0A                //if first, use as a question%0A        
+QuestionDTO question = new QuestionDTO();%0A%0A
     
@@ -614,27 +614,22 @@
     if (
-alternative
+nugget
 s.size()
@@ -633,12 +633,12 @@
 e() 
-== 0
+%3E= 4
 ) %7B%0A
@@ -649,24 +649,16 @@
         
-        
 question
@@ -670,26 +670,36 @@
 uestion(
-tempNugget
+randomNuggets.get(0)
 .getFact
@@ -726,32 +726,53 @@
 f -%3E f.getType()
+%0A                    
 .equals(question
@@ -781,37 +781,8 @@
 pe))
-%0A                            
 .fin
@@ -819,41 +819,47 @@
 ));%0A
-                %7D%0A               
+%0A            randomNuggets.forEach(n -%3E
  alt
@@ -876,18 +876,9 @@
 add(
-tempNugget
+n
 .get
@@ -921,28 +921,8 @@
 pe()
-.equals(answerType))
 %0A   
@@ -938,20 +938,36 @@
         
-    
+.equals(answerType))
 .findFir
@@ -998,77 +998,16 @@
 a())
+)
 ;%0A
+%0A
     
-        %7D%0A        %7D else %7B%0A            return null;%0A        %7D%0A%0A
     
@@ -1063,32 +1063,36 @@
 et(0));%0A        
+    
 question.setAlte
@@ -1122,32 +1122,36 @@
 et(1));%0A        
+    
 question.setAlte
@@ -1177,24 +1177,28 @@
 es.get(2));%0A
+    
         ques
@@ -1239,16 +1239,68 @@
 get(3));
+%0A        %7D else %7B%0A            return null;%0A        %7D
 %0A%0A      
@@ -1325,9 +1325,8 @@
 %0A    %7D%0A%7D
-%0A

Clean code