package leigh.mecha.nodegraph;

import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * This class represents an individual node on a {@link NodeGraph}. The node has a visual position on the graph
 * denoted by x,y coordinates, as well as optionally attributes and connections of those attributes to other
 * nodes.
 *
 * @author Alex Leigh
 */
public class NodeObject {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(NodeObject.class);
    private final int id;
    private double posX;
    private double posY;
    private final NodeConfig nodeConfig;
    private final ArrayList<NodeConnection> connections = new ArrayList<>();
    private final HashMap<String, String> values = new HashMap<>();

    public HashMap<String, String> getValues() {
        return values;
    }

    public NodeObject(int id, NodeConfig nodeConfig) {
        this.id = id;
        this.nodeConfig = nodeConfig;
    }

    @Override
    public String toString() {
        return "NodeObject{" +
                "id=" + id +
                ", posX=" + posX +
                ", posY=" + posY +
                ", nodeConfig=" + nodeConfig +
                ", connections=" + connections +
                ", values=" + values +
                '}';
    }

    public double getPosX() {
        return posX;
    }

    public void setPosX(double posX) {
        logger.info("X updating: {}", posX);
        this.posX = posX;
    }

    public double getPosY() {
        return posY;
    }

    public void setPosY(double posY) {
        logger.info("Y updating: {}", posY);
        this.posY = posY;
    }

    /**
     * Return the meta-configuration for the node. This is the configuration which is common to all instances
     * of the node in a graph.
     */
    public NodeConfig getNodeConfig() {
        return nodeConfig;
    }

    /**
     * Return the integer id for the node. The id is unique within a given nodegraph and carries no other meaning.
     * The id can change from one serialization to another, so it should not be relied on.
     */
    public int getId() {
        return id;
    }

    /**
     * Return a value for the given key if it has been set in the node.
     */
    public String getValue(String key) {
        return values.get(key);
    }

    /**
     * Set a value for the given key in the node.
     */
    public void setValue(String key, String value) {
        values.put(key, value);
    }

    /**
     * Return the connections for this node. Connections are stored on the outbound node (the origination node),
     * so to find connections that connect to this node, you must search the other nodes in the graph.
     */
    public ArrayList<NodeConnection> getConnections() {
        return connections;
    }
}
