package org.studiorailgun.ai.linguistics;

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

import org.studiorailgun.Globals;
import org.studiorailgun.ai.conversation.parser.depend.Argument;
import org.studiorailgun.ai.conversation.tracking.Quote;
import org.studiorailgun.ai.conversation.web.QuoteQuery;
import org.studiorailgun.ai.knowledge.Node;
import org.studiorailgun.ai.knowledge.Relation;
import org.studiorailgun.ai.knowledge.query.InstanceQuery;
import org.studiorailgun.ai.knowledge.types.RelationTypes;

/**
 * Queries related to possession
 */
public class PosessionQuery {
    
    /**
     * Gets the root node with the possession modifier applied
     * @param rootNode The root node
     * @param argument The argument
     * @return The node that represents the possession modifier applied to the root node
     */
    public static Node getPossessionModified(Quote quote, Node rootNode, Argument argument){
        Node rVal = rootNode;
        String possessionModifier = argument.getPossessiveModifier().originalText().toLowerCase();
        switch(possessionModifier){
            case "my": {
                List<Node> instances = InstanceQuery.getInstances(rootNode);
                List<Node> possessedInstances = instances.stream().filter((Node node) -> {
                    List<Relation> relations = Globals.web.getRelationsOfChildNode(node);
                    List<Relation> possessionRelationsToRoot = relations.stream().filter(relation -> relation.getName().equals(RelationTypes.POSSESSION_OF) && relation.getParent().equals(rootNode)).collect(Collectors.toList());
                    return possessionRelationsToRoot.size() > 0;
                }).collect(Collectors.toList());
                if(possessedInstances.size() == 1){
                    //already exists, return it
                    rVal = possessedInstances.get(0);
                } else if(possessedInstances.size() > 1) {
                    //already exists, return it
                    throw new Error("Can't currently handle plural instances!");
                } else {
                    rVal = PosessionQuery.generatePossessionModified(quote, rootNode, argument);
                }
            } break;
            default: {
                throw new Error("Unsupported possession modifier! " + possessionModifier);
            }
        }
        return rVal;
    }

    /**
     * Generates a node to fill in the instance of the possessor owning an instance of the root node
     * @param quote The quote
     * @param rootNode The root node
     * @param argument The argument
     * @return The generated node
     */
    private static Node generatePossessionModified(Quote quote, Node rootNode, Argument argument){
        Node possession = Globals.web.createNode("possession(inst)");

        //attach to concept
        Globals.web.createRelation(RelationTypes.INSTANCE_OF, rootNode, possession);
        
        //attach to speaker
        Node possessor = QuoteQuery.getSpeaker(quote.getNode());
        Globals.web.createRelation(RelationTypes.POSSESSION_OF, possessor, possession);

        return possession;
    }

}
