package org.studiorailgun.conversation.evaluators.transfer;

import java.util.List;

import org.studiorailgun.Globals;
import org.studiorailgun.conversation.evaluators.query.NounStack;
import org.studiorailgun.conversation.synthesis.NounStackSynthesizer;
import org.studiorailgun.conversation.synthesis.QualitySynthesizer;
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.InstanceQueryFilter;
import org.studiorailgun.knowledge.query.filter.PossessionQueryFilter;

/**
 * Interrogatives available
 */
public class Interrogative {

    /**
     * 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 String evalWhichQuery(Conversation conversation, Quote quote, NounStack interrogative, NounStack items){

        //get the thing that has the quality
        if(QualiaQuery.isQualiaClarificationQuery(interrogative)){
            Node finalQualifier = InstanceQuery.getConcept(items.getIndexedWord().originalText());
            if(items.getPossessive() != null){
                List<Node> qualifierInstances = InstanceQuery.getInstances(finalQualifier);
                finalQualifier = PossessionQueryFilter.withPossessor(qualifierInstances, Globals.web.getAnchors().getSelfNode());
            }

            //get the actual qualities that we care about
            List<Node> qualiaOfInstance = QualiaQuery.getQualiaOfInstance(finalQualifier);
            Node qualiaType = QualiaQuery.getRequestedQualiaType(interrogative);
            List<Node> applicableQualities = InstanceQueryFilter.filter(qualiaOfInstance, qualiaType);

            //synthesize
            String subject = NounStackSynthesizer.synthesize(finalQualifier, items.getPossessive() != null);
            String verb = "is";
            String object = QualitySynthesizer.synthesize(applicableQualities, qualiaType);

            return subject + " " + verb + " " + object;
        } 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 String evalWhatQuery(Conversation conversation, Quote quote, NounStack interrogative, NounStack items){
        if(QualiaQuery.isQualiaClarificationQuery(interrogative)){

            //get the thing that has the quality
            Node finalQualifier = InstanceQuery.getConcept(items.getIndexedWord().originalText());
            if(items.getPossessive() != null){
                List<Node> qualifierInstances = InstanceQuery.getInstances(finalQualifier);
                finalQualifier = PossessionQueryFilter.withPossessor(qualifierInstances, Globals.web.getAnchors().getSelfNode());
            }

            //get the actual qualities that we care about
            List<Node> qualiaOfInstance = QualiaQuery.getQualiaOfInstance(finalQualifier);
            Node qualiaType = QualiaQuery.getRequestedQualiaType(interrogative);
            List<Node> applicableQualities = InstanceQueryFilter.filter(qualiaOfInstance, qualiaType);

            //synthesize
            String subject = NounStackSynthesizer.synthesize(finalQualifier, items.getPossessive() != null);
            String verb = "is";
            String object = QualitySynthesizer.synthesize(applicableQualities, qualiaType);

            return subject + " " + verb + " " + object;
        } else {
            throw new Error("Unknown query type!");
        }
    }

}
