package org.studiorailgun.ai.conversation.web;

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

import org.studiorailgun.Globals;
import org.studiorailgun.ai.knowledge.Node;
import org.studiorailgun.ai.knowledge.types.RelationTypes;

/**
 * Queries related to quotes
 */
public class QuoteQuery {
    
    /**
     * Assigns a speaker to a quote
     * @param speaker The speaker
     * @param quote The quote
     */
    public static void assignSpeaker(Node speaker, Node quote){
        Globals.web.createRelation(RelationTypes.QUOTE_OF, speaker, quote);
    }

    /**
     * Gets the speaker of a quote
     * @param quote The quote
     * @return The speaker of the quote
     */
    public static Node getSpeaker(Node quote){
        List<Node> speaker = Globals.web.getRelationsOfChildNode(quote).stream()
        .filter(relation -> relation.getName().equals(RelationTypes.QUOTE_OF))
        .map(relation -> relation.getParent())
        .collect(Collectors.toList());
        if(speaker.size() == 0){
            throw new Error("No speaker defined!");
        } else if(speaker.size() > 1){
            throw new Error("Unhandled number of speakers! " + speaker.size());
        }
        return speaker.get(0);
    }

    /**
     * Assigns a conversation to a quote
     * @param conversation The conversation
     * @param quote The quote
     */
    public static void assignConversation(Node conversation, Node quote){
        Globals.web.createRelation(RelationTypes.MEMBER_OF, conversation, quote);
    }

}
