package cz.vutbr.feec.gui;

import ij.process.BinaryProcessor;
import ij.process.ByteProcessor;

import java.io.File;
import java.io.IOException;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.net.URL;
import java.util.LinkedList;
import java.util.ResourceBundle;
import java.util.Vector;

import javax.xml.bind.annotation.XmlElementDecl.GLOBAL;

import org.controlsfx.*;
import org.controlsfx.control.action.Action;
import org.controlsfx.dialog.Dialog;
import org.controlsfx.dialog.Dialogs;

import com.sun.glass.ui.MenuBar;
import com.sun.org.apache.xml.internal.resolver.readers.XCatalogReader;

import cz.vutbr.feec.grafy.Graf;
import cz.vutbr.feec.graph.Dijkstra;
import cz.vutbr.feec.graph.Graph;
import cz.vutbr.feec.graph.Path;
import cz.vutbr.feec.graph.Router;
import cz.vutbr.feec.graph.RoutingTable;
import cz.vutbr.feec.libraryClasses.GraphCreatingMethods;
import cz.vutbr.feec.libraryClasses.GuiMethods;
import cz.vutbr.feec.libraryClasses.GuiPainting;
import cz.vutbr.feec.libraryClasses.SearchingMethods;
import cz.vutbr.feec.libraryClasses.Skeletonization;
import cz.vutbr.feec.libraryClasses.XMLmethods;
import cz.vutbr.feec.newGui.*;
import cz.vutbr.feec.project.GlobalParameters;
import cz.vutbr.feec.project.TestSet;
import cz.vutbr.feec.stockElements.AbstractStockCell;
import cz.vutbr.feec.stockElements.BorderCell;
import cz.vutbr.feec.stockElements.CoridorCell;
import cz.vutbr.feec.stockElements.RouterCell;
import cz.vutbr.feec.stockElements.Stock;
import cz.vutbr.feec.transportObjects.AccidentCrate;
import cz.vutbr.feec.transportObjects.OtherObjectCrate;
import cz.vutbr.feec.transportObjects.ReckCrate;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
/*
 import javafx.scene.control.Dialogs;
 import javafx.scene.control.Dialogs.DialogResponse;
 */
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.Tooltip;
import javafx.scene.effect.BlendMode;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class ControllerInitializeGui implements Initializable {

	int counter = 0;
	@FXML
	Button testButton;

	public void testButtonClicked(ActionEvent evt) throws IOException {
/*
		TestSet ts = new TestSet(counter);
		counter++;
		*/
		TestSet.casy.removeAllElements();
		TestSet ts = new TestSet();
		TestSet.printResult();
		
	}

	// progressBar
	@FXML
	Label progressLabel;
	@FXML
	ProgressBar progressRT;
	@FXML
	Button cancelProgress;

	@FXML
	javafx.scene.control.MenuBar menuBar = new javafx.scene.control.MenuBar();
	// /////////////////////////

	@FXML
	ImageView newIcon = new ImageView();
	@FXML
	ImageView openIcon = new ImageView();
	@FXML
	ImageView saveIcon = new ImageView();
	@FXML
	ImageView saveAsIcon = new ImageView();
	@FXML
	ImageView undoIcon = new ImageView();
	@FXML
	ImageView redoIcon = new ImageView();
	@FXML
	ImageView zoomInIcon = new ImageView();
	@FXML
	ImageView zoomOutIcon = new ImageView();
	@FXML
	ImageView settingsIcon = new ImageView();
	@FXML
	ImageView rotateIcon = new ImageView();
	@FXML
	ImageView nextStepIcon = new ImageView();
	@FXML
	ImageView toCreatingStepIcon = new ImageView();
	@FXML
	ImageView toMaintenanceStepIcon = new ImageView();
	@FXML
	ImageView toWorkingStepIcon = new ImageView();
	@FXML
	ToggleButton deleteButton = new ToggleButton();
	@FXML
	ToggleButton rotateButton = new ToggleButton();
	@FXML
	ToggleButton netButton = new ToggleButton();
	@FXML
	ToggleButton meterButton = new ToggleButton();
	@FXML
	Canvas mainCreatingCanvas = new Canvas();
	@FXML
	Canvas reckCanvas = new Canvas();
	@FXML
	Canvas otherCanvas = new Canvas();
	@FXML
	Canvas mainMaintenanceCanvas = new Canvas();
	@FXML
	Canvas mainWorkingCanvas = new Canvas();
	@FXML
	Canvas accidentCanvas = new Canvas();
	@FXML
	Button cellSelectionSettings = new Button();
	@FXML
	MenuItem newProject = new MenuItem();
	@FXML
	MenuItem open = new MenuItem();
	@FXML
	MenuItem save = new MenuItem();
	@FXML
	MenuItem saveAs = new MenuItem();
	@FXML
	MenuItem close = new MenuItem();
	@FXML
	MenuItem undo = new MenuItem();
	@FXML
	MenuItem redo = new MenuItem();
	@FXML
	MenuItem options = new MenuItem();
	@FXML
	ScrollPane mainScrollPane = new ScrollPane();
	@FXML
	Button reckCreateButton = new Button();
	@FXML
	Button metricButton = new Button();
	// zrusit
	@FXML
	public static TextField pathNumberTextField = new TextField();
	@FXML
	TextField metricTextField = new TextField();
	// zrusit
	@FXML
	Label startCellLabel;
	// zrusit
	@FXML
	Label dstCellLabel;
	@FXML
	Label currentMetricLabel = new Label();

	@FXML
	TabPane tabPane = new TabPane();
	@FXML
	Tab creatingTab = new Tab();
	@FXML
	Tab maintenanceTab = new Tab();
	@FXML
	Tab workTab = new Tab();

	static Stock stock = Stock.getInstance();
	private static float zoomRatio = 1;
	private static GraphicsContext gc;
	public static int cellCurrentX = 0;
	public static int cellCurrentY = 0;
	private Image imageVertical = new Image("file:graphics/icons/vertical.png");
	private Image imageHorizontal = new Image(
			"file:graphics/icons/horizontal.png");

	private static Vector<ImageView> initialIcons = new Vector<ImageView>();
	private static Vector<MenuItem> initialMenuItems = new Vector<MenuItem>();

	public static int getCellCurrentX() {
		return cellCurrentX;

	}

	public static void setCellCurrentX(int cellCurrentX) {
		ControllerInitializeGui.cellCurrentX = cellCurrentX;
	}

	public static int getCellCurrentY() {
		return cellCurrentY;
	}

	public static void setCellCurrentY(int cellCurrentY) {
		ControllerInitializeGui.cellCurrentY = cellCurrentY;
	}

	public void clickIdentification(MouseEvent evt) {
		int xInCanvas = GuiMethods.getClickedFieldX(evt.getX());
		int yInCanvas = GuiMethods.getClickedFieldY(evt.getY());
		int xInField = GuiMethods.getClickedIndexOfFieldX(xInCanvas);
		int yInField = GuiMethods.getClickedIndexOfFieldY(yInCanvas);
		System.out.println("index " + xInField + "," + yInField);
		// doplnene kvoli zobrazeniu ciest
		cellCurrentX = xInField;
		cellCurrentY = yInField;
		// pozrem sa na ktorom som tabe / ak na maintenanceTabe tak idem do inej
		// metody
		if (tabPane.getSelectionModel().getSelectedItem() == maintenanceTab) {
			GuiMethods.maintenanceCanvasClicked(xInField, yInField, evt);
			return;
		}
		// pokial nie je vytvorene pole este, (hadze to null pointer) tak
		// vraciam
		if (stock.getFieldIndex(xInField, yInField) == null) {
			setCellCurrentX(1);
			setCellCurrentY(1);
			return;
		}

		if (stock.getFieldIndex(xInField, yInField).getClass() == BorderCell.class) {
			BorderCell cell = (BorderCell) stock.getFieldIndex(xInField,
					yInField);
			// ak sa kliklo vedla cize na hranicne bunky tak vraciam
			// staci ze bunka ma nastaveny obrazok
			System.out.println("Border");
			if (!cell.isImage()) {
				System.out.println("nemazat, return");
				return;
			}
		}

		// pravy klik
		if (evt.getButton() == MouseButton.SECONDARY) {
			ReckCanvas rc = GlobalParameters.reckCanvas;
			OtherCanvas oc = GlobalParameters.otherCanvas;
			if (rc.getSelectedReck() != null) {
				rc.setSelectedReck(null);
				rc.paintCanvas();
				GuiPainting.createZoomedCanvas();
				return;
			} else if (oc.getSelectedObject() != null) {
				oc.setSelectedObject(null);
				oc.paintCanvas();
				GuiPainting.createZoomedCanvas();
				return;
			} else if (GlobalParameters.deleteButton.isSelected()) {
				GlobalParameters.deleteButton.setSelected(false);
				GuiPainting.createZoomedCanvas();
			} else {
				// zobrazim nastavenia pre danu bunku

				cellSettings(new ActionEvent());
			}

		}

		if (evt.getClickCount() > 1) {
			// pokial je dvojklik
			// zobrazim informacie

			try {
				cellSettings(new ActionEvent());
				GuiMethods.cellInformation(xInField, yInField);

			} catch (Exception e2) {
				System.out.println("outOfBorder");
				// TODO: handle exception

			}

		} else {
			// normalny klik
			// check ci je zapnute kreslenie

			// treba pozret ci je vybraty nejaky regal alebo iny object
			// if (creatingReckCheckBox.isSelected()) {

			if (GuiMethods.isCreatingSelected()) {

				// vytvaram regal, alebo robm nejaku zmenu
				GuiMethods.fieldChanged();
				GuiMethods.reckCreating(xInField, yInField, true);

			}
			// normalny klik - info o bunke
			try {
				// vypisuje co je bunka zac
				GuiMethods.cellInformation(xInField, yInField);

			} catch (Exception e2) {
				System.out.println("outOfBorder/alebo nie je nastavena este");
				// TODO: handle exception
			}
		}
	}

	public void keyboardPressed(KeyEvent event) {
		if (event.getCode() == KeyCode.UP) {
			// najskor ceknut ci to tam je v pohode v textfieldoch
			int x = cellCurrentX;
			int y = cellCurrentY;
			// aby sa neslo na hranice pri pohybovani
			if (y > 1) {
				y = y - 1;
				GuiMethods.cellInformation(x, y);
				cellCurrentY = y;
			} else
				return;

		} else if (event.getCode() == KeyCode.DOWN) {
			// najskor ceknut ci to tam je v pohode v textfieldoch
			int x = cellCurrentX;
			int y = cellCurrentY;

			// aby sa neslo na hranice pri pohybovani
			if (y < stock.getWidth() / stock.getDelimiter() - 2) {
				y = y + 1;
				GuiMethods.cellInformation(x, y);
				cellCurrentY = y;
			} else
				return;

		} else if (event.getCode() == KeyCode.RIGHT) {
			int x = cellCurrentX;
			int y = cellCurrentY;
			// aby sa neslo na hranice pri pohybovani
			if (x < stock.getLength() / stock.getDelimiter() - 2) {
				x = x + 1;
				GuiMethods.cellInformation(x, y);
				cellCurrentX = x;
			} else
				return;

		} else if (event.getCode() == KeyCode.LEFT) {
			// najskor ceknut ci to tam je v pohode v textfieldoch
			int x = cellCurrentX;
			int y = cellCurrentY;
			// aby sa neslo na hranice pri pohybovani
			if (x > 1) {
				x = x - 1;
				GuiMethods.cellInformation(x, y);
				cellCurrentX = x;
			} else
				return;

		} else if (event.getCode() == KeyCode.ENTER) {
			cellSettings(new ActionEvent());
		}

	}

	public void cellSettings(ActionEvent evt) {
		if (stock.getField() != null) {
			CellSettingsWindow cw = new CellSettingsWindow(cellCurrentX,
					cellCurrentY);
			/*
			 * MainCellSettingsGui cellSettings = new MainCellSettingsGui();
			 * cellSettings.start(new Stage());
			 */
		}
	}

	public void newReckClicked(ActionEvent evt) {
		NewReckWindow nr = new NewReckWindow("", "");
		/*
		 * MainNewReckGui newReck = new MainNewReckGui(); newReck.start(new
		 * Stage());
		 */
		/*
		 * if (GuiMethods.validationReckSize()) { // ceknut ci tam take uz nie
		 * je // aj otocene hodnoty
		 * 
		 * }
		 */
	}

	public void mouseMovingWhenCreating(MouseEvent evt) {
		int currentX = -1;
		int currentY = -1;
		int xInCanvas = GuiMethods.getClickedFieldX(evt.getX());
		int yInCanvas = GuiMethods.getClickedFieldY(evt.getY());
		int xInField = GuiMethods.getClickedIndexOfFieldX(xInCanvas);
		int yInField = GuiMethods.getClickedIndexOfFieldY(yInCanvas);
		// ceknem ci je zapnute kreslenie
		// pozret ci ma vybraty nejaky regal

		// if (creatingReckCheckBox.isSelected() && currentX != xInField
		// && currentY != yInField) {
		if ((GuiMethods.isCreatingSelected()) && currentX != xInField
				&& currentY != yInField) {
			try {
				// System.out.println("creating");
				GuiPainting.createZoomedCanvas();
				GuiMethods.reckCreating(xInField, yInField, false);
				currentX = xInField;
				currentY = yInField;

				// zobrazim informacie

				// cellSettings(new ActionEvent());
				// GuiMethods.cellInformation(xInField, yInField);
			} catch (Exception e2) {
				System.out.println("outOfBorder");
			}
		}
	}

	public void createRoutersClicked(ActionEvent evt) {

		// musim zmazat vsetky routerCells, bez toho sa nevyhladaju nove routre
		// v
		// v skeletonizaci. Roture su sice vymazane ale pre binp treba tiez
		// zmazat

		GuiMethods.clearRoutersAndDistances();

		ByteProcessor bp = Skeletonization.stockToSkelet();
		Skeletonization.skeletonize(bp);

		/*
		 * 
		 * BinaryProcessor binp = Skeletonization.findRouters(bp);
		 * 
		 * Skeletonization.findPaths(binp); // optimalizujem cesty - vsetky
		 * bunky musia byt navzajom dostupne, // nesmie to byt // cez
		 * uhlopriecku GraphCreatingMethods.optimalizePaths();
		 * GuiPainting.showRouters(canvas, gc, Color.BROWN);
		 * GuiPainting.showPaths(canvas, gc, Color.YELLOW);
		 * System.out.println(stock.getPaths().size());
		 * 
		 * // rovno vytvaram aj graf GraphCreatingMethods.createGraph();
		 */
		GraphCreatingMethods.createGraph();
		GuiPainting.createZoomedCanvas();
	}

	public void pathNumberOkClicked(ActionEvent evt) {
		try {
			int x = Integer.valueOf(pathNumberTextField.getText());
			metricTextField.setText(String.valueOf(stock.getPaths().get(x)
					.getCost()));
			GuiPainting.showPathByNumber(x, mainCreatingCanvas, gc);
		} catch (Exception e) {
			pathNumberTextField.setText("!!!");
		}

	}

	public void metricOkClicked(MouseEvent evt) {
		try {
			int cost = Integer.valueOf(metricTextField.getText());
			int pathNumber = GuiPainting.selectedRoute;
			Path path = stock.getPaths().get(pathNumber);
			path.setCost(cost);
			metricTextField.setText(String.valueOf(path.getCost()));
			System.out.println("cena cesty "
					+ stock.getPaths().get(pathNumber).getCost());
			System.out.println("prvy router "
					+ stock.getPaths().get(pathNumber).getFirstRouter());
			// zatial mam zmenenu cost iba v ceste
			// treba este zmenit cost aj pre neighborov danych routrov

			Router r1 = path.getFirstRouter();
			Router r2 = path.getSecondRouter();
			System.out.println("mam obidva routre");
			r1.setCost(r2, cost);
			r2.setCost(r1, cost);
			// treba prepocitat RT
			GraphCreatingMethods.createDijkstraAndRT();
			// skuska vypisov
			SearchingMethods.cellSorting();
			SearchingMethods.findDistances();
			// dat vediet ze sa zmeny neulozili
			GlobalParameters.isSaved = false;
			GuiMethods.setNameOfProject();
		} catch (Exception e) {
			metricTextField.setText("!!!");
		}
	}

	public void dijkstraClicked(ActionEvent evt) {
		GraphCreatingMethods.createDijkstraAndRT();

	}

	/*
	 * public void createGraphClicked(ActionEvent evt) {
	 * 
	 * Vector<Path> path = stock.getStockPaths(); String nameFirst; String
	 * nameSecond = null; for (int i = 0; i < path.size(); i++) { nameFirst =
	 * String .valueOf(path.get(i).getFirstRouter().getxInField()) + "," +
	 * String.valueOf(path.get(i).getFirstRouter().getyInField()); if
	 * (path.get(i).getSecondRouter() == null) { nameSecond = "null" +
	 * String.valueOf(i); System.out.println("null second router"); } else {
	 * nameSecond = String.valueOf(path.get(i).getSecondRouter() .getxInField())
	 * + "," + String.valueOf(path.get(i).getSecondRouter() .getyInField());
	 * System.out.println("second router ok "); }
	 * stock.getGraph().add(nameFirst, nameSecond, path.get(i).getCost());
	 * System.out.println("pridane do grafu " + i);
	 * 
	 * }
	 * 
	 * Dijkstra d = new Dijkstra(stock.getGraph()); stock.setDijkstra(d);
	 * 
	 * // Faster search for paths using precomputed RoutingTable /* RoutingTable
	 * rt = new RoutingTable(g); LinkedList<Path> paths = rt.getPaths(cil,
	 * start); System.out.println("RT nejkratsi cesty " + paths);
	 * 
	 * // ukazka ulozeni tabulky na disk a ziskani tabulky z disku
	 * rt.saveToFile("file.rt"); rt = RoutingTable.loadFromFile("file.rt");
	 * 
	 * FixedPoints frame = new FixedPoints(g); frame.pack();
	 * frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 * frame.setVisible(true);
	 */

	// }

	public void findPathButtonClicked(ActionEvent evt) {
		long startTime = System.nanoTime();

		String cells = startCellLabel.getText() + "," + dstCellLabel.getText();
		String[] s = cells.split(",");
		int xStart = Integer.valueOf(s[0]);
		int yStart = Integer.valueOf(s[1]);
		int xDst = Integer.valueOf(s[2]);
		int yDst = Integer.valueOf(s[3]);
		// String path = GraphCreatingMethods.findPath(xStart, yStart, xDst,
		// yDst);
		System.out.println("distance = "
				+ SearchingMethods.findPathFromFieldIndex(xStart, yStart, xDst,
						yDst));
		long endTime = System.nanoTime();
		System.out.println("Took " + (endTime - startTime) + " ns");
		/*
		 * GuiPainting.showPathBetweenRouters(path); // este vykreslit cesty do
		 * prveho a posledneho routra String[] nodes = path.split(";"); String
		 * s1[] = nodes[0].split(","); String s2[] = nodes[nodes.length -
		 * 2].split(",");
		 * 
		 * int xRouter1 = Integer.valueOf(s1[0]); int yRouter1 =
		 * Integer.valueOf(s1[1]); System.out.println("myslim ze tu ok"); int
		 * xRouter2 = Integer.valueOf(s2[0]); int yRouter2 =
		 * Integer.valueOf(s2[1]);
		 * System.out.println("tu by mohol byt problem");
		 * 
		 * int pathNumber = GraphCreatingMethods.getPathNumberByClick(xStart,
		 * yStart); int counter =
		 * GraphCreatingMethods.getCellIndexInPath(pathNumber, xStart, yStart);
		 * 
		 * GuiPainting.showPathToRouter(counter, pathNumber, xRouter1,
		 * yRouter1);
		 * 
		 * pathNumber = GraphCreatingMethods.getPathNumberByClick(xDst, yDst);
		 * 
		 * counter = GraphCreatingMethods.getCellIndexInPath(pathNumber, xDst,
		 * yDst);
		 * 
		 * GuiPainting.showPathToRouter(counter, pathNumber, xRouter2,
		 * yRouter2);
		 */

	}

	public void newProjectClicked(ActionEvent evt) {
		GuiMethods.newProject();
	}

	public void closeClicked(ActionEvent evt) {
		if (GuiMethods.windowClosedClicked(GlobalParameters.primaryStage))
			GlobalParameters.primaryStage.close();
	}

	public void options(ActionEvent evt) {
		GuiMethods.options();
	}

	public void undo(ActionEvent evt) {
		GuiMethods.undo();
	}

	public void redo(ActionEvent evt) {
		GuiMethods.redo();

	}

	public void open(ActionEvent evt) {
		GuiMethods.open();

	}

	public void save(ActionEvent evt) {
		GuiMethods.save();
	}

	public void saveAs(ActionEvent evt) {
		GuiMethods.saveAs();
	}

	// toggleButtons
	public void deleteButtonClicked(MouseEvent evt) {
		GlobalParameters.reckCanvas.setSelectedReck(null);
		GlobalParameters.reckCanvas.paintCanvas();
		GlobalParameters.otherCanvas.setSelectedObject(null);
		GlobalParameters.otherCanvas.paintCanvas();
		GuiPainting.createZoomedCanvas();
	}

	public void netButtonClicked(MouseEvent evt) {
		GuiPainting.createZoomedCanvas();
	}

	public void meterButtonClicked(MouseEvent evt) {
		GuiPainting.createZoomedCanvas();
	}

	public void rotateButtonClicked(MouseEvent evt) {

		if (rotateButton.isSelected()) {
			// zmenit iconu
			GlobalParameters.reckCanvas.setHorizontal(true);
			rotateIcon.setImage(imageHorizontal);
		} else {
			// zmenit iconu naspat
			GlobalParameters.reckCanvas.setHorizontal(false);
			rotateIcon.setImage(imageVertical);
		}

	}

	// ///////////////////////////////////////////////////////////
	// icons
	public void newMouseClicked(MouseEvent evt) {
		GuiMethods.newProject();
	}

	public void newMouseEntered(MouseEvent evt) {
		Tooltip t = new Tooltip("New project");
		Tooltip.install(newIcon, t);
		GuiMethods.iconEntered(newIcon);
	}

	public void newMouseExited(MouseEvent evt) {
		GuiMethods.iconExited(newIcon);
	}

	public void newPressed(MouseEvent evt) {
		GuiMethods.iconPressed(newIcon);

	}

	public void newReleased(MouseEvent evt) {
		GuiMethods.iconReleased(newIcon);
	}

	public void openMouseClicked(MouseEvent evt) {
		GuiMethods.open();
	}

	public void openMouseEntered(MouseEvent evt) {
		Tooltip t = new Tooltip("Open project");
		Tooltip.install(openIcon, t);
		GuiMethods.iconEntered(openIcon);
	}

	public void openMouseExited(MouseEvent evt) {
		GuiMethods.iconExited(openIcon);
	}

	public void openPressed(MouseEvent evt) {
		GuiMethods.iconPressed(openIcon);
	}

	public void openReleased(MouseEvent evt) {
		GuiMethods.iconReleased(openIcon);
	}

	public void saveMouseClicked(MouseEvent evt) {
		GuiMethods.save();
	}

	public void saveMouseEntered(MouseEvent evt) {
		Tooltip t = new Tooltip("Save project");
		Tooltip.install(saveIcon, t);
		GuiMethods.iconEntered(saveIcon);
	}

	public void saveMouseExited(MouseEvent evt) {
		GuiMethods.iconExited(saveIcon);
	}

	public void savePressed(MouseEvent evt) {
		GuiMethods.iconPressed(saveIcon);
	}

	public void saveReleased(MouseEvent evt) {
		GuiMethods.iconReleased(saveIcon);
	}

	public void saveAsMouseClicked(MouseEvent evt) {
		GuiMethods.saveAs();
	}

	public void saveAsMouseEntered(MouseEvent evt) {
		Tooltip t = new Tooltip("Save project as");
		Tooltip.install(saveAsIcon, t);
		GuiMethods.iconEntered(saveAsIcon);
	}

	public void saveAsMouseExited(MouseEvent evt) {
		GuiMethods.iconExited(saveAsIcon);
	}

	public void saveAsPressed(MouseEvent evt) {
		GuiMethods.iconPressed(saveAsIcon);
	}

	public void saveAsReleased(MouseEvent evt) {
		GuiMethods.iconReleased(saveAsIcon);
	}

	public void undoMouseClicked(MouseEvent evt) {
		GuiMethods.undo();
	}

	public void undoMouseEntered(MouseEvent evt) {
		Tooltip t = new Tooltip("Undo");
		Tooltip.install(undoIcon, t);
		GuiMethods.iconEntered(undoIcon);
	}

	public void undoMouseExited(MouseEvent evt) {
		GuiMethods.iconExited(undoIcon);
	}

	public void undoPressed(MouseEvent evt) {
		GuiMethods.iconPressed(undoIcon);
	}

	public void undoReleased(MouseEvent evt) {
		GuiMethods.iconReleased(undoIcon);
	}

	public void redoMouseClicked(MouseEvent evt) {
		GuiMethods.redo();
	}

	public void redoMouseEntered(MouseEvent evt) {
		Tooltip t = new Tooltip("Redo");
		Tooltip.install(redoIcon, t);
		GuiMethods.iconEntered(redoIcon);
	}

	public void redoMouseExited(MouseEvent evt) {
		GuiMethods.iconExited(redoIcon);
	}

	public void redoPressed(MouseEvent evt) {
		GuiMethods.iconPressed(redoIcon);
	}

	public void redoReleased(MouseEvent evt) {
		GuiMethods.iconReleased(redoIcon);
	}

	public void zoomOutMouseClicked(MouseEvent evt) {
		if (zoomRatio - 1 <= 0) {
			System.out.println("neda sa");
			return;

		} else {
			zoomRatio = (zoomRatio - 1);
			GuiPainting.createZoomedCanvas();
		}
	}

	public void zoomOutMouseEntered(MouseEvent evt) {
		Tooltip t = new Tooltip("Zoom out");
		Tooltip.install(zoomOutIcon, t);
		GuiMethods.iconEntered(zoomOutIcon);
	}

	public void zoomOutMouseExited(MouseEvent evt) {
		GuiMethods.iconExited(zoomOutIcon);
	}

	public void zoomOutPressed(MouseEvent evt) {
		GuiMethods.iconPressed(zoomOutIcon);
	}

	public void zoomOutReleased(MouseEvent evt) {
		GuiMethods.iconReleased(zoomOutIcon);
	}

	public void zoomInMouseClicked(MouseEvent evt) {
		zoomRatio = (zoomRatio + 1);
		GuiPainting.createZoomedCanvas();
	}

	public static float getZoomRatio() {
		return zoomRatio;
	}

	public static void setZoomRatio(float zoomRatio) {
		ControllerInitializeGui.zoomRatio = zoomRatio;
	}

	public void zoomInMouseEntered(MouseEvent evt) {
		Tooltip t = new Tooltip("Zoom in");
		Tooltip.install(zoomInIcon, t);
		GuiMethods.iconEntered(zoomInIcon);
	}

	public void zoomInMouseExited(MouseEvent evt) {
		GuiMethods.iconExited(zoomInIcon);
	}

	public void zoomInPressed(MouseEvent evt) {
		GuiMethods.iconPressed(zoomInIcon);
	}

	public void zoomInReleased(MouseEvent evt) {
		GuiMethods.iconReleased(zoomInIcon);
	}

	public void settingsMouseClicked(MouseEvent evt) {
		GuiMethods.options();
	}

	public void settingsMouseEntered(MouseEvent evt) {
		Tooltip t = new Tooltip("Settings");
		Tooltip.install(settingsIcon, t);
		GuiMethods.iconEntered(settingsIcon);
	}

	public void settingsMouseExited(MouseEvent evt) {
		GuiMethods.iconExited(settingsIcon);
	}

	public void settingsPressed(MouseEvent evt) {
		GuiMethods.iconPressed(settingsIcon);

	}

	public void settingsReleased(MouseEvent evt) {
		GuiMethods.iconReleased(settingsIcon);
	}

	public void nextStepMouseClicked(MouseEvent evt) {
		// validacna hlaska
		GlobalParameters.progressCanceled = false;
		if (GuiMethods.nextStepProceed(GlobalParameters.primaryStage)) {
			// pokial sa to potvrdilo
			System.out.println("dalsi tab");
			cellCurrentX = 0;
			cellCurrentY = 0;
			stock.setIsCurrent(stock.getFieldIndex(0, 0));
			GlobalParameters.reckCanvas.setSelectedReck(null);
			GlobalParameters.otherCanvas.setSelectedObject(null);

			goToMaintenanceTab();
			GlobalParameters.isSaved = false;
			GuiMethods.setNameOfProject();
		}
	}

	public static void goToMaintenanceTab() {
		// disable tab
		GlobalParameters.creatingTab.setDisable(true);
		GlobalParameters.workTab.setDisable(true);
		// prehod na dalsi tab
		GlobalParameters.maintenanceTab.setDisable(false);
		GlobalParameters.tabPane.getSelectionModel().select(
				GlobalParameters.maintenanceTab);
		// canvas prekopirovat
		GuiMethods.setCanvas(GlobalParameters.mainMaintenanceCanvas);
		GlobalParameters.edit.removeAll();
		// vykresli
		GuiPainting.createZoomedCanvas();

	}

	public void nextStepMouseEntered(MouseEvent evt) {
		GuiMethods.iconEntered(nextStepIcon);
	}

	public void nextStepMouseExited(MouseEvent evt) {
		GuiMethods.iconExited(nextStepIcon);
	}

	public void nextStepPressed(MouseEvent evt) {
		GuiMethods.iconPressed(nextStepIcon);

	}

	public void nextStepReleased(MouseEvent evt) {
		GuiMethods.iconReleased(nextStepIcon);
	}

	public static void goToCreatingTab() {
		// disable tab
		GlobalParameters.maintenanceTab.setDisable(true);
		// prehod na dalsi tab
		GlobalParameters.creatingTab.setDisable(false);
		GlobalParameters.tabPane.getSelectionModel().select(
				GlobalParameters.creatingTab);
		// canvas prekopirovat
		GuiMethods.setCanvas(GlobalParameters.mainCreatingCanvas);
		stock.clearAccidentCells();
		stock.setDijkstra(null);
		stock.setRoutingTable(null);
		stock.setGraph(new Graph());
	}

	public void toMaintenanceStepMouseEntered(MouseEvent evt) {
		GuiMethods.iconEntered(toMaintenanceStepIcon);
	}

	public void toMaintenanceStepMouseExited(MouseEvent evt) {
		GuiMethods.iconExited(toMaintenanceStepIcon);
	}

	public void toMaintenanceStepPressed(MouseEvent evt) {
		GuiMethods.iconPressed(toMaintenanceStepIcon);

	}

	public void toMaintenanceStepReleased(MouseEvent evt) {
		GuiMethods.iconReleased(toMaintenanceStepIcon);
	}

	public void toMaintenanceStepMouseClicked(MouseEvent evt) {
		goToMaintenanceTab();
		// vykresli
		GuiPainting.createZoomedCanvas();

	}

	public static void goToWorkTab() {
		// disable tab
		GlobalParameters.maintenanceTab.setDisable(true);
		// prehod na dalsi tab
		GlobalParameters.workTab.setDisable(false);
		GlobalParameters.tabPane.getSelectionModel().select(
				GlobalParameters.workTab);
		// canvas prekopirovat

		// kliklo sa niekde mimo cesty
		GlobalParameters.metricTextField.setText("");
		// zrusim zvyraznenie cesty
		GuiPainting.selectedRoute = -1;
		GuiMethods.setCanvas(GlobalParameters.mainWorkingCanvas);
	}

	public void toCreatingStepMouseClicked(MouseEvent evt) {
		// validacna hlaska
		if (!GuiMethods.toCreatingStepProceed(GlobalParameters.primaryStage))
			return;
		// pokial sa to potvrdilo
		goToCreatingTab();
		// vymazat vsetky routre

		// nastavit selectedPath na nulu
		GlobalParameters.accidentCanvas.setSelectedAccident(null);
		GuiPainting.selectedRoute = -1;

		GuiMethods.clearRoutersAndDistances();
		GuiPainting.createZoomedCanvas();

	}

	public void toCreatingStepMouseEntered(MouseEvent evt) {
		GuiMethods.iconEntered(toCreatingStepIcon);
	}

	public void toCreatingStepMouseExited(MouseEvent evt) {
		GuiMethods.iconExited(toCreatingStepIcon);
	}

	public void toCreatingStepPressed(MouseEvent evt) {
		GuiMethods.iconPressed(toCreatingStepIcon);

	}

	public void toCreatingStepReleased(MouseEvent evt) {
		GuiMethods.iconReleased(toCreatingStepIcon);
	}

	public void toWorkingtepMouseClicked(MouseEvent evt) {

		goToWorkTab();

		// vykresli
		GuiPainting.createZoomedCanvas();

	}

	public void toWorkingStepMouseEntered(MouseEvent evt) {
		GuiMethods.iconEntered(toWorkingStepIcon);
	}

	public void toWorkingStepMouseExited(MouseEvent evt) {
		GuiMethods.iconExited(toWorkingStepIcon);
	}

	public void toWorkingStepPressed(MouseEvent evt) {
		GuiMethods.iconPressed(toWorkingStepIcon);

	}

	public void toWorkingStepReleased(MouseEvent evt) {
		GuiMethods.iconReleased(toWorkingStepIcon);
	}

	public void deleteButtonHover(MouseEvent evt) {
		Tooltip t = new Tooltip("Delete");
		Tooltip.install(deleteButton, t);
		// GuiMethods.iconEntered(settingsIcon);
	}

	public void netButtonHover(MouseEvent evt) {
		Tooltip t = new Tooltip("Grid");
		Tooltip.install(netButton, t);
		// GuiMethods.iconEntered(settingsIcon);
	}

	public void meterButtonHover(MouseEvent evt) {
		Tooltip t = new Tooltip("Ruler");
		Tooltip.install(meterButton, t);
		// GuiMethods.iconEntered(settingsIcon);
	}

	public static GraphicsContext getGc() {
		return gc;
	}

	/*
	 * public static Canvas getMainCanvas() { return mainCreatingCanvas; }
	 */
	// /////////////////////////////
	// canvas
	public void reckSelected(MouseEvent evt) {
		// potrebujem zistit kam sa kliklo a ci to bol nejaky regal
		ReckCanvas rc = GlobalParameters.reckCanvas;
		int xClicked = (int) evt.getX();
		int yClicked = (int) evt.getY();
		for (int i = 0; i < rc.getRecks().size(); i++) {
			ReckCrate reck = new ReckCrate(rc.getRecks().get(i));
			if (reck.isThisReck(xClicked, yClicked)) {
				// pozrem najskor ci je uz vybraty - ak ano, rusim vybraty
				if (reck.reck == rc.getSelectedReck()) {
					rc.setSelectedReck(null);
					rc.paintCanvas();
					// musim vykreslit aj normalny canvas znovu aby sa zrusil
					// vybraty regal aj z hlavneho canvasu
					GuiPainting.createZoomedCanvas();
					return;
				}
				// ak sa kliklo pravym tlacitkom regal by sa mal zmazat
				// najskor validacna hlaska
				if (evt.getButton() == MouseButton.SECONDARY) {

					// musim posunut vsetky ostatne o poziciu vymazaneho a
					// zmensit canvas
					/*
					 * MainValidationGui gui = new MainValidationGui();
					 * ValidationController.msg = "Remove reck?"; gui.start(new
					 * Stage());
					 */

					/*
					 * DialogResponse response = Dialogs.showConfirmDialog(
					 * GlobalParameters.primaryStage,
					 * "Do you want to continue?", "Remove rack", ""); if
					 * (response != DialogResponse.YES) return;
					 */
					Action response = Dialogs.create()
							.owner(GlobalParameters.primaryStage).title("")
							.masthead("Remove rack")
							.message("Do you want to continue?").showConfirm();

					if (response != Dialog.ACTION_YES)
						return;

					GlobalParameters.reckCanvas.removeReck(reck);
					rc.paintCanvas();
					return;
				}
				// je to tento regal, nastavim na prave vybraty
				rc.setSelectedReck(reck.reck);
				// vynulujem iny object ak je vybraty
				GlobalParameters.otherCanvas.setSelectedObject(null);
				GlobalParameters.otherCanvas.paintCanvas();
				// vykreslim
				rc.paintCanvas();
				// vratim aby sa nepozeralo zbytocne dalej
				return;
			}
		}

	}

	public void otherObjectSelected(MouseEvent evt) {
		// potrebujem zistit kam sa kliklo a ci to bol nejaky regal
		OtherCanvas oc = GlobalParameters.otherCanvas;
		int xClicked = (int) evt.getX();
		int yClicked = (int) evt.getY();
		for (int i = 0; i < oc.getObjects().size(); i++) {
			OtherObjectCrate object = new OtherObjectCrate(oc.getObjects().get(
					i));
			if (object.isThisObject(xClicked, yClicked)) {
				// pozrem najskor ci je uz vybraty - ak ano, rusim vybraty
				if (object.object == oc.getSelectedObject()) {
					oc.setSelectedObject(null);
					oc.paintCanvas();
					// musim vykreslit aj normalny canvas znovu aby sa zrusil
					// vybraty regal aj z hlavneho canvasu
					GuiPainting.createZoomedCanvas();
					return;
				}
				// je to tento regal, nastavim na prave vybraty
				oc.setSelectedObject(object.object);
				// vynulujem regal ak je vybraty
				GlobalParameters.reckCanvas.setSelectedReck(null);
				GlobalParameters.reckCanvas.paintCanvas();
				// vykreslim
				oc.paintCanvas();
				// vratim aby sa nepozeralo zbytocne dalej
				return;
			}
		}

	}

	public void AccidentObjectSelected(MouseEvent evt) {

		

		// potrebujem zistit kam sa kliklo a ci to bol nejaky regal
		AccidentCanvas oc = GlobalParameters.accidentCanvas;
		int xClicked = (int) evt.getX();
		int yClicked = (int) evt.getY();
		for (int i = 0; i < oc.getObjects().size(); i++) {
			AccidentCrate object = new AccidentCrate(oc.getObjects().get(i));
			if (object.isThisObject(xClicked, yClicked)) {
				// pozrem najskor ci je uz vybraty - ak ano, rusim vybraty
				if (object.object == oc.getSelectedAccident()) {
					oc.setSelectedAccident(null);
					oc.paintCanvas();
					// musim vykreslit aj normalny canvas znovu aby sa zrusil
					// vybraty regal aj z hlavneho canvasu
					GuiPainting.createZoomedCanvas();
					return;
				}
				// je to tento regal, nastavim na prave vybraty
				oc.setSelectedAccident(object.object);
				// vykreslim
				oc.paintCanvas();
				// vratim aby sa nepozeralo zbytocne dalej
				return;
			}
		}

	}

	public void scrollZoom(ScrollEvent evt) {
		System.out.println("x " + evt.getDeltaX());
		System.out.println("y " + evt.getDeltaY());
		if (evt.getDeltaY() < 0) {
			if (zoomRatio - 1 <= 0) {
				System.out.println("neda sa");
				return;
			}
			zoomRatio = (zoomRatio - 1);

		} else {
			zoomRatio = (zoomRatio + 1);
		}
		GuiPainting.createZoomedCanvas();
	}

	public static Vector<ImageView> getInitialIcons() {
		return initialIcons;
	}

	public static Vector<MenuItem> getInitialMenuIcons() {
		return initialMenuItems;
	}

	public static void enableMenu() {
		for (int i = 0; i < initialMenuItems.size(); i++) {
			initialMenuItems.get(i).setDisable(false);
		}
	}

	public void clickWorkCanvas(MouseEvent evt) {
		int xInCanvas = GuiMethods.getClickedFieldX(evt.getX());
		int yInCanvas = GuiMethods.getClickedFieldY(evt.getY());
		int xInField = GuiMethods.getClickedIndexOfFieldX(xInCanvas);
		int yInField = GuiMethods.getClickedIndexOfFieldY(yInCanvas);
		AbstractStockCell c;
		try {
			c = (CoridorCell) stock.getFieldIndex(xInField, yInField);
		} catch (Exception e) {
			System.out.println("clicked not to coridor");
			if (stock.getFieldIndex(xInField, yInField).getClass() == RouterCell.class) {
				c = (RouterCell) stock.getFieldIndex(xInField, yInField);
			} else
				return;
		}
		if (evt.getButton() == MouseButton.SECONDARY) {

			GlobalParameters.routeShow.clear();
			return;

		} else {
			GlobalParameters.routeShow.add(c);
		}
	}

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		GlobalParameters.newIcon = newIcon;
		GlobalParameters.openIcon = openIcon;
		GlobalParameters.undoIcon = undoIcon;
		GlobalParameters.redoIcon = redoIcon;
		GlobalParameters.toCreatingStepIcon = toCreatingStepIcon;
		GlobalParameters.toWorkingStepIcon = toWorkingStepIcon;
		GlobalParameters.deleteButton = deleteButton;
		GlobalParameters.meterButton = meterButton;
		GlobalParameters.netButton = netButton;
		GlobalParameters.mainCreatingCanvas = mainCreatingCanvas;
		GlobalParameters.mainMaintenanceCanvas = mainMaintenanceCanvas;
		GlobalParameters.mainWorkingCanvas = mainWorkingCanvas;
		GlobalParameters.undo = undo;
		GlobalParameters.redo = redo;
		GlobalParameters.mainScrollPane = mainScrollPane;
		GlobalParameters.metricButton = metricButton;
		GlobalParameters.metricTextField = metricTextField;
		GlobalParameters.creatingTab = creatingTab;
		GlobalParameters.maintenanceTab = maintenanceTab;
		GlobalParameters.workTab = workTab;
		GlobalParameters.tabPane = tabPane;
		GlobalParameters.menuBar = menuBar;
		GlobalParameters.progressLabel = progressLabel;
		GlobalParameters.progressRT = progressRT;
		GlobalParameters.cancelProgress = cancelProgress;

		GuiPainting.createReckCanvas(reckCanvas);
		GuiPainting.createOtherCanvas(otherCanvas);
		GuiPainting.createAccidentCanvas(accidentCanvas);

		initialIcons.add(redoIcon);
		initialIcons.add(undoIcon);
		initialIcons.add(saveAsIcon);
		initialIcons.add(saveIcon);
		initialIcons.add(zoomInIcon);
		initialIcons.add(zoomOutIcon);
		initialIcons.add(settingsIcon);
		initialIcons.add(nextStepIcon);
		// menuitems
		initialMenuItems.add(redo);
		initialMenuItems.add(undo);
		initialMenuItems.add(save);
		initialMenuItems.add(saveAs);
		initialMenuItems.add(options);

		for (int i = 0; i < initialMenuItems.size(); i++) {
			initialMenuItems.get(i).setDisable(true);
		}

		GuiMethods.iconsInitialSetDisable(initialIcons);

		// maintenance tab set disable
		maintenanceTab.setDisable(true);
		workTab.setDisable(true);

	}

	public void cancelProgressClicked(ActionEvent evt) {
		System.out.println("cancel progress bar");
		GlobalParameters.progressCanceled = true;
		goToCreatingTab();
		// nastavit selectedPath na nulu
		GlobalParameters.accidentCanvas.setSelectedAccident(null);
		GuiPainting.selectedRoute = -1;

		GuiMethods.clearRoutersAndDistances();
		GuiPainting.createZoomedCanvas();
	}

}
