package org.studiorailgun.knowledge.anchor;

import java.util.Map;

import org.studiorailgun.Globals;
import org.studiorailgun.knowledge.Node;

/**
 * Anchor nodes for the rest of the web
 */
public class AnchorNodes {
    
    /**
     * The node that represents the self
     */
    Integer self;

    /**
     * The node that represents the idea of a concept
     */
    Integer concept;

    /**
     * The concept of a qualia
     */
    Integer qualia;

    /**
     * The concept of a name
     */
    Integer name;

    /**
     * Gets the self
     * @return The self node
     */
    public Node getSelfNode() {
        if(self == null){
            return null;
        }
        return Globals.web.getNode(self);
    }

    /**
     * Sets the self node ID
     * @param id The self node's ID
     */
    public void setSelf(int id) {
        this.self = id;
    }

    /**
     * Gets the id for the self node
     * @return The id
     */
    public Integer getSelf(){
        return self;
    }

    /**
     * Gets the idea of a concept
     * @return The idea of a concept
     */
    public Node getConceptNode() {
        if(concept == null){
            return null;
        }
        return Globals.web.getNode(concept);
    }

    /**
     * Sets the concept node
     * @param id The concept node
     */
    public void setConcept(int id){
        this.concept = id;
    }

    /**
     * Gets the id for the concept node
     * @return The id for the concept node
     */
    public Integer getConcept(){
        return concept;
    }

    /**
     * Gets the idea of a qualia
     * @return The idea of a qualia
     */
    public Node getQualiaNode() {
        if(qualia == null){
            return null;
        }
        return Globals.web.getNode(qualia);
    }

    /**
     * Sets the qualia node
     * @param id The qualia node
     */
    public void setQualia(int id){
        this.qualia = id;
    }

    /**
     * Gets the id for the qualia node
     * @return The id for the qualia node
     */
    public Integer getQualia(){
        return qualia;
    }

    /**
     * Gets the idea of a name
     * @return The idea of a name
     */
    public Node getNameNode() {
        if(name == null){
            return null;
        }
        return Globals.web.getNode(name);
    }

    /**
     * Sets the name node
     * @param id The name node
     */
    public void setName(int id){
        this.name = id;
    }

    /**
     * Gets the id for the name node
     * @return The id for the name node
     */
    public Integer getName(){
        return name;
    }

    /**
     * Copies anchor nodes from the child to the parent
     * @param parentAnchors The parent anchors
     * @param childAnchors The child anchors
     */
    public static void copyToParent(AnchorNodes parentAnchors, AnchorNodes childAnchors){
        if(childAnchors.getConcept() != null){
            parentAnchors.setConcept(childAnchors.getConcept());
        }
        if(childAnchors.getSelf() != null){
            parentAnchors.setSelf(childAnchors.getSelf());
        }
        if(childAnchors.getQualia() != null){
            parentAnchors.setQualia(childAnchors.getQualia());
        }
        if(childAnchors.getName() != null){
            parentAnchors.setName(childAnchors.getName());
        }
    }

    /**
     * Updates the IDs for the anchor nodes based on the newly assigned IDs after loading
     * @param crossRelationLookupMap The crossrelational lookup map
     * @param tag The tag for the current web
     */
    public void updateReferences(Map<String,Integer> crossRelationLookupMap, String tag){
        if(this.getSelf() != null){
            int oldId = this.getSelf();
            String lookupId = "node-" + tag + oldId;
            int newNodeId = crossRelationLookupMap.get(lookupId);
            this.setSelf(newNodeId);
        }
        if(this.getConcept() != null){
            int oldId = this.getConcept();
            String lookupId = "node-" + tag + oldId;
            int newNodeId = crossRelationLookupMap.get(lookupId);
            this.setConcept(newNodeId);
        }
        if(this.getQualia() != null){
            int oldId = this.getQualia();
            String lookupId = "node-" + tag + oldId;
            int newNodeId = crossRelationLookupMap.get(lookupId);
            this.setQualia(newNodeId);
        }
        if(this.getName() != null){
            int oldId = this.getName();
            String lookupId = "node-" + tag + oldId;
            int newNodeId = crossRelationLookupMap.get(lookupId);
            this.setName(newNodeId);
        }
    }

    /**
     * Checks for errors in the anchor definitions
     */
    public void errorCheck(){
        if(self == null){
            throw new Error("Self undefined!");
        }
        if(concept == null){
            throw new Error("Concept undefined!");
        }
    }

}
