package be.iminds.ilabt.jfed.experimenter_gui.canvas.rspec;

import be.iminds.ilabt.jfed.experimenter_gui.config.JFedGuiConfig;
import be.iminds.ilabt.jfed.experimenter_gui.canvas.NewLinkTarget;
import be.iminds.ilabt.jfed.experimenter_gui.canvas.TestbedAwareCanvasNode;
import be.iminds.ilabt.jfed.experimenter_gui.util.ImageSize;
import be.iminds.ilabt.jfed.experimenter_gui.util.ImageUtil;

import be.iminds.ilabt.jfed.lowlevel.TestbedInfoSource;
import be.iminds.ilabt.jfed.rspec.model.javafx_impl.FXRspecNode;
import javafx.geometry.Point2D;

/**
 * User: twalcari
 * Date: 11/8/13
 * Time: 12:13 PM
 */
public class RspecCanvasNode extends TestbedAwareCanvasNode implements NewLinkTarget {
    private final FXRspecNode node;

    public RspecCanvasNode(FXRspecNode node, TestbedInfoSource testbedInfoSource, JFedGuiConfig config) {
        super(testbedInfoSource, config);
        this.node = node;

        setImage(ImageUtil.getRspecNodeImage(config, node, ImageSize.SMALL));

        idProperty().bind(node.clientIdProperty());
        textProperty().bind(node.clientIdProperty());

        layoutXProperty().bindBidirectional(node.editorXProperty());
        layoutYProperty().bindBidirectional(node.editorYProperty());

        //special case: if no jFed location, but a flack location DOES exist, set jFed location to flack location
        if (node.getEditorX() < 0.0 && node.getEditorY() < 0.0 && node.getFlackEditorX() >= 0) {
            node.setEditorX(node.getFlackEditorX());
            node.setEditorY(node.getFlackEditorY());
        }

        node.resourceClassIdProperty().addListener(observable -> setImage(ImageUtil.getRspecNodeImage(config, RspecCanvasNode.this.node, ImageSize.SMALL)));

        componentManagerId.bind(node.componentManagerIdProperty());
    }


    public FXRspecNode getRspecNode() {
        return node;
    }

    @Override
    public Point2D getCenterPosition() {
        return new Point2D(NewLinkTarget.getCenterX(this), NewLinkTarget.getCenterY(this));
    }

}
