package com.nincraft.ninbot.components.trivia;

import lombok.val;
import net.dv8tion.jda.core.JDA;

import java.util.Random;
import java.util.TimerTask;

public class TriviaHintTask extends TimerTask {

    private TriviaInstance triviaInstance;
    private TriviaInstanceDao triviaInstanceDao;
    private JDA jda;
    private int hintNumber;
    private Random random;

    public TriviaHintTask(TriviaInstance triviaInstance, TriviaInstanceDao triviaInstanceDao, JDA jda, int hintNumber) {
        this.triviaInstance = triviaInstance;
        this.triviaInstanceDao = triviaInstanceDao;
        this.jda = jda;
        this.hintNumber = hintNumber;
        random = new Random();
    }

    @Override
    public void run() {
        if (triviaInstanceDao.isActiveTriviaChannel(triviaInstance.getChannelId())) {
            val answer = triviaInstance.getAnswer();
            String hint = "";
            if (hintNumber == 1) {
                hint = replaceAll(answer);
            } else if (hintNumber == 2) {
                hint = replaceSome(answer);
            }
            jda.getTextChannelById(triviaInstance.getChannelId()).sendMessage("Hint! " + hint).queue();
        }
    }

    private String replaceAll(String answer) {
        String newAnswer = "";
        for (char letter : answer.toCharArray()) {
            if (letter == ' ') {
                newAnswer += letter;
            } else {
                newAnswer += "-";
            }
        }
        return newAnswer;
    }

    private String replaceSome(String answer) {
        int count = 0;
        String newAnswer = "";
        for (char letter : answer.toCharArray()) {
            if ((letter == ' ' || random.nextBoolean()) && count < answer.length() / 2) {
                newAnswer += letter;
                count++;
            } else {
                newAnswer += "-";
            }
        }
        return newAnswer;
    }
}

@@ -115,52 +115,12 @@
 til.
-Random;%0Aimport java.util.TimerTask;%0A%0Apublic 
+*;%0A%0A
 clas
@@ -330,23 +330,16 @@
 m;%0A%0A    
-public 
 TriviaHi
@@ -845,189 +845,39 @@
 t = 
-%22%22;%0A            if (hintNumber == 1) %7B%0A                hint = replaceAll(answer);%0A            %7D else if (hintNumber == 2) %7B%0A                hint = replaceSome(answer);%0A            %7D
+revealHint(answer, hintNumber);
 %0A   
@@ -1019,16 +1019,16 @@
 g re
-placeAll
+vealHint
 (Str
@@ -1029,32 +1029,48 @@
 nt(String answer
+, int hintNumber
 ) %7B%0A        Stri
@@ -1063,32 +1063,39 @@
 %7B%0A        String
+Builder
  newAnswer = %22%22;
@@ -1095,92 +1095,468 @@
 r = 
-%22%22;%0A        for (char letter : answer.toCharArray()) %7B%0A            if (letter == ' '
+new StringBuilder();%0A        double percentOfForcedHint = ((hintNumber - 1) * 25) / 100.0;%0A        int numOfLettersToShow = (int) (answer.length() * percentOfForcedHint);%0A        List%3CInteger%3E hintIndexes = getHintIndexList(numOfLettersToShow, answer.length());%0A        char%5B%5D charArray = answer.toCharArray();%0A        for (int i = 0; i %3C charArray.length; i++) %7B%0A            char letter = charArray%5Bi%5D;%0A            if (letter == ' ' %7C%7C hintIndexes.contains(i)
 ) %7B%0A
@@ -1576,34 +1576,39 @@
    newAnswer
- += 
+.append(
 letter
+)
 ;%0A          
@@ -1635,39 +1635,44 @@
        newAnswer
- += 
+.append(
 %22-%22
+)
 ;%0A            %7D%0A
@@ -1697,32 +1697,43 @@
 return newAnswer
+.toString()
 ;%0A    %7D%0A%0A    pri
@@ -1741,238 +1741,236 @@
 ate 
-String replaceSome(String answer) %7B%0A        int count = 0;%0A        String newAnswer = %22%22;%0A        for (char letter : answer.toCharArray()) %7B%0A            if ((letter == ' ' %7C%7C random.nextBoolean()) && count %3C answer.length() / 2) %7B
+List%3CInteger%3E getHintIndexList(int numOfLettersToShow, int length) %7B%0A        Set%3CInteger%3E hintIndexes = new HashSet%3C%3E();%0A        for (int i = 0; i %3C numOfLettersToShow; i++) %7B%0A            hintIndexes.add(random.nextInt(length));
 %0A   
@@ -1970,32 +1970,34 @@
 ));%0A        
+%7D%0A
         
 newAnswer +=
@@ -1988,157 +1988,43 @@
     
-newAnswer += letter;%0A                count++;%0A            %7D else %7B%0A                newAnswer += %22-%22;%0A            %7D%0A        %7D%0A        return newAnswer
+return new ArrayList%3C%3E(hintIndexes)
 ;%0A  

Fix trivia hints