package de.michaelhoffer;

import javafx.scene.Node;
import javafx.scene.layout.Pane;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;

/**
 * Implementation of a pane which is mouse-click draggable around its parent.
 *
 * @author Michael Hoffer, Alex Leigh
 */
public class DraggableNodePane extends Pane {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(DraggableNodePane.class);
    // node position
    private double x = 0;
    private double y = 0;
    // mouse position
    private double mousex = 0;
    private double mousey = 0;
    private Node view;
    private boolean dragging = false;
    private boolean moveToFront = true;

    public DraggableNodePane() {
        init();
    }

    public DraggableNodePane(Node view) {
        this.view = view;

        getChildren().add(view);
        init();
    }

    private void init() {
        onMousePressedProperty().set(event -> {
            // record the current mouse X and Y position on Node
            mousex = event.getSceneX();
            mousey = event.getSceneY();

            x = getLayoutX();
            y = getLayoutY();

            if (isMoveToFront()) {
                toFront();
            }
        });

        //Event Listener for MouseDragged
        onMouseDraggedProperty().set(event -> {
            // Get the exact moved X and Y

            double offsetX = event.getSceneX() - mousex;
            double offsetY = event.getSceneY() - mousey;

            x += offsetX;
            y += offsetY;

            double scaledX = x;
            double scaledY = y;

            setLayoutX(scaledX);
            setLayoutY(scaledY);

            dragging = true;

            // again set current Mouse x AND y position
            mousex = event.getSceneX();
            mousey = event.getSceneY();

            event.consume();
        });

        onMouseClickedProperty().set(event -> dragging = false);
    }

    /**
     * @return the dragging
     */
    protected boolean isDragging() {
        return dragging;
    }

    /**
     * @return the view
     */
    public Node getView() {
        return view;
    }

    /**
     * @param moveToFront the moveToFront to set
     */
    public void setMoveToFront(boolean moveToFront) {
        this.moveToFront = moveToFront;
    }

    /**
     * @return the moveToFront
     */
    public boolean isMoveToFront() {
        return moveToFront;
    }

    public void removeNode(Node n) {
        getChildren().remove(n);
    }
}