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

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.Node;
import javafx.scene.shape.Line;

import javax.annotation.Nonnull;

/**
 * User: twalcari
 * Date: 11/8/13
 * Time: 12:19 PM
 */
public class CanvasLink extends Line {

    private static final String CANVAS_LINK_STYLECLASS = "canvas-link";
    private static final String CANVAS_LINK_SELECTED_STYLECLASS = "canvas-link";
    private static final String CANVAS_LINK_EDITABLE_STYLECLASS = "canvas-link-editable";

    private final BooleanProperty selected = new SimpleBooleanProperty();
    @Nonnull
    private final Node nodeA;
    @Nonnull
    private final Node nodeB;

    {
        /*
            Styling
         */

        //add listener to selected-property for styling
        selected.addListener((observableValue, newValue, oldValue) -> {
            if (newValue) {
                getStyleClass().remove(CANVAS_LINK_STYLECLASS);
                getStyleClass().add(CANVAS_LINK_SELECTED_STYLECLASS);

            } else {
                getStyleClass().remove(CANVAS_LINK_SELECTED_STYLECLASS);
                getStyleClass().add(CANVAS_LINK_STYLECLASS);

            }
        });

        getStyleClass().add(CANVAS_LINK_STYLECLASS);
        getStyleClass().add(CANVAS_LINK_EDITABLE_STYLECLASS);

        /*
            Event Listeners
         */
        // setOnMouseClicked(this);
    }

    public CanvasLink(@Nonnull Node nodeA, @Nonnull Node nodeB) {
        this.nodeA = nodeA;
        this.nodeB = nodeB;

        startXProperty().bind(NewLinkTarget.createCenterXBinding(nodeA));
        startYProperty().bind(NewLinkTarget.createCenterYBinding(nodeA));

        endXProperty().bind(NewLinkTarget.createCenterXBinding(nodeB));
        endYProperty().bind(NewLinkTarget.createCenterYBinding(nodeB));
    }

    @Nonnull
    public Node getNodeA() {
        return nodeA;
    }

    @Nonnull
    public Node getNodeB() {
        return nodeB;
    }


    public boolean isSelected() {
        return selected.get();
    }

    public void setSelected(boolean selected) {
        this.selected.set(selected);
    }

    public BooleanProperty selectedProperty() {
        return selected;
    }
}
