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.*;
import java.util.stream.Collectors;

@Component
public class QuestionHandler {

    public QuestionDTO getQuestion(List<Nugget> nuggets, String questionType, String answerType) {
        Random random = new Random();
        List<Nugget> notHiddenNuggets = nuggets.stream().filter(n -> !n.isHidden()).collect(Collectors.toList());
        Nugget nugget = notHiddenNuggets.get(random.nextInt(notHiddenNuggets.size()));
        return createQuestion(nugget, notHiddenNuggets, questionType, answerType);
    }

    public List<QuestionDTO> getQuestions(List<Nugget> nuggets, String questionType, String answerType) {
        List<Nugget> notHiddenNuggets = nuggets.stream().filter(n -> !n.isHidden()).collect(Collectors.toList());
        List<QuestionDTO> questions = notHiddenNuggets.stream()
                .map(n -> createQuestion(n, notHiddenNuggets, questionType, answerType))
                .collect(Collectors.toList());
        Collections.shuffle(questions);
        return questions;
    }

    protected QuestionDTO createQuestion(Nugget nugget, List<Nugget> nuggets, String questionType, String answerType) {
        if (nuggets.size() >= 4) {
            List<Nugget> shuffledNuggets = new LinkedList<>(nuggets);
            Collections.shuffle(shuffledNuggets);
            shuffledNuggets.remove(nugget);
            QuestionDTO question = new QuestionDTO();
            List<String> alternatives = new ArrayList<>();
            alternatives.add(nugget.getFacts().stream().filter(f -> f.getType()
                    .equals(answerType)).findFirst().orElse(null).getData());
            
            //Avoid getting the same alternative from another nugget
            while (alternatives.size() < 4) {
                Nugget tempNugget = shuffledNuggets.remove(0);
                String tempAlternative = tempNugget.getFacts().stream().filter(f -> f.getType().equals(answerType))
                        .findFirst().orElse(null).getData();
                if (!alternatives.contains(tempAlternative)) {
                    alternatives.add(tempAlternative);
                }
            }
            question.setQuestion(nugget.getFacts().stream().filter(f -> f.getType()
                    .equals(questionType)).findFirst().orElse(null).getData());
            question.setCorrectAlternative(alternatives.get(0));
            question.setAlternative1(alternatives.get(1));
            question.setAlternative2(alternatives.get(2));
            question.setAlternative3(alternatives.get(3));
            return question;
        } else {
            return null;
        }
    }
}

@@ -1304,47 +1304,8 @@
 ) %7B%0A
-        if (nuggets.size() %3E= 4) %7B%0A    
     
@@ -1370,28 +1370,24 @@
 s);%0A        
-   
- 
 Collections.
@@ -1420,20 +1420,16 @@
         
-    
 shuffled
@@ -1460,20 +1460,16 @@
         
- 
-   
 Question
@@ -1506,28 +1506,24 @@
 ();%0A        
-   
- 
 List%3CString%3E
@@ -1553,28 +1553,24 @@
 ayList%3C%3E();%0A
-    
         alte
@@ -1641,36 +1641,32 @@
                 
- 
-   
 .equals(answerTy
@@ -1678,35 +1678,28 @@
 findFirst().
-orElse(null
+get(
 ).getData())
@@ -1704,25 +1704,9 @@
 ));%0A
-            %0A    
+%0A
     
@@ -1774,20 +1774,16 @@
         
-    
 while (a
@@ -1804,24 +1804,50 @@
 ze() %3C 4
+ && !shuffledNuggets.isEmpty()
 ) %7B%0A
-    
         
@@ -1897,20 +1897,16 @@
 ove(0);%0A
-    
         
@@ -2029,20 +2029,16 @@
         
-    
 .findFir
@@ -2038,35 +2038,28 @@
 findFirst().
-orElse(null
+get(
 ).getData();
@@ -2055,28 +2055,24 @@
 .getData();%0A
-    
             
@@ -2130,28 +2130,24 @@
             
-   
- 
 alternatives
@@ -2169,32 +2169,38 @@
 ative);%0A        
+    %7D%0A
         %7D%0A      
@@ -2193,37 +2193,63 @@
       %7D%0A        
-    %7D
+if (alternatives.size() == 4) %7B
 %0A            que
@@ -2376,19 +2376,12 @@
 t().
-orElse(null
+get(
 ).ge

Fix for if there is not enough nuggets to create a question