package cz.vutbr.feec.gui;

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

import cz.vutbr.feec.graph.Path;
import cz.vutbr.feec.graph.Router;
import cz.vutbr.feec.libraryClasses.GraphCreatingMethods;
import cz.vutbr.feec.libraryClasses.GuiMethods;
import cz.vutbr.feec.libraryClasses.SearchingMethods;
import cz.vutbr.feec.project.GlobalParameters;
import cz.vutbr.feec.stockElements.Stock;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.control.CheckBox;
/*
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 SettingsWindow {

	private Stock s = Stock.getInstance();
	String msResult = null;
	String factorResult = null;

	
	
	
	public SettingsWindow(String meterPerSecond, String factor2) {
		GridPane grid = new GridPane();
		grid.setHgap(10);
		grid.setVgap(10);
		grid.setPadding(new Insets(0, 10, 0, 10));
		final TextField ms = new TextField();
	//	final TextField factor = new TextField();
		final CheckBox skelet = new CheckBox();
		ms.setText(meterPerSecond);
	//	factor.setText(factor2);
		

		grid.add(new Label("Speed:"), 0, 0);
		grid.add(ms, 1, 0);
	//	grid.add(new Label("Factor:"), 0, 1);
	//	grid.add(factor, 1, 1);
		grid.add(new Label("Skeleton images:"), 0, 1);
		grid.add(skelet, 1, 1);
		skelet.setSelected(GlobalParameters.isSkeletonImage);
		/*
		
		Callback myCallback = new Callback() {

			@Override
			public Object call(Object param) {
				// TODO Auto-generated method stub
				msResult = ms.getText();
				factorResult = factor.getText();
				if(skelet.isSelected()){
					GlobalParameters.isSkeletonImage = true;
				}
				else{
					System.out.println("false");
					GlobalParameters.isSkeletonImage = false;
				}
				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 parameters for computing in warehouse", "Settings",
				DialogOptions.OK_CANCEL, myCallback);
		System.out.println("Custom Dialog: User clicked: " + resp);

		if (resp == DialogResponse.OK) {
		*/
		Dialog dlg = new Dialog(GlobalParameters.primaryStage, "Settings");
	    dlg.setMasthead("Set parameters for computing in warehouse");
	    dlg.setContent(grid);
	    dlg.getActions().addAll(Dialog.ACTION_OK, Dialog.ACTION_CANCEL);
	    Action response = dlg.show();
	
	    if (response == Dialog.ACTION_OK) {
	   
	    	msResult = ms.getText();
			//factorResult = factor.getText();
			if(skelet.isSelected()){
				GlobalParameters.isSkeletonImage = true;
			}
			else{
				System.out.println("false");
				GlobalParameters.isSkeletonImage = false;
			}
	    
			System.out.println("Custom Dialog: Fields set from custom dialog: "
					+ msResult + "/" + factorResult );
			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 msText;
		String factorText;
		int currentMS = GlobalParameters.meterPerSecond;
		int tmpMS;
		//double factor;
		
		try {
			tmpMS = Integer.parseInt(msResult);
			if (tmpMS <= 0) {
				msText = "Put positive number";
				new SettingsWindow(msText, factorResult);
				return;
			} 
		} catch (Exception e) {
			msText = "Put valid number";
			new SettingsWindow(msText, factorResult);
			return;
		}
		/*
		try {
			factor = Double.parseDouble(factorResult);
			if (factor <= 0) {
				factorText = "Put positive number";
				new SettingsWindow(msResult, factorText);
				return;
			}
		} catch (Exception e) {
			factorText = "Put valid number";
			new SettingsWindow(msResult, factorText);
			return;
		}
		*/
		// ak uz su vypocitane cesty, treba prepocitat metriku
		for (int i = 0; i < s.getPaths().size(); i++) {
			Path path = s.getPaths().get(i);
			int cost = path.getCost() * currentMS / tmpMS;
			path.setCost(cost);
			Router r1 = path.getFirstRouter();
			Router r2 = path.getSecondRouter();
			r1.setCost(r2, cost);
			r2.setCost(r1, cost);
		}
			// treba prepocitat RT
			GraphCreatingMethods.createDijkstraAndRT();
			//prepocitat aj bunkz
			SearchingMethods.cellSorting();
			System.out.println("zoradene");
			SearchingMethods.findDistances();
			GlobalParameters.meterPerSecond = tmpMS;
		//	GlobalParameters.factor = factor;
			GlobalParameters.isSaved = false;
			GuiMethods.setNameOfProject();
				
		}
	}
