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

import com.github.bot.curiosone.core.analysis.EmotionAnalysis;
import com.github.bot.curiosone.core.extraction.Brain;
import com.github.bot.curiosone.core.extraction.BrainResponse;
import com.github.bot.curiosone.core.nlp.Phrase;
import com.github.bot.curiosone.core.nlp.Sentence;

import java.io.IOException;
import java.util.List;
import java.util.Optional;

/**
 * Manages the Logic Layer of the Curiosone.
 * This class consists of a static method used to generate an answer to a provided message.
 */
public class Logic {

  /**
   * Tries to compute an answer to a given Message.
   * @param msg the Message to be answered. Can be null.
   * @return a Message instance.
   *         If an answer has been found, the instance contains a choerent reply body.
   *         Otherwise, a default message is embedded in the Message instance
   */
  public static Message talk(Message msg) {
    // We are not able to parse a null string :(
    if (msg == null) {
      return new Message("Sorry my head hurts, what were we talking about?", "", "");
    }
    Optional<BrainResponse> br;

    // We just use the first now.
    List<Phrase> phrases = Phrase.extract(msg.getMessage());
    if (phrases.size() == 0) {
      return new Message("Sorry my head hurts, what were we talking about?", "", "");
    }
    Phrase phrase = phrases.get(0);

    // Analysis
    String emotion = EmotionAnalysis.getEmotion(phrase);

    // If it's a conversational text answer directly.
    br = Brain.conversate(phrase);
    if (br.isPresent()) {
      BrainResponse answer = br.get();
      return new Message(answer.getMessage(), answer.getScope(), emotion);
    }

    // We just use the first now.
    List<Sentence> sentences = Sentence.extract(phrase);
    if (sentences.size() == 0) {
      BrainResponse answer = Brain.random(phrase);
      return new Message(answer.getMessage(), answer.getScope(), emotion);
    }
    Sentence sentence = sentences.get(0);

    br = Brain.compute(sentence, msg.getScope());
    if (br.isPresent()) {
      BrainResponse answer = br.get();
      //TODO: add refinement here.
      return new Message(answer.getMessage(), answer.getScope(), emotion);
    }

    // We have understood something but we are unable to answer now!
    return new Message("Sorry my head hurts, what were we talking about?", "", emotion);
  }
}

@@ -544,20 +544,94 @@
 essage.%0A
+ * @see  com.github.bot.curiosone.core.workflow.Message The Message Class%0A
  */%0A
-
 public c
@@ -714,19 +714,33 @@
  @param 
+ 
 msg
+%0A   *        
  the Mes
@@ -893,16 +893,16 @@
 y body.%0A
-
    *    
@@ -971,16 +971,516 @@
 nstance%0A
+   * @see  com.github.bot.curiosone.core.workflow.Message The Message Class%0A   * @see  com.github.bot.curiosone.core.extraction.Brain The Brain Class%0A   * @see  com.github.bot.curiosone.core.extraction.BrainResponse The BrainResponse Class%0A   * @see  com.github.bot.curiosone.core.nlp.Phrase The Phrase Class%0A   * @see  com.github.bot.curiosone.core.nlp.Sentence The Sentence Class%0A   * @see  com.github.bot.curiosone.core.nlp.Phrase The Phrase Class%0A   * @see  java.util.Optional The Optional Class%0A
    */%0A  

improve Logic javadoc