package com.github.bot.curiosone.core.extraction;

import com.github.bot.curiosone.core.nlp.Phrase;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * Precomputed answers to some common bad words.
 */
public class BadWords {
  private static String badWordsPath = "/conversation/bad_words.txt";
  /** Path of the database containing known insults. */

  /** List of different insults that the bot knows. */
  private static List<String> knownBadWords;

  /** List of different answers that the bot gives in output. */
  private static String[] readyAnswers;

  /**
   * Private constructor.
   */
  private BadWords() { }

  /**
   * Loads the known bad words in memory.
   */
  private static void loadSentences() {
    readyAnswers = new String[] {
      "I do not like your way of talking.",
      "You should speak more politely.",
      "Why are you talking like this? Are you a little kid?",
      "Come back when you grow up."
    };
    knownBadWords = new ArrayList<>();
    Path path = null;

    try {
      URL resource = Conversation.class.getResource(badWordsPath);
      path = Paths.get(resource.toURI());
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }

    try (Stream<String> stream = Files.lines(path)) {
      stream.forEach(line -> knownBadWords.add(line));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * Check if the phrase contains one of the known bad words.
   * @param phrase to check
   * @return answer if the input is known
   */
  public static Optional<BrainResponse> getAnswer(Phrase phrase) {
    if (knownBadWords == null) {
      loadSentences();
    }
    int randpos = (int)(Math.random() * readyAnswers.length);
    boolean bad = phrase.getTokens().stream()
        .map(word -> word.getText())
        .anyMatch(word -> knownBadWords.contains(word));
    if (bad) {
      return Optional.of(new BrainResponse(readyAnswers[randpos], ""));
    }
    return Optional.empty();
  }
}

@@ -453,16 +453,66 @@
 dWords %7B
+%0A%0A  /**%0A   * Path to known insults database.%0A   */
 %0A  priva
@@ -578,78 +578,25 @@
 t%22;%0A
+%0A
   /**
- Path of the database containing known insults. */%0A%0A  /*
+%0A   
 * List
- of
+s
  dif
@@ -614,27 +614,28 @@
 lts 
-that
+known by
  the 
-bot knows.
+Bot.%0A  
  */%0A
@@ -685,21 +685,27 @@
 ;%0A%0A  /**
- List
+%0A   * Array
  of diff
@@ -747,16 +747,19 @@
  output.
+%0A  
  */%0A  pr
@@ -796,205 +796,13 @@
 wers
-;%0A%0A  /**%0A   * Private constructor.%0A   */%0A  private BadWords() %7B %7D%0A%0A  /**%0A   * Loads the known bad words in memory.%0A   */%0A  private static void loadSentences() %7B%0A    readyAnswers = new String%5B%5D %7B%0A  
+ = %7B%0A
     
@@ -835,26 +835,24 @@
  talking.%22,%0A
-  
     %22You sho
@@ -882,18 +882,16 @@
 .%22,%0A    
-  
 %22Why are
@@ -942,18 +942,16 @@
 ?%22,%0A    
-  
 %22Come ba
@@ -978,12 +978,169 @@
 %22%0A  
-  
 %7D;
+%0A%0A  /**%0A   * Private constructor.%0A   */%0A  private BadWords() %7B %7D%0A%0A  /**%0A   * Loads the known bad words in memory.%0A   */%0A  private static void loadSentences() %7B
 %0A   
@@ -1580,134 +1580,317 @@
   * 
-Check if the phrase contains one of the known bad words.%0A   * @param phrase to check%0A   * @return answer if the input is known
+Returns an answer for the given phrase.%0A   * @param phrase the original phrase to be answered%0A   * @return an Optional instance.%0A   *         The returned instance is empty, if no bad word or insult has been found in the original%0A   *         Phrase, contains a value (an instance of BrainResponse) otherwise.
 %0A   

improve BadWords code and javadoc