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.libraryClasses.GuiPainting;
import cz.vutbr.feec.project.GlobalParameters;
import cz.vutbr.feec.stockElements.Stock;
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.TextField;
import javafx.scene.layout.GridPane;
import javafx.util.Callback;

public class NewProjectWindow {

	private Stock s = Stock.getInstance();
	String lengthResult = null;
	String widthResult = null;
	String delimiterResult = null;

	public NewProjectWindow(String lengthText, String widthText,
			String delimiterText) {
		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);
		delimiter.setText(delimiterText);

		grid.add(new Label("Length:"), 0, 0);
		grid.add(length, 1, 0);
		grid.add(new Label("Width:"), 0, 1);
		grid.add(width, 1, 1);
		grid.add(new Label("Delimiter:"), 0, 2);
		grid.add(delimiter, 1, 2);

		/*
		Callback myCallback = new Callback() {

			@Override
			public Object call(Object param) {
				// TODO Auto-generated method stub
				lengthResult = length.getText();
				widthResult = width.getText();
				delimiterResult = delimiter.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 warehouse", "New Project",
				DialogOptions.OK_CANCEL, myCallback);
		System.out.println("Custom Dialog: User clicked: " + resp);

		if (resp == DialogResponse.OK) {
		*/
		
		Dialog dlg = new Dialog(GlobalParameters.primaryStage, "New Project");
	    dlg.setMasthead("Set length and width of warehouse");
	    dlg.setContent(grid);
	    dlg.getActions().addAll(Dialog.ACTION_OK, Dialog.ACTION_CANCEL);
	    Action response = dlg.show();
	
	    if (response == Dialog.ACTION_OK) {
			System.out.println("Custom Dialog: Fields set from custom dialog: "
					+ lengthResult + "/" + widthResult + "/" + delimiterResult);
			lengthResult = length.getText();
			widthResult = width.getText();
			delimiterResult = delimiter.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;
		String delimiterText;
		short delimiter;
		float length;
		float width;

		try {
			short tmp = Short.parseShort(delimiterResult);
			if (tmp <= 0) {
				delimiterText = "Put positive value";
				new NewProjectWindow(lengthResult, widthResult, delimiterText);
				return;
			} else {
				delimiter = tmp;
			}
		} catch (Exception e) {
			delimiterText = "Put correct number";
			new NewProjectWindow(lengthResult, widthResult, delimiterText);
			return;
		}

		try {
			float tmp = Float.parseFloat(lengthResult.replace(",", "."));
			if (tmp <= 0) {
				lengthText = "Put positive value";
				new NewProjectWindow(lengthText, widthResult, delimiterResult);
				return;
			} 
			else if (tmp % delimiter != 0) {
				lengthText = "Has to be divided by: "+delimiter;
				new NewProjectWindow(lengthText, widthResult, delimiterResult);
				return;
			}
			else {
				// pridanie dvoch policok velkosti delimitra kvoli okrajom
				length = tmp + (delimiter * 2);
			}
		} catch (NumberFormatException e) {
			lengthText = "Put correct number";
			new NewProjectWindow(lengthText, widthResult, delimiterResult);
			return;
		}
		try {
			float tmp = Float.parseFloat(widthResult.replace(",", "."));
			if (tmp <= 0) {
				widthText = "Put positive value";
				new NewProjectWindow(lengthResult, widthText, delimiterResult);
				return;
			} 
		 else if (tmp % delimiter != 0) {
			widthText = "Has to be divided by: "+delimiter;
			new NewProjectWindow(lengthResult, widthText, delimiterResult);
			return;
		}
		else {
				// pridanie dvoch policok velkosti delimitra kvoli okrajom
				width = tmp + (delimiter * 2);
			}
		} catch (Exception e) {
			widthText = "Put correct number";
			new NewProjectWindow(lengthResult, widthText, delimiterResult);
			return;
		}

		GuiMethods.clearRoutersAndDistances();
		s.setWidth(width);
		s.setLength(length);
		s.setDelimiter(delimiter);
		ControllerInitializeGui.goToCreatingTab();
		GlobalParameters.accidentCanvas.setSelectedAccident(null);
		GuiPainting.selectedRoute = -1;
		GuiMethods.createCanvas();
		
	}
}
