package application.controllers;

import application.fxobjects.cell.Cell;
import core.graph.Graph;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import javafx.geometry.Rectangle2D;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.WritableImage;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Screen;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;


/**
 * Created by Daphne van Tetering on 4-5-2016.
 */
@SuppressWarnings("PMD.UnusedPrivateField")
public class GraphController extends Controller<ScrollPane> {
    private Graph graph;
    private ZoomController zoomController;
    private GraphMouseHandling graphMouseHandling;
    AnchorPane root = new AnchorPane();
    private Rectangle2D screenSize;
    private int maxWidth;
    private int maxHeight;


    private String position;

    /**
     * Constructor method for this class.
     *
     * @param g               the graph.
     * @param ref             the reference string.
     * @param m               the mainController.
     * @param depth           the depth to draw.
     * @param selectedGenomes the genomes to display.
     */
    @SuppressFBWarnings("URF_UNREAD_FIELD")
    public GraphController(Graph g, Object ref, MainController m, int depth,
                           List<String> selectedGenomes) {
        super(new ScrollPane());
        this.graph = g;
        this.maxWidth = 0;
        this.screenSize = Screen.getPrimary().getVisualBounds();
        this.zoomController = new ZoomController(this);
        this.graphMouseHandling = new GraphMouseHandling(m);
        this.getRoot().setHbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
        this.getRoot().setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        this.position = "";
        maxWidth = 0;
        maxHeight = (int) screenSize.getHeight();

        this.getRoot().addEventFilter(ScrollEvent.SCROLL, event -> {
            if (event.getDeltaY() != 0) {
                event.consume();
            }
        });

        try {
            init(ref, depth, selectedGenomes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Getter method for the graph.
     *
     * @return the graph.
     */
    public Graph getGraph() {
        return graph;
    }

    /**
     * Getter method for the ZoomController
     *
     * @return the ZoomController
     */
    public ZoomController getZoomController() {
        return zoomController;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }

    /**
     * Init method for this class.
     *
     * @param ref             the reference string.
     * @param depth           the depth to draw.
     * @param selectedGenomes the genomes to be selected.
     * @throws IOException Throw exception on read GFA read failure.
     */
    public void init(Object ref, int depth, List<String> selectedGenomes) throws IOException {
        if (ref != graph.getCurrentRef()) {
            root.getChildren().clear();
        }
        int size = graph.getModel().getLevelMaps().size();

        if (depth <= size - 1 && depth >= 0 && depth != graph.getCurrentInt()) {
            root.getChildren().clear();
        }

        graph.addGraphComponents(ref, depth, selectedGenomes);

        // add components to graph pane

        if (graph.getModel().getAllCells().size() > 0) {
            root.getChildren().addAll(graph.getModel().getAllEdges());
            root.getChildren().addAll(graph.getModel().getAllCells());
        } else {
            root.getChildren().addAll(graph.getModel().getAddedEdges());
            root.getChildren().addAll(graph.getModel().getAddedCells());
        }

        List<Cell> list = graph.getModel().getAddedCells();

        for (Cell c : list) {
            graphMouseHandling.setMouseHandling(c);
        }

        graph.endUpdate();


        this.getRoot().setContent(root);

    }

    /**
     * Method to attach the keyHandler to the root of the Controller
     */
    public void initKeyHandler() {
        this.getRoot().setOnKeyPressed(zoomController.getZoomBox().getKeyHandler());
    }


    /**
     * Getter method for the genomes.
     *
     * @return the list of genomes.
     */
    public List<String> getGenomes() {
        return graph.getGenomes();
    }

    /**
     * Getter method for the position of the generated
     * snapshot
     *
     * @return the position of the snapshot
     */
    public String getPosition() {
        return position;
    }

    /**
     * Method take a snapshot of the current graph.
     *
     * @return A snapshot taken of the graph.
     * @throws IOException Throw exception on write failure.
     */
    public Image takeSnapshot() throws IOException {
        maxWidth = ((int) graph.getModel().getGraphLayout().getMaxWidth()) + 50;


        WritableImage image = new WritableImage(2500,
                (int) screenSize.getHeight());
        WritableImage snapshot = this.getRoot().getContent().snapshot(
                new SnapshotParameters(), image);

        return snapshot;
    }
}

@@ -914,92 +914,8 @@
 ize;
-%0A    private int maxWidth;%0A    private int maxHeight;%0A%0A%0A    private String position;
 %0A%0A  
@@ -1464,35 +1464,8 @@
  g;%0A
-        this.maxWidth = 0;%0A
     
@@ -1521,24 +1521,24 @@
 alBounds();%0A
+
         this
@@ -1642,16 +1642,17 @@
 ing(m);%0A
+%0A
         
@@ -1792,108 +1792,77 @@
 R);%0A
- 
+%0A
        
-this.position = %22%22;%0A        maxWidth = 0;%0A        maxHeight = (int) screenSize.getHeight();%0A
+ //TODO: Remove event filter, for enabling zooming in and out
 %0A   
@@ -4367,212 +4367,8 @@
  %7D%0A%0A
-    /**%0A     * Getter method for the position of the generated%0A     * snapshot%0A     *%0A     * @return the position of the snapshot%0A     */%0A    public String getPosition() %7B%0A        return position;%0A    %7D%0A%0A
     
@@ -4602,91 +4602,8 @@
 n %7B%0A
-        maxWidth = ((int) graph.getModel().getGraphLayout().getMaxWidth()) + 50;%0A%0A%0A
     

Remove unnecessary lines of code