package cz.spiffyk.flpmanager.application.screens.setup;

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

import org.apache.commons.lang3.SystemUtils;

import com.sun.javafx.scene.shape.PathUtils;

import cz.spiffyk.flpmanager.AppConfiguration;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextField;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;

public class SetupDialog extends Dialog<Boolean> {
	
	private static final AppConfiguration appConfiguration = AppConfiguration.get();
	
	private static final String FL_EXE_NAME = "FL.exe";
	private static final String DEFAULT_TEMPLATE_NAME =
			"Data" + File.separator +
			"Projects" + File.separator +
			"Templates" + File.separator +
			"Minimal" + File.separator +
			"Empty" + File.separator +
			"Empty.flp";
	
	@FXML private TextField pathToFl;
	private File pathToFlFile;
	private final DirectoryChooser flDirChooser = new DirectoryChooser();
	{
		flDirChooser.setTitle("Select path to FL Studio");
	}
	
	@FXML private TextField pathToExe;
	private final FileChooser exeFileChooser = new FileChooser();
	{
		exeFileChooser.setTitle("Select path to FL Studio executable");
		exeFileChooser.setInitialDirectory(pathToFlFile);
	}
	
	@FXML private TextField pathToTemplate;
	private final FileChooser templateFileChooser = new FileChooser();
	{
		templateFileChooser.setTitle("Select path to the default template");
		templateFileChooser.setInitialDirectory(pathToFlFile);
	}
	
	@FXML private TextField pathToWorkspace;
	private final DirectoryChooser workspaceDirChooser = new DirectoryChooser();
	{
		workspaceDirChooser.setTitle("Select path to your workspace");
	}
	
	public SetupDialog() {
		super();
		this.setTitle("First time setup");
		
		final FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("SetupDialog.fxml"));
		loader.setController(this);
		this.setResultConverter(this::convertResult);
		
		try {
			this.setDialogPane((DialogPane) loader.load());
		} catch (IOException e) {
			e.printStackTrace();
			Platform.exit();
		}
	}
	
	private void updateFlPaths() {
		pathToExe.setText(new File(pathToFlFile, FL_EXE_NAME).getAbsolutePath());
		pathToTemplate.setText(new File(pathToFlFile, DEFAULT_TEMPLATE_NAME).getAbsolutePath());
	}
	
	@FXML private void initialize() {
		pathToWorkspace.setText(SystemUtils.getUserHome() + File.separator + "FLWorkspace");
	}
	
	@FXML private void changePathToFlText() {
		pathToFlFile = new File(pathToFl.getText());
		updateFlPaths();
	}
	
	@FXML private void setPathToFl() {
		File f = flDirChooser.showDialog(null);
		if (f != null) {
			pathToFlFile = f;
			pathToFl.setText(f.getAbsolutePath());
			
			updateFlPaths();
		}
	}
	
	@FXML private void setPathToExe() {
		File f = exeFileChooser.showOpenDialog(null);
		if (f != null) {
			pathToExe.setText(f.getAbsolutePath());
		}
	}
	
	@FXML private void setPathToTemplate() {
		File f = templateFileChooser.showOpenDialog(null);
		if (f != null) {
			pathToTemplate.setText(f.getAbsolutePath());
		}
	}
	
	@FXML private void setPathToWorkspace() {
		File f = workspaceDirChooser.showDialog(null);
		if (f != null) {
			pathToWorkspace.setText(f.getAbsolutePath());
		}
	}
	
	private Boolean convertResult(ButtonType buttonType) {
		if (buttonType == ButtonType.OK) {
			appConfiguration.setFlExecutablePath(pathToExe.getText());
			appConfiguration.setFlpTemplatePath(pathToTemplate.getText());
			appConfiguration.setWorkspacePath(pathToWorkspace.getText());
			return true;
		}
		
		return false;
	}
}

@@ -99,16 +99,45 @@
 ception;
+%0Aimport java.util.Collection;
 %0A%0Aimport
@@ -586,16 +586,65 @@
 Chooser;
+%0Aimport javafx.stage.FileChooser.ExtensionFilter;
 %0A%0Apublic
@@ -1463,32 +1463,343 @@
 (pathToFlFile);%0A
+%09%09exeFileChooser.getExtensionFilters().addAll(%0A%09%09%09%09new ExtensionFilter(%22Executable%22, %22*.exe%22, %22*.bat%22, %22*.sh%22),%0A%09%09%09%09new ExtensionFilter(%22Windows Executable%22, %22*.exe%22),%0A%09%09%09%09new ExtensionFilter(%22Batch file%22, %22*.bat%22),%0A%09%09%09%09new ExtensionFilter(%22Shell script%22, %22*.sh%22),%0A%09%09%09%09new ExtensionFilter(%22All files%22, %22*.*%22));%0A
 %09%7D%0A%09%0A%09@FXML priv
@@ -2027,16 +2027,174 @@
 lFile);%0A
+%09%09templateFileChooser.getExtensionFilters().addAll(%0A%09%09%09%09new ExtensionFilter(%22FL Studio project file%22, %22*.flp%22),%0A%09%09%09%09new ExtensionFilter(%22All files%22, %22*.*%22));%0A
 %09%7D%0A%09%0A%09@F

Add file filters to setup dialog