package org.studiorailgun.conversation.evaluators.query;

import java.util.List;

import org.studiorailgun.Globals;
import org.studiorailgun.conversation.tracking.Conversation;
import org.studiorailgun.conversation.tracking.Quote;
import org.studiorailgun.knowledge.Node;
import org.studiorailgun.knowledge.query.InstanceQuery;
import org.studiorailgun.knowledge.query.QualiaQuery;
import org.studiorailgun.knowledge.query.filter.PossessionQueryFilter;

/**
 * Interrogatives available
 */
public class Interrogative {
    
    /**
     * Pronouns that are interrogative
     */
    public static enum InterrogativePronoun {
        WHAT,
        WHICH,
        WHO,
        WHOSE,
    }

    /**
     * Adverbs that are interrogative
     */
    public static enum InterrogativeAdverb {
        WHY,
        WHERE,
        HOW,
        WHEN,
    }

    /**
     * Evaluates a query of "which"
     * @param conversation The conversation
     * @param quote The quote
     * @param interrogative The interrogative noun
     * @param items The items noun
     * @return null
     */
    public static void evalWhichQuery(Conversation conversation, Quote quote, NounStack interrogative, NounStack items){
        if(QualiaQuery.isQualiaClarificationQuery(interrogative)){
            Node finalQualifier = InstanceQuery.getConcept(items.indexedWord.originalText());
            
            if(items.possessive != null){
                List<Node> qualifierInstances = InstanceQuery.getInstances(finalQualifier);
                finalQualifier = PossessionQueryFilter.withPossessor(qualifierInstances, Globals.web.getAnchors().getSelfNode());
            }

            List<Node> qualiaOfInstance = QualiaQuery.getQualiaOfInstance(finalQualifier);

            Node qualiaType = QualiaQuery.getRequestedQualiaType(interrogative);

            conversation.addQuote(quote);
            conversation.getGoalData().getQueryData().getRecentQueries().add(quote);
        } else {
            throw new Error("Unknown query type!");
        }
    }

    /**
     * Evaluates a query of "what"
     * @param conversation The conversation
     * @param quote The quote
     * @param interrogative The interrogative noun
     * @param items The items noun
     * @return null
     */
    public static void evalWhatQuery(Conversation conversation, Quote quote, NounStack interrogative, NounStack items){
        if(QualiaQuery.isQualiaClarificationQuery(interrogative)){
            Node finalQualifier = InstanceQuery.getConcept(items.indexedWord.originalText());
            
            if(items.possessive != null){
                List<Node> qualifierInstances = InstanceQuery.getInstances(finalQualifier);
                finalQualifier = PossessionQueryFilter.withPossessor(qualifierInstances, Globals.web.getAnchors().getSelfNode());
            }

            List<Node> qualiaOfInstance = QualiaQuery.getQualiaOfInstance(finalQualifier);

            Node qualiaType = QualiaQuery.getRequestedQualiaType(interrogative);

            conversation.addQuote(quote);
            conversation.getGoalData().getQueryData().getRecentQueries().add(quote);
        } else {
            throw new Error("Unknown query type!");
        }
    }

}
