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

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

import java.util.List;
import java.util.Optional;

/**
 * Precomuted answers to sentences not understood by chatbot.
 */
public class RandomAnswer {

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

  /** This array contains costants added to the answer when the input is made by a single word. */
  private static String[] costants;

  /** This array contains answers to a not-english sentence. */
  private static String[] englishAnswers;

  /** This array contains answers made for too much general/not understood sentence. */
  private static String[] generalAnswers;

  /**
   * Gives a general answer to a general user input.
   * @param phrase given in input
   * @return general answer
   */
  public static BrainResponse getAnswer(Phrase phrase) {
    if (costants == null) {
      costants = new String[] {
          "?",
          "? I do not understand",
          ".. that is cool"
      };
      englishAnswers = new String[] {
          "I think you should speak english",
          "PLEASE, speak english!",
          "Are you a robot too?"
      };
      generalAnswers = new String[] {
          "What a nice day to talk to a chatbot!",
          "Can you reformulate your sentence please?",
          "Tell me something interesting please."
      };
    }

    List<Token> tokenList = phrase.getTokens();

    if (tokenList.size() == 1) {
      return new BrainResponse(phrase.getText() + costants[randomIndex(costants)], "");
    } else {
      long count = tokenList.stream().map(Token::isKnown).filter(x -> !x).count();
      if (count > tokenList.size() / 2) {
        return new BrainResponse(englishAnswers[randomIndex(englishAnswers)], "");
      }
    }
    return new BrainResponse(generalAnswers[randomIndex(generalAnswers)], "");
  }

  /**
   * Returns a random index of the array given in input.
   * @param array whose index is needed
   * @return index
   */
  private static int randomIndex(String[] array) {
    return (int)(Math.random() * array.length);
  }
}

@@ -204,14 +204,23 @@
 %0A * 
-P
+Handles p
 recom
+p
 uted
@@ -259,16 +259,20 @@
 tood by 
+the 
 chatbot.
@@ -320,83 +320,9 @@
   * 
-Private constructor.%0A   */%0A  private RandomAnswer() %7B %7D%0A%0A  /** This array c
+C
 onta
@@ -327,16 +327,17 @@
 tains co
+n
 stants a
@@ -395,16 +395,19 @@
 le word.
+%0A  
  */%0A  pr
@@ -411,32 +411,38 @@
  private static 
+final 
 String%5B%5D costant
@@ -438,37 +438,106 @@
 g%5B%5D 
-costants;%0A%0A  /** This array c
+CONSTANTS = %7B%0A      %22?%22,%0A      %22? I do not understand%22,%0A      %22.. that is cool%22%0A  %7D;%0A%0A  /**%0A   * C
 onta
@@ -566,32 +566,35 @@
 nglish sentence.
+%0A  
  */%0A  private st
@@ -590,32 +590,38 @@
  private static 
+final 
 String%5B%5D english
@@ -617,43 +617,149 @@
 g%5B%5D 
-englishAnswers;%0A%0A  /** This array c
+ENGLISH_ANSWERS = %7B%0A      %22I think you should speak english%22,%0A      %22PLEASE, speak english!%22,%0A      %22Are you a robot too?%22%0A  %7D;%0A%0A  /**%0A   * C
 onta
@@ -820,16 +820,19 @@
 entence.
+%0A  
  */%0A  pr
@@ -844,16 +844,22 @@
  static 
+final 
 String%5B%5D
@@ -863,727 +863,468 @@
 g%5B%5D 
-generalAnswers;%0A%0A  /**%0A   * Gives a general answer to a general user input.%0A   * @param phrase given in input%0A   * @return general answer%0A   */%0A  public static BrainResponse getAnswer(Phrase phrase) %7B
+GENERAL_ANSWERS = %7B%0A      %22What a nice day to talk to a chatbot!%22,%0A      %22Can you reformulate your sentence please?%22,
 %0A    
-if (costants == null) %7B%0A      costants = new String%5B%5D %7B%0A          %22?%22,%0A          %22? I do not und
+  %22Tell me something int
 er
+e
 st
-and%22,%0A          %22.. that is cool%22%0A      %7D;%0A      englishAnswers = new String%5B%5D %7B%0A          %22I think you should speak english%22,%0A          %22PLEASE, speak english!%22,%0A          %22Are you a robot too?%22%0A      %7D;%0A      generalAnswers = new String%5B%5D %7B%0A          %22What a nice day to talk to a chatbot!%22,%0A          %22Can you reformulate your sentence please?%22,%0A          %22Tell me something interesting please.%22%0A      %7D;%0A    %7D%0A
+ing please.%22%0A  %7D;%0A%0A  /**%0A   * Private constructor.%0A   */%0A  private RandomAnswer() %7B %7D%0A%0A  /**%0A   * Gives a general answer to a general user input.%0A   * @param phrase given in input%0A   * @return a BrainResponse instance, containing a general answer%0A   */%0A  public static BrainResponse getAnswer(Phrase phrase) %7B
 %0A   
@@ -1452,24 +1452,25 @@
 ext() + 
-costants
+CONSTANTS
 %5BrandomI
@@ -1478,16 +1478,17 @@
 dex(
-costants
+CONSTANTS
 )%5D, 
@@ -1667,49 +1667,51 @@
 nse(
-englishAnswers%5BrandomIndex(englishAnswers
+ENGLISH_ANSWERS%5BrandomIndex(ENGLISH_ANSWERS
 )%5D, 
@@ -1762,49 +1762,51 @@
 nse(
-generalAnswers%5BrandomIndex(generalAnswers
+GENERAL_ANSWERS%5BrandomIndex(GENERAL_ANSWERS
 )%5D, 
@@ -1931,16 +1931,39 @@
 @return 
+the randomly generated 
 index%0A  

improve RandomAnswer javadoc and codestyle