package cz.net21.ttulka.json.mock.generator;

import java.io.Console;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Entry point of the application.
 * 
 * @author ttulka
 */
public class App {
	
	static final String OUTPUT_DEFAULT = "output.json";
	
	public static void main(String[] argv) {
		final Console console = System.console();
		
		String conf = null;
		while (conf == null || conf.isEmpty()) {
			conf = console.readLine("Configuration file: ");

			Path path = Paths.get(conf);
			if (Files.notExists(path) || !Files.isRegularFile(path) || !Files.isReadable(path)) {
				System.err.println("Configuration file '" + conf + "' is not a readable file.");
				conf = null;
			}
		}
		Path confPath = Paths.get(conf);
		
		String output = console.readLine("Output JSON file (%s): ", OUTPUT_DEFAULT);
		if (output == null || output.isEmpty()) {
			output = OUTPUT_DEFAULT;
		}

		Path outputPath = Paths.get(output);
		if (Files.exists(outputPath) && !Files.isWritable(outputPath)) {
			System.err.println("Output file '" + output + "' is incorrect.");
			outputPath = Paths.get(OUTPUT_DEFAULT);
		}

		try {
			new Generator(confPath, outputPath).run();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
}

@@ -43,60 +43,8 @@
 r;%0A%0A
-import java.io.Console;%0Aimport java.io.IOException;%0A
 impo
@@ -312,52 +312,248 @@
 %7B%0A%09%09
-final Console console = System.console();%0A%09%09
+Path confPath = readConfigPath();%0A%09%09Path outputPath = readOutputPath(OUTPUT_DEFAULT);%0A%0A%09%09try %7B%0A%09%09%09new Generator(confPath, outputPath).run();%0A%09%09%7D%0A%09%09catch (Exception e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%7D%0A%0A%09private static Path readConfigPath() %7B
 %0A%09%09S
@@ -624,23 +624,32 @@
 %09conf = 
+System.
 console
+()
 .readLin
@@ -914,44 +914,146 @@
 %7D%0A%09%09
-Path confPath = Paths.get(conf);%0A%09%09%0A
+return Paths.get(conf);%0A%09%7D%0A%0A%09private static Path readOutputPath(String defaultOutputPath) %7B%0A%09%09Path path = null;%0A%09%09while (path == null) %7B%0A%09
 %09%09St
@@ -1066,23 +1066,32 @@
 utput = 
+System.
 console
+()
 .readLin
@@ -1119,33 +1119,37 @@
 %25s): %22, 
-OUTPUT_DEFAULT
+defaultOutputPath
 );%0A
+%09
 %09%09if (ou
@@ -1183,24 +1183,25 @@
 pty()) %7B%0A%09%09%09
+%09
 output = OUT
@@ -1201,43 +1201,37 @@
 t = 
-OUTPUT_DEFAULT
+defaultOutputPath
 ;%0A
+%09
 %09%09%7D%0A%0A%09%09
-Path outputP
+%09p
 ath 
@@ -1253,16 +1253,17 @@
 put);%0A%09%09
+%09
 if (File
@@ -1275,22 +1275,47 @@
 sts(
-outputP
+p
 ath) &&
+ (!Files.isRegularFile(path) %7C%7C
  !Fi
@@ -1333,23 +1333,19 @@
 ble(
-outputP
+p
 ath))
+)
  %7B%0A
+%09
 %09%09%09S
@@ -1399,181 +1399,72 @@
  is 
-incorrect.%22);%0A%09%09%09outputPath = Paths.get(OUTPUT_DEFAULT)
+not a writable file.%22);%0A%09%09%09%09path = null
 ;%0A
+%09
 %09%09%7D%0A
-%0A
 %09%09
-try %7B%0A%09%09%09new Generator(confPath, outputPath).run();%0A%09%09%7D%0A%09%09catch (IOException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D
+%7D%0A%09%09return path;
 %0A%09%7D%0A

check if the output file is correct