package org.studiorailgun.conversation.evaluators;

import org.studiorailgun.Globals;
import org.studiorailgun.conversation.categorization.SentenceFunctionCategorizor;
import org.studiorailgun.conversation.evaluators.goal.GoalEval;
import org.studiorailgun.conversation.evaluators.greet.GreetingEval;
import org.studiorailgun.conversation.evaluators.query.QueryEval;
import org.studiorailgun.conversation.evaluators.synthesis.ResponseEval;
import org.studiorailgun.conversation.parser.NLPParser;
import org.studiorailgun.conversation.tracking.Conversation;
import org.studiorailgun.conversation.tracking.Quote;
import org.studiorailgun.conversation.tracking.Sentence;

/**
 * Evaluates a sentence based on data about the sentence
 */
public class EvaluationTree {

    /**
     * Number of times to evaluate response logic
     */
    static final int RESPONSE_EVAL_COUNT = 2;
    
    /**
     * Evaluates a quote
     * @param quote The quote
     */
    public static Quote evaluate(Conversation conversation, Quote quote){

        //perform NLP evaluation of the quote
        NLPParser.parse(quote);

        //parse data about the quote
        for(Sentence sentence : quote.getSentences()){
            SentenceFunctionCategorizor.categorize(sentence);
        }

        //add the quote to the conversation
        conversation.addQuote(quote);

        //evaluate each sentence
        for(Sentence sentence : quote.getSentences()){
            switch(sentence.getFunction()){
                case UTILITY: {
                    GreetingEval.evaluate(conversation, quote, sentence);
                } break;
                case QUERY: {
                    QueryEval.evaluate(conversation, quote, sentence);
                } break;
                default: {
                    throw new UnsupportedOperationException("Unsupported quote function: " + sentence.getFunction());
                }
            }
        }


        //the response quote
        Quote response = new Quote("");


        //repeatedly check if we can generate a new sentence
        for(int i = 0; i < RESPONSE_EVAL_COUNT; i++){
            //evaluate the AI's current goal in the conversation
            GoalEval.evaluate(conversation);

            //finished evaluation (couldn't find any more goals to pursue)
            if(Globals.conversation.getGoalData().getGoal() == null){
                break;
            }

            //synthesize language based on the results of the actions performed
            Sentence newSent = ResponseEval.evaluate(conversation);
            if(newSent != null){
                response.appendSentence(newSent);
            }
        }

        return response;
    }

}
