package org.studiorailgun.ai.knowledge.query;

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

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

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


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

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

}
