package org.stummi.factorio.gui;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

import org.stummi.factorio.luaconf.GamePathFinder;
import org.stummi.factorio.luaconf.LuaEntityLoader;

public class PlannerApplication extends Application {

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws IOException {
		File appDir = determineAppDir(stage);
		if (appDir == null) {
			// No Game directory could be determined. We cannot do anything
			// without it
			return;
		}

		LuaEntityLoader loader = new LuaEntityLoader(new File(
				System.getenv("HOME"),
				".steam/steam/SteamApps/common/Factorio/"));
		JFXImageFactory factory = new JFXImageFactory(
				loader.getResourceFactory());

		FactoryTable factoryTable = new FactoryTable(loader, factory);
		factoryTable.setEditable(true);

		ReportTable reportTable = new ReportTable(factory);

		Button addButton = new Button("Add");
		Button delButton = new Button("Delete");

		addButton.setOnAction(me -> factoryTable.newItem());
		delButton.setOnAction(me -> factoryTable.deleteSelected());
		factoryTable.addListener(me -> reportTable.setReport(factoryTable
				.getReport()));

		HBox hbox = new HBox(addButton, delButton);
		HBox vbox = new HBox(factoryTable, reportTable);
		BorderPane bp = new BorderPane(vbox);
		bp.setTop(hbox);

		Scene scene = new Scene(bp);
		scene.getStylesheets().add(
				PlannerApplication.class.getResource("/style.css")
						.toExternalForm());
		stage.setScene(scene);
		stage.show();
	}

	private File determineAppDir(Stage stage) {
		File file = GamePathFinder.findGameDir();
		if(file != null) {
			// We could find the game directory automatically
			return file;
		} 

		// no success with automatically file finding. Lets bother the User
		// with it
		Alert alert = new Alert(AlertType.CONFIRMATION);
		alert.getButtonTypes().setAll(ButtonType.YES, ButtonType.NO);
		alert.setTitle("Factorio Game Directory");
		alert.setHeaderText("The Game directory could not be found on your system");
		alert.setContentText("This tool needs a Factorio installation on your system to work\n"
				+ "But it couldn't determine it automatically.\nDo you want to choose a directory now?");
		Optional<ButtonType> button = alert.showAndWait();
		if (!button.isPresent() || button.get() != ButtonType.YES) {
			return null;
		}

		DirectoryChooser chooser = new DirectoryChooser();
		chooser.setTitle("Please Select the Game directory");
		Alert wrongDirectory = new Alert(AlertType.WARNING);
		wrongDirectory.setTitle("Wrong Directory");
		wrongDirectory
				.setHeaderText("This does not seem to be a Factorio Directory");
		wrongDirectory
				.setContentText("Hint: The Installation Directory contains a data\ndirectory which contains a core and base directory");

		do {
			file = chooser.showDialog(stage);
			if(file == null) {
				// user canceled
				return null;
			} else if (!GamePathFinder.isSuitable(file)) {
				wrongDirectory.showAndWait();
				chooser.setInitialDirectory(file);
			} else {
				return file;
			}
		} while(true);
	}
}
@@ -398,16 +398,53 @@
 t.HBox;%0A
+import javafx.scene.layout.Priority;%0A
 import j
@@ -893,17 +893,53 @@
 termined
-.
+ and user canceled the dialog. %0A%09%09%09//
  We cann
@@ -956,24 +956,21 @@
 hing
-%0A%09%09%09// without i
+ at this poin
 t%0A%09%09
@@ -1034,91 +1034,14 @@
 der(
-new File(%0A%09%09%09%09System.getenv(%22HOME%22),%0A%09%09%09%09%22.steam/steam/SteamApps/common/Factorio/%22)
+appDir
 );%0A%09
@@ -1665,16 +1665,67 @@
 Table);%0A
+%09%09HBox.setHgrow(factoryTable, Priority.ALWAYS);%0A%09%09%0A
 %09%09Border

Fix problem with determining the game directory on linux