// Carnegie Mellon University
//   Information Networking Institute and
//   School of Computer Science
//
// Master Thesis: A Monitoring Tool for Overlay Network
// By: TungFai Chan and Annie Cheng
//
// File: Node.java
// Path: userInterfaces/graph/
// Description: Object extends VisualElement represent node on the graph


package userInterfaces.graph;

import userInterfaces.images.*;

import java.awt.*;
import java.awt.image.*;
import java.util.*;

public class Node extends VisualElement {

    static final int LABEL_CHAR_OFFSET_X = 6;
    static final int LABEL_OFFSET_Y = 20;
    static final int IMG_RADIUS = 8;
    static final int SEL_IMG_RADIUS = 12;


    Coordination coordinate;
    String nodeLabel = null;
    int nodeID = -1;
    Color textColor = null;

    Node parentNode = null;
    Hashtable childrenNodes = null;

    boolean activated = true;

    public Node(Coordination coord, int id, String name, Color txtCol) {
        coordinate = coord;
        nodeID = id;
        nodeLabel = name;
        textColor = txtCol;

        childrenNodes = new Hashtable();

        elementType = VisualElement.NODE;

    }

    public int getNodeID() {
        return nodeID;
    }

    public Coordination getCoordination() {
	return coordinate;
    }

    public Node getParentNode() {
        return parentNode;
    }

    public String getNodeLabel() {
        return nodeLabel;
    }

    public void setNodeID(int newID) {
        nodeID = newID;
    }

    public void setCoordination(Coordination newCoord) {
        coordinate = newCoord;
    }

    public void setParentNode(Node parent) {
        parentNode = parent;
    }

    public void setNodeLabel(String name) {
        nodeLabel = name;
    }

    public void addChild(Node node) {
        childrenNodes.put(new Integer(node.getNodeID()), node);
    }

    public void removeChild(int id) {
        childrenNodes.remove(new Integer(id));
    }

    public void draw(Graphics g, int graphType) {

        if (!activated)
            return;

        g.setColor(textColor);
        int halfLen = nodeLabel.length() / 2;
        g.drawString(nodeLabel, coordinate.x - halfLen * LABEL_CHAR_OFFSET_X,
                     coordinate.y + LABEL_OFFSET_Y);

        if (!selected)
            g.drawImage(ImagesList.nodeImage, coordinate.x - IMG_RADIUS,
                        coordinate.y - IMG_RADIUS, null);
        else
            g.drawImage(ImagesList.nodeSelectedImage, coordinate.x - SEL_IMG_RADIUS,
                        coordinate.y - SEL_IMG_RADIUS, null);


    }

    public boolean equal(Node node) {
        return (nodeID == node.getNodeID());
    }

    public boolean isThisNode(Coordination coord) {

        if (isInRange(coord))
            selected = true;
        else
            selected = false;

        return selected;
    }

    public boolean isChild(Node node) {
        if (childrenNodes.get(new Integer(node.getNodeID())) == null)
            return false;

        return true;
    }

    public int numChildren() {
        return childrenNodes.size();
    }

    public boolean isInRange(Coordination coord) {
        int dx = coord.x - coordinate.x;
        int dy = coord.y - coordinate.y;
        if (dx * dx + dy * dy <= IMG_RADIUS * IMG_RADIUS)
            return true;

        return false;
    }

    public void setActivated(boolean act) {
        activated = act;
    }

    public boolean isActivated() {
        return activated;
    }

    public String toString() {
        return "Node: " + getNodeLabel();
    }



}
