package org.studiorailgun.conversation.evaluators.transfer;

import java.util.Iterator;
import java.util.Set;

import org.studiorailgun.conversation.evaluators.query.NounStack;
import org.studiorailgun.conversation.parser.NLPParser;
import org.studiorailgun.conversation.parser.PennTreebankTagSet;
import org.studiorailgun.conversation.tracking.Conversation;
import org.studiorailgun.conversation.tracking.Quote;

import edu.stanford.nlp.ling.IndexedWord;
import edu.stanford.nlp.semgraph.SemanticGraph;

/**
 * Synthesizes an answer to a question
 */
public class AnswerSynthesis {
    

    /**
     * Evaluates a quote
     * @param conversation The conversation
     * @param quote The quote
     */
    public static Quote evaluate(Conversation conversation, Quote query){
        NLPParser.parse(query);
        SemanticGraph semanticGraph = query.getGraph();
        if(semanticGraph.getRoots().size() > 1){
            String message = "Multiple roots to sentence!\n" +
            "\"" + query.getRaw() + "\"\n" +
            semanticGraph;
            throw new UnsupportedOperationException(message);
        }
        IndexedWord root = semanticGraph.getFirstRoot();
        if(PennTreebankTagSet.isVerb(root.tag())){
            if(PennTreebankTagSet.isBe(root.tag())){
                String answerRaw = AnswerSynthesis.evaluateBe(conversation, query);
                Quote quote = new Quote(answerRaw);
                return quote;
            } else {
                String message = "Unsupported root verb type!\n" +
                "\"" + query.getRaw() + "\"\n" +
                semanticGraph;
                throw new UnsupportedOperationException(message);
            }
        } else {
            String message = "Unsupported root type!\n" +
                "\"" + query.getRaw() + "\"\n" +
                semanticGraph;
            throw new UnsupportedOperationException(message);
        }
    }

    /**
     * Evaluates an equivalence query
     * @param conversation The conversation
     * @param quote The quote
     */
    private static String evaluateBe(Conversation conversation, Quote quote){
        //get the two things we're comparing
        SemanticGraph graph = quote.getGraph();
        IndexedWord root = graph.getFirstRoot();
        Set<IndexedWord> children = graph.getChildren(root);
        Iterator<IndexedWord> iterator = children.iterator();
        IndexedWord firstItem = iterator.next();
        IndexedWord secondItem = iterator.next();
        NounStack firstNoun = NounStack.parse(graph, firstItem);
        NounStack secondNoun = NounStack.parse(graph, secondItem);

        //interrogative error checking
        if(firstNoun.getInterrogative() != null && secondNoun.getInterrogative() != null){
            String message = "Comparing two interrogatives!";
            throw new Error(message);
        }

        //handle interrogative solving
        if(firstNoun.getInterrogative() != null || secondNoun.getInterrogative() != null){
            NounStack interrogativeStack = firstNoun.getInterrogative() != null ? firstNoun : secondNoun;
            NounStack qualifierStack = firstNoun.getInterrogative() != null ? secondNoun : firstNoun;

            switch(interrogativeStack.getInterrogative().toLowerCase()){
                case "which": {
                    return Interrogative.evalWhichQuery(conversation, quote, interrogativeStack, qualifierStack);
                }
                case "what": {
                    return Interrogative.evalWhatQuery(conversation, quote, interrogativeStack, qualifierStack);
                }
                default : {
                    throw new Error("Unhandled interrogative type! " + interrogativeStack.getInterrogative().toLowerCase());
                }
            }

            
        }
        throw new Error("Failed to synthesize answer!");
    }

}
