package cz.vutbr.feec.gui;

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

import cz.vutbr.feec.libraryClasses.GuiMethods;
import cz.vutbr.feec.project.GlobalParameters;
import cz.vutbr.feec.stockElements.Stock;
import cz.vutbr.feec.transportObjects.PositionCrate;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
/*
import javafx.scene.control.Dialogs.DialogOptions;
import javafx.scene.control.Dialogs.DialogResponse;
import javafx.scene.control.Dialogs;
*/
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.util.Callback;

public class NewReckWindow {

	private Stock s = Stock.getInstance();
	String lengthResult = null;
	String widthResult = null;
	
	public NewReckWindow(String lengthText, String widthText) {
		
		
		GridPane grid = new GridPane();
		grid.setHgap(10);
		grid.setVgap(10);
		grid.setPadding(new Insets(0, 10, 0, 10));
		final TextField length = new TextField();
		final TextField width = new TextField();
		final TextField delimiter = new TextField();
		length.setText(lengthText);
		width.setText(widthText);
		

		grid.add(new Label("Length:"), 0, 0);
		grid.add(length, 1, 0);
		grid.add(new Label("Width:"), 0, 1);
		grid.add(width, 1, 1);
				/*
		Callback myCallback = new Callback() {

			@Override
			public Object call(Object param) {
				// TODO Auto-generated method stub
				lengthResult = length.getText();
				widthResult = width.getText();
				return null;
			}
		};
		*/
		/*
		 * @Override public Object call(Object param) { // TODO Auto-generated
		 * method stub lengthResult = length.getText(); widthResult =
		 * width.getText(); return null; } };
		 */
		/*
		DialogResponse resp = Dialogs.showCustomDialog(
				GlobalParameters.primaryStage, grid,
				"Set length and width of rack", "Create new rack",
				DialogOptions.OK_CANCEL, myCallback);
		System.out.println("Custom Dialog: User clicked: " + resp);

		if (resp == DialogResponse.OK) {
		*/
		Dialog dlg = new Dialog(GlobalParameters.primaryStage, "Create new rack");
	    dlg.setMasthead("Set length and width of rack");
	    dlg.setContent(grid);
	    dlg.getActions().addAll(Dialog.ACTION_OK, Dialog.ACTION_CANCEL);
	    Action response = dlg.show();
	
	    if (response == Dialog.ACTION_OK) {
	    	lengthResult = length.getText();
			widthResult = width.getText();
			
			validate();

		}
		// You must check the resp, since input fields' texts are returned
		// regardless of what button was pressed. (ie. If user clicked 'Cancel'
		// disregard the input)

	}

	private void validate() {

		String lengthText;
		String widthText;
		short delimiter = s.getDelimiter();
		int x;
		int y;
		
		try {
			int tmp = Integer.valueOf(lengthResult.replace(",", "."));
			// par osetreni
			if ((tmp < 0) || (tmp >= s.getLength())) {
				lengthText = "Invalid value";
				new NewReckWindow(lengthText, widthResult);
				return;
			} else if (tmp % delimiter != 0) {
				lengthText = "Has to be divided by: "+delimiter;
				new NewReckWindow(lengthText, widthResult);
				return;
			} else {
				x = (int) tmp;
			}
		} catch (NumberFormatException e) {
			lengthText = "Put correct number";
			new NewReckWindow(lengthText, widthResult);
			return;
		}
		try {
			int tmp = Integer.valueOf(widthResult.replace(",", "."));
			if ((tmp < 0) || (tmp >= s.getWidth())) {
				widthText = "Invalid value";
				new NewReckWindow(lengthResult, widthText);	
				return;
			} else if (tmp % delimiter != 0) {
				widthText = "Has to be divided by: "+delimiter;
				new NewReckWindow(lengthResult, widthText);	
				return;
			} else {
				y = tmp;
			}

		} catch (Exception e) {
			widthText = "Put correct value";
			new NewReckWindow(lengthResult, widthText);	
			return;
		}
		// po validaciach vytvorim novy regal
		// v konstruktoru je definovana poloha kam sa do canvasu ma vykreslit a
		// size policok, nie v metroch
		x = x / delimiter;
		y = y / delimiter;
		// zistujem volnu pozicu v canvasu
		PositionCrate position = GlobalParameters.reckCanvas.getFreePosition();
		ReckInCanvas reck = new ReckInCanvas(position.x, position.y, x, y,
				GlobalParameters.reckCanvas);
		// pridavam do reckCanvasu
		GlobalParameters.reckCanvas.addReck(reck);
		// vykreslujem
		GlobalParameters.reckCanvas.paintCanvas();
	}
}
