package co.j6mes.ui.fluidgrid.extension;

import javafx.scene.Node;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.input.DragEvent;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Region;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by james on 01/05/2016.
 */
public class DragAndDrop implements Extension {

    List<Node> nodes = new ArrayList<Node>();

    ExtensionManager host;




    @Override
    public void registerHost(ExtensionManager exm) {
        this.host = exm;
    }

    @Override
    public void register(Node object) {

    }

    @Override
    public void handleContextMenu(ContextMenuEvent event) {

    }

    @Override
    public void handleDragDetected(MouseEvent event) {
        System.out.println("Drag");
        System.out.println(event.getSource());

        if(host.getObjects().size()<2) {
            System.out.println("Only 1 component. No need for drag.");
            return;
        }

        ((Node)event.getSource()).startFullDrag();
        //displayPreview(pf,event,l);

    }

    @Override
    public void handleDragOver(DragEvent event) {

    }

    @Override
    public void handleMouseClicked(MouseEvent event) {

    }

    @Override
    public void handleDragExited(DragEvent event) {

    }

    @Override
    public void handleDragEntered(DragEvent event) {

    }

    @Override
    public void handleDragDropped(DragEvent event) {

        //int indexOfDropTarget = children.getChildren().indexOf(l);
       //
        // rotateNodes(children, indexOfDraggingNode, indexOfDropTarget);
        event.consume();
    }

    @Override
    public void handleDragDone(DragEvent event) {

    }

    @Override
    public void handleMouseDragReleased(Node endRegion, MouseDragEvent event) {
        System.out.println("Handling mouse drop");

        if(!event.isConsumed()) {
            event.consume();
        } else {
            return;
        }

        System.out.println("Not cosumed");


     //   int indexOfDraggingNode = host.getObjects().indexOf(event.get());


        Region source = (Region) event.getGestureSource();
        Region target = (Region) endRegion;


       // int targetIdx = host.getObjects().indexOf(target);

        if(host.getObjects().indexOf(target) > host.getObjects().indexOf(source)) {

            host.getObjects().remove(source);
            host.getObjects().add(host.getObjects().indexOf(target)+1, source);
        } else {

            host.getObjects().remove(source);
            host.getObjects().add(host.getObjects().indexOf(target), source);
        }


        host.update();

    }
}

@@ -1837,60 +1837,8 @@
 ) %7B%0A
-        System.out.println(%22Handling mouse drop%22);%0A%0A
     
@@ -1948,131 +1948,8 @@
  %7D%0A%0A
-        System.out.println(%22Not cosumed%22);%0A%0A%0A     //   int indexOfDraggingNode = host.getObjects().indexOf(event.get());%0A%0A%0A
     
@@ -2052,71 +2052,8 @@
 n;%0A%0A
-%0A       // int targetIdx = host.getObjects().indexOf(target);%0A%0A
     

Remove useless debug