package gui.sub_controllers;

import graph.SequenceGraph;
import graph.SequenceNode;
import gui.GraphDrawer;
import javafx.scene.control.TextField;

import java.io.IOException;

/**
 * Created by Jip on 17-5-2017.
 * The Zoom Controller controls all buttons
 * in the GUI that are the boss of zooming.
 */
public class ZoomController {

    private static final double BUTTON_ZOOM_IN_FACTOR = 0.8;
    private static final double BUTTON_ZOOM_OUT_FACTOR = 1.25;
    private static final double SCROLL_ZOOM_IN_FACTOR = 0.9;
    private static final double SCROLL_ZOOM_OUT_FACTOR = 1.1;

    private final SequenceGraph graph;
    private final GraphDrawer drawer;
    private final TextField nodeTextField, radiusTextField;
    private final PanningController panningController;

    /**
     * Constructor of the Zoom Controller.
     * @param drwr A GraphDrawer that can draw the graph for us.
     * @param panningController The panningcontroller.
     * @param nodeField The textField that contains the centre node.
     * @param radField The textField that contains the radius.
     */
    public ZoomController(SequenceGraph graph, GraphDrawer drwr, PanningController panningController,
                          TextField nodeField, TextField radField) {
        this.graph = graph;
        drawer = drwr;
        nodeTextField = nodeField;
        radiusTextField = radField;
        this.panningController = panningController;
    }

    /**
     * ZoomIn.
     * @throws IOException exception.
     */
    public void zoomIn() {
        if (nodeTextField.getText().equals("")) {
            drawer.zoom(BUTTON_ZOOM_IN_FACTOR,
                    drawer.getRealCentreNode().getColumn());
            panningController.setScrollbarSize();
        } else {
            drawer.zoom(BUTTON_ZOOM_IN_FACTOR,
                    drawer.getColumnId(Integer.parseInt(nodeTextField.getText())));
            panningController.setScrollbarSize();
        }
        updateRadius((int) Math.ceil(drawer.getRadius()) + "");
    }

    /**
     * Zooms in.
     * @param column the column to zoom in on.
     * @throws IOException thrown if can't find
     */
    public void zoomIn(int column) throws IOException {
        drawer.zoom(SCROLL_ZOOM_IN_FACTOR, column);
        panningController.setScrollbarSize(column);
        updateRadius((int) Math.ceil(drawer.getRadius()) + "");
    }

    /**
     * ZoomOut.
     * @throws IOException exception.
     */
    public void zoomOut() throws IOException {
        if (nodeTextField.getText().equals("")) {
            drawer.zoom(BUTTON_ZOOM_OUT_FACTOR,
                    drawer.getRealCentreNode().getColumn());
            panningController.setScrollbarSize();
        } else {
            drawer.zoom(BUTTON_ZOOM_OUT_FACTOR,
                    drawer.getColumnId(Integer.parseInt(nodeTextField.getText())));
            panningController.setScrollbarSize();
        }
        updateRadius((int) Math.ceil(drawer.getRadius()) + "");
    }

    /**
     * Zooms out.
     * @param column the column to zoom out on.
     * @throws IOException thrown if can't find
     */
    public void zoomOut(int column) throws IOException {
        drawer.zoom(SCROLL_ZOOM_OUT_FACTOR, column);
        panningController.setScrollbarSize(column);
        updateRadius((int) Math.ceil(drawer.getRadius()) + "");
    }

    /**
     * Traverses the graph loaded to a specified destination.
     * @param centreNode The centre node to move to
     * @param radius The radius to be viewed
     */
    public void traverseGraphClicked(int centreNode, int radius) {
        drawer.changeZoom(graph.getNode(centreNode).getColumn(), radius);
        drawer.highlight(centreNode);
    }

    /**
     * Displays the centre node and radius of the current view.
     */
    public void displayInfo() {
        nodeTextField.setText(drawer.getRealCentreNode().getId() + "");
        radiusTextField.setText((int) Math.ceil(drawer.getRadius()) + "");
    }

    /**
     * Updates the radius.
     * @param newRadius the new radius.
     */
    private void updateRadius(String newRadius) {
        radiusTextField.setText(newRadius);
    }

    /**
     * Setter for the Node textfield.
     * @param newCentreNode the new centre node.
     */
    public void setNodeTextField(String newCentreNode) {
        nodeTextField.setText(newCentreNode);
    }

    /**
     * Setter for the radius textfield.
     * @param newRadius the new radius.
     */
    public void setRadiusTextField(String newRadius) {
        radiusTextField.setText(newRadius);
    }

}

@@ -3599,26 +3599,21 @@
     
-drawer.changeZoom(
+int column = 
 grap
@@ -3645,16 +3645,50 @@
 Column()
+;%0A        drawer.changeZoom(column
 , radius
@@ -3720,32 +3720,84 @@
 ht(centreNode);%0A
+        panningController.setScrollbarSize(column);%0A
     %7D%0A%0A    /**%0A 

Fix scrollbar location and size when go to node