package com.grayben.riskExtractor.helpers;

import com.grayben.riskExtractor.htmlScorer.ScoredText;
import com.grayben.riskExtractor.htmlScorer.ScoredTextElement;
import com.grayben.riskExtractor.htmlScorer.partScorers.Scorer;
import com.grayben.riskExtractor.htmlScorer.partScorers.tagScorers.MapScorer;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;

import java.util.*;

/**
 * Created by beng on 23/04/2016.
 */
public class ScoredTextGenerator {

    public static Pair<ScoredText, String> randomPairedScoredTextAndHtml(Random random, Set<Scorer<Element>> elementScorers) {
        Set<MapScorer<Tag>> tagScorers = ScoredTextReverseEngineerer.equivalentScorersFrom(elementScorers);
        List<MapScorer<Tag>> scorerList = new ArrayList<>();
        scorerList.addAll(tagScorers);

        int numScorers = scorerList.size();

        ScoredText scoredText = new ScoredText();
        StringBuilder htmlBuilder = new StringBuilder();
        for(int i = 0; i < random.nextInt(); i++){
            List<Tag> tagsToApply = new ArrayList<>();

            int maxNumScorersToUse = random.nextInt() % (numScorers + 1);
            List<MapScorer<Tag>> scorersCopy = new ArrayList<>(scorerList);

            for(int j = 0; j < maxNumScorersToUse; j++){
                if(random.nextInt() % 2 > 0){
                    int scorersCopySize = scorersCopy.size();
                    MapScorer<Tag> selectedScorer = scorersCopy.remove(random.nextInt() % scorersCopySize);
                    List<Tag> tagList = new ArrayList<>();
                    tagList.addAll(selectedScorer.getScoresMap().keySet());
                    tagsToApply.add(tagList.get(random.nextInt() % tagList.size()));
                } else {
                    tagsToApply.add(Tag.valueOf("foobarbaz"));
                }

            }
            Iterator<Tag> tagIterator = tagsToApply.iterator();
            String text = ((Integer)random.nextInt()).toString();
            Element element = new Element(tagIterator.next(), "").text(text);
            while(tagIterator.hasNext()){
                Element parent = new Element(tagIterator.next(), "");
                parent.appendChild(element);
                element = parent;
            }

            htmlBuilder.append(element.html());

            ScoredTextElement scoredTextElement;
            Map<String, Integer> elementScores = new HashMap<>();
            for(Scorer<Element> scorer: elementScorers){
                elementScores.put(scorer.getScoreLabel(), scorer.score(element));
            }

            scoredTextElement = new ScoredTextElement(text, elementScores);
            scoredText.add(scoredTextElement);
        }
        return new ImmutablePair<>(scoredText, htmlBuilder.toString());
    }


    public static ScoredText randomScoredText(Random random, Set<Scorer<Element>> elementScorers){
        Set<MapScorer<Tag>> tagScorers = ScoredTextReverseEngineerer.equivalentScorersFrom(elementScorers);
        List<MapScorer<Tag>> scorerList = new ArrayList<>();
        scorerList.addAll(tagScorers);

        ScoredText scoredText = new ScoredText();

        for(int i = 0; i < random.nextInt(); i++){
            Map<String, Integer> textScores = new HashMap<>();

            for(MapScorer<Tag> tagMapScorer: scorerList){
                Map<Tag, Integer> scoreMap = tagMapScorer.getScoresMap();

                String label = tagMapScorer.getScoreLabel();
                Integer score = Scorer.DEFAULT_SCORE;

                if(random.nextInt() % 2 > 0){
                    List<Integer> scoreList = new ArrayList<>();
                    scoreList.addAll(scoreMap.values());
                    score = scoreList.get(random.nextInt() % scoreList.size());
                }
                textScores.put(label, score);

            }
            String text = ((Integer)random.nextInt()).toString();

            scoredText.add(new ScoredTextElement(text, textScores));
        }
        return scoredText;
    }
}

@@ -2860,16 +2860,156 @@
 %0A    %7D%0A%0A
+    public static int randomInt(Random random, int min, int max)%7B%0A        return min + (int)(random.nextFloat() * ((max - min) + 1));%0A    %7D%0A
 %0A    pub
@@ -3355,25 +3355,24 @@
 oredText();%0A
-%0A
         for(
@@ -3388,33 +3388,42 @@
 ; i %3C random
-.nextInt(
+Int(random, 1, 100
 ); i++)%7B%0A   
@@ -3753,33 +3753,42 @@
    if(random
-.nextInt(
+Int(random, 1, 100
 ) %25 2 %3E 0)%7B%0A
@@ -3953,33 +3953,60 @@
 t.get(random
-.nextInt(
+Int(random, 1, Integer.MAX_VALUE - 1
 ) %25 scoreLis
@@ -4133,33 +4133,60 @@
 teger)random
-.nextInt(
+Int(random, 1, Integer.MAX_VALUE - 1
 )).toString(

Use bounded-range random generator