package org.studiorailgun.knowledge.query;

import java.util.List;
import java.util.stream.Collectors;

import org.studiorailgun.Globals;
import org.studiorailgun.conversation.evaluators.query.NounStack;
import org.studiorailgun.knowledge.Node;
import org.studiorailgun.knowledge.query.filter.NameQueryFilter;
import org.studiorailgun.knowledge.types.RelationTypes;

/**
 * Performs a qualia query
 */
public class QualiaQuery {
    
    /**
     * Checks if this noun stack is querying for a qualia
     * @param interrogativeStack The interrogative noun stack
     * @return true if it is a qualia query, false otherwise
     */
    public static boolean isQualiaClarificationQuery(NounStack interrogativeStack){
        String originalWord = interrogativeStack.getIndexedWord().originalText();
        List<Node> nodes = InstanceQuery.getInstances(Globals.web.getAnchors().getQualiaNode());
        Node rVal = NameQueryFilter.withNodeName(nodes, originalWord);
        return rVal != null;
    }


    /**
     * Gets the type of qualia requested by the interrogative stack
     * @param interrogativeStack The noun stack containing the qualia type
     * @return The qualia type
     */
    public static Node getRequestedQualiaType(NounStack interrogativeStack){
        String originalWord = interrogativeStack.getIndexedWord().originalText();
        List<Node> nodes = InstanceQuery.getInstances(Globals.web.getAnchors().getQualiaNode());
        return NameQueryFilter.withNodeName(nodes, originalWord);
    }

    /**
     * Gets all the qualia of an instance
     * @param inst The instance
     * @return The qualia
     */
    public static List<Node> getQualiaOfInstance(Node inst){
        return Globals.web.getRelationsOfChildNode(inst).stream().filter(relation -> relation.getName().equals(RelationTypes.QUALIA_OF)).map(relation -> relation.getParent()).collect(Collectors.toList());
    }

}
