package org.studiorailgun.conversation.evaluators.query;

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

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;

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

    /**
     * Evaluates an equivalence query
     * @param conversation The conversation
     * @param quote The quote
     */
    private static void 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.interrogative != null && secondNoun.interrogative != null){
            String message = "Comparing two interrogatives!";
            throw new Error(message);
        }

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

            switch(interrogativeStack.interrogative.toLowerCase()){
                case "which": {
                    Interrogative.evalWhichQuery(conversation, quote, interrogativeStack, qualifierStack);
                } break;
                case "what": {
                    Interrogative.evalWhatQuery(conversation, quote, interrogativeStack, qualifierStack);
                } break;
                default : {
                    throw new Error("Unhandled interrogative type! " + interrogativeStack.interrogative.toLowerCase());
                }
            }
            
            System.out.println("Turn this into a query");
        }
    }

}
