package org.studiorailgun.conversation.evaluators.query;

import java.util.List;

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

/**
 * A noun stack
 */
public class NounStack {

    /**
     * If not null, this is the interrogative for this part of the noun stack
     */
    String interrogative;

    /**
     * If not null, this is the possessive pronoun for this part of the noun stack
     */
    String possessive;

    /**
     * The indexed word for this part of the noun stack
     */
    IndexedWord indexedWord = null;
    
    /**
     * Parses a noun stack from an indexed word
     * @param graph The semantic graph
     * @param indexedWord The word
     * @return The NounStack
     */
    public static NounStack parse(SemanticGraph graph, IndexedWord indexedWord){
        NounStack rVal = NounStack.parseRecursive(graph, indexedWord);
        return rVal;
    }

    /**
     * Parses a noun stack recursively
     * @param graph The semantic graph
     * @param currentRoot The current root
     * @return The noun stack
     */
    private static NounStack parseRecursive(SemanticGraph graph, IndexedWord currentRoot){
        NounStack rVal = new NounStack();
        rVal.indexedWord = currentRoot;
        List<IndexedWord> children = graph.getChildList(currentRoot);
        if(children.size() > 0){
            for(IndexedWord child : children){
                switch(child.tag()){
                    case "WDT":
                    case "WP":
                    case "WP$":
                    case "WRB": {
                        //the child is an interrogative modifying the verb
                        rVal.interrogative = child.originalText();
                    } break;
                    case "PRP$": {
                        //the child is a possessive pronoun
                        rVal.possessive = child.originalText();
                    } break;
                    default: {
                        System.out.println(child);
                        System.out.println(child.tag());
                        String message = "Unsupported child type in noun stack processing!\n" + 
                        child + "\n" +
                        child.tag() + "\n" +
                        graph;
                        throw new UnsupportedOperationException(message);
                    }
                }
            }
        } else {
            return rVal;
        }
        return rVal;
    }

    public String getInterrogative() {
        return interrogative;
    }

    public String getPossessive() {
        return possessive;
    }

    public IndexedWord getIndexedWord() {
        return indexedWord;
    }

    

}
