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

import com.github.bot.curiosone.core.knowledge.SemanticNetwork;
import com.github.bot.curiosone.core.knowledge.SemanticQuery;
import com.github.bot.curiosone.core.knowledge.SemanticRelationType;
import com.github.bot.curiosone.core.knowledge.interfaces.Edge;
import com.github.bot.curiosone.core.nlp.LEX;
import com.github.bot.curiosone.core.nlp.Meaning;
import com.github.bot.curiosone.core.nlp.POS;
import com.github.bot.curiosone.core.nlp.Sentence;
import com.github.bot.curiosone.core.nlp.Word;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Description.
 */
public class Affirmation {
  /**
   * getAnswer description.
   *
   * @param sentence [description]
   * @param scope [description]
   * @return [description]
   */
  public static Optional<BrainResponse> getAnswer(Sentence sentence, String scope) {
    boolean answer = false;
    if (scope.length() > 0 && scope.charAt(scope.length() - 1) == '?') {
      answer = true;
    }

    SemanticNetwork semanticNetwork;
    try {
      semanticNetwork = SemanticNetwork.getInstance();
    } catch (IOException e) {
      e.printStackTrace();
      semanticNetwork = null;
    }

    if (answer) {
      Word verb;
      Word object;
      scope = scope.substring(0, scope.length() - 1);

      if (sentence.respect(POS.V, POS.NP)) {
        // System.out.println("V, NP");
        List<Word>[] extracted = sentence.parse(POS.V, POS.NP);
        verb = extracted[0].stream().filter(w -> w.itMeans(POS.V)).findFirst().get();
        List<Word> nouns =
            extracted[1].stream().filter(w -> w.itMeans(POS.N)).collect(Collectors.toList());
        object = nouns.get(nouns.size() - 1);
      }
      if (sentence.has(POS.V) && sentence.has(POS.N)) {
        // System.out.println("V, N");
        verb = sentence.get(POS.V).get(0);
        List<Word> nouns = sentence.get(POS.N);
        object = nouns.get(nouns.size() - 1);
      } else {
        return Optional.empty();
      }

      SemanticQuery sq = new SemanticQuery(
          SemanticRelationType.IS_A,
          scope,
          object.getText(),
          null
      );
      Optional<Edge> opt = semanticNetwork.query(sq);

      String newMessage;
      String newScope;
      if (opt.isPresent()) {
        newMessage = "I already knew that " + scope + " is a " + object.getText() + ".";
        newScope = object.getText();
      } else {
        semanticNetwork.learn(scope, SemanticRelationType.IS_A, object.getText());
        newMessage = "Wow really interesting! Now I know that a " + scope + " is a "
            + object.getText() + ".";
        newScope = object.getText();
      }

      return Optional.of(new BrainResponse(newMessage, newScope));
    }

    if (sentence.has(POS.N)) {
      List<Word> nouns = sentence.get(POS.N);
      Word object = nouns.get(nouns.size() - 1);

      SemanticQuery sq = new SemanticQuery(
          SemanticRelationType.IS_A,
          null,
          object.getText(),
          null
      );
      Optional<Edge> opt = semanticNetwork.query(sq);

      String newMessage;
      String newScope;
      if (opt.isPresent()) {
        String answ = opt.get().getTarget().toString();
        newMessage = "For what I know, " + object.getText() + " is " + answ + "!";
        newScope = object.getText();
      } else {
        newMessage = "Mhh! What is a " + object.getText() + "?";
        newScope = object.getText() + "?";
      }
      return Optional.of(new BrainResponse(newMessage, newScope));
    }

    return Optional.empty();
  }
}

@@ -698,19 +698,88 @@
 %0A * 
-Description
+Contains utility method to get an answer from a given sentence and a given scope
 .%0A *
@@ -807,16 +807,17 @@
 ation %7B%0A
+%0A
   /**%0A  
@@ -823,102 +823,186 @@
   * 
-getAnswer description.%0A   *%0A   * @param sentence %5Bdescription%5D%0A   * @param scope %5Bdescripti
+Gets a coherent answer from an input sentence and scope as Optional instance.%0A   * @param sentence The quesiton to be answered%0A   * @param scope The subject of the given quesit
 on
-%5D
 %0A   
@@ -1015,21 +1015,183 @@
 urn 
-%5Bdescription%5D
+an Optional instance.%0A   *         If the answer has not been found, an empty Optional will be returned.%0A   *         Otherwise, the Optional instance will contain the answer.
 %0A   
@@ -1761,48 +1761,8 @@
 ) %7B%0A
-        // System.out.println(%22V, NP%22);%0A
     
@@ -2078,24 +2078,24 @@
 1);%0A      %7D%0A
+
       if (se
@@ -2142,47 +2142,8 @@
 ) %7B%0A
-        // System.out.println(%22V, N%22);%0A
     

improve Affirmation javadoc and remove useless comments