package com.ericsson.eiffel.remrem.generate.cli;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.tomcat.util.http.fileupload.FileUtils;

import com.ericsson.eiffel.remrem.semantics.SemanticsService;
import com.ericsson.eiffel.remrem.shared.MsgService;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
 * @author evasiba
 *
 */
public class CLI {
	private Options options=null;

	public CLI() {
		options = createCLIOptions();
	}

	/**
	 * Creates the options needed by command line interface
	 * @return
	 */
	private static Options createCLIOptions() {
		Options options = new Options();
		options.addOption("h", "help", false, "show help.");
		options.addOption("f", "content_file", true, "message content file");
		options.addOption("t", "message_type", true, "message type, mandatory if -f is given");
//		options.addOption("r", "response_file", true, "file to store the response in");
		return options;
	}

	/**
	 * Prints the help for this application and exits.
	 * @param options
	 */
	private static void help(Options options) {
		// This prints out some help
		HelpFormatter formater = new HelpFormatter();
		formater.printHelp("java -jar", options);
		System.exit(0);
	}

	/**
	 * Parse the given arguments
	 * @param args
	 * @return
	 */
	public boolean parse(String[] args) {
		CommandLineParser parser = new DefaultParser(); 
		boolean startService = true;
		try {
			CommandLine commandLine = parser.parse(options, args);
			Option[] existingOptions = commandLine.getOptions(); 
			if (existingOptions.length > 0) {
				startService = false;
			}

			if (commandLine.hasOption("h")) {
				help(options);
			}
			
			if (commandLine.hasOption("f") && commandLine.hasOption("t")) {
				String filePath = commandLine.getOptionValue("f");
				String msgType = commandLine.getOptionValue("t");
				handleContentFile(msgType, filePath);
			}
		} catch (Exception e) {
			help(options);
		}
		return startService;
	}
	
	public void handleContentFile(String msgType, String filePath) {
		JsonParser parser = new JsonParser();
		MsgService msgService = new SemanticsService();
		try {
			byte[] fileBytes = Files.readAllBytes(Paths.get(filePath));
			String fileContent = new String(fileBytes);
			JsonObject bodyJson = parser.parse(fileContent).getAsJsonObject();
			JsonElement returnJson = parser.parse(msgService.generateMsg(msgType, bodyJson));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

@@ -188,32 +188,60 @@
 utStreamReader;%0A
+import java.io.PrintWriter;%0A
 import java.nio.
@@ -705,32 +705,61 @@
 red.MsgService;%0A
+import com.google.gson.Gson;%0A
 import com.googl
@@ -1358,18 +1358,16 @@
 iven%22);%0A
-//
 %09%09option
@@ -1436,16 +1436,42 @@
 ponse in
+, mandatory if -f is given
 %22);%0A%09%09re
@@ -2268,16 +2268,51 @@
 ion(%22t%22)
+ &&%0A%09%09%09%09%09commandLine.hasOption(%22r%22)
 ) %7B%0A%09%09%09%09
@@ -2358,24 +2358,87 @@
 Value(%22f%22);%0A
+%09%09%09%09String responseFilePath = commandLine.getOptionValue(%22r%22);%0A
 %09%09%09%09String m
@@ -2518,16 +2518,34 @@
 filePath
+, responseFilePath
 );%0A%09%09%09%7D%0A
@@ -2682,16 +2682,50 @@
 filePath
+,%0A%09%09%09%09%09%09%09%09%09String responseFilePath
 ) %7B%0A%09%09Js
@@ -3083,16 +3083,203 @@
 Json));%0A
+%09%09%09Gson gson = new Gson();%0A%09%09%09String returnJsonStr = gson.toJson(returnJson);%0A%09%09%09try(  PrintWriter out = new PrintWriter( responseFilePath )  )%7B%0A%09%09%09    out.println( returnJsonStr );%0A%09%09%09%7D%0A
 %09%09%7D catc

add file to write json response