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

import java.io.IOException;
import java.io.PrintWriter;
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 com.ericsson.eiffel.remrem.semantics.SemanticsService;
import com.ericsson.eiffel.remrem.shared.MsgService;
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 the options this CLI can handle
     */
    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("json", "json_content", true, "json content");
        options.addOption("t", "message_type", true, "message type, mandatory if -f or -json is given");
        options.addOption("r", "response_file", true, "file to store the response in, optional");
        return options;
    }

    /**
     * Prints the help for this application and exits.
     * @param options the options to print usage help for
     */
    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 and act on them
     * @param args command line arguments
     * @return if the service should start or not
     */
    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("f") && commandLine.hasOption("t")) {
            	handleFileArgs(commandLine);
            } else if (commandLine.hasOption("json") && commandLine.hasOption("t")) {
            	handleJsonArgs(commandLine);
            }else {
                help(options);
            }
        } catch (Exception e) {
            help(options);
        }
        return startService;
    }
    
    private void handleFileArgs(CommandLine commandLine) {
    	String filePath = commandLine.getOptionValue("f");       
        String jsonContent = readJsonContent(filePath);
        handleJsonString(jsonContent, commandLine);
    }
    
    private String readJsonContent(String filePath) {
		try {
			JsonParser parser = new JsonParser();
			byte[] fileBytes = Files.readAllBytes(Paths.get(filePath));
	        return new String(fileBytes);	    
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    
    private void handleJsonArgs(CommandLine commandLine) {
    	String jsonContent = commandLine.getOptionValue("json");
    	handleJsonString(jsonContent, commandLine);
    }
    
    /**
     * Handle message from file
     * @param msgType the Eiffel message type
     * @param filePath the file path where the message content resides
     * @param responseFilePath the file path where to store the prepared message, stdout if null
     */
    private void handleJsonString(String jsonString,
    							  CommandLine commandLine) {
        
        MsgService msgService = new SemanticsService();
        String responseFilePath = null; 
        if (commandLine.hasOption("r"))
            responseFilePath = commandLine.getOptionValue("r");
        String msgType = commandLine.getOptionValue("t");
        try {
        	JsonParser parser = new JsonParser();
        	JsonObject jsonContent = parser.parse(jsonString).getAsJsonObject();
        	String returnJsonStr = msgService.generateMsg(msgType, jsonContent);
            if (responseFilePath != null) {
                try(  PrintWriter out = new PrintWriter( responseFilePath )  ){
                    out.println( returnJsonStr );
                }
            } else {
                System.out.println( returnJsonStr );
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
@@ -606,16 +606,361 @@
 r;%0A%0A/**%0A
+ * Class for interpreting the passed arguments from command line.%0A * Parse method returns true, meaning we need to start the service afterwards, if no argument%0A * is given. The same method returns false, meaning we do not start the service afterwards, if any%0A * argument is given. If an argument is given that it is not recognized we print help%0A
  * @auth
@@ -3053,32 +3053,151 @@
 ice;%0A    %7D%0A    %0A
+    /**%0A     * Reads the content from the given file and sends it to message service%0A     * @param commandLine%0A     */%0A
     private void
@@ -3331,28 +3331,28 @@
 ntent = read
-Json
+File
 Content(file
@@ -3413,32 +3413,121 @@
 ne);%0A    %7D%0A    %0A
+    /**%0A     * Read file content as String%0A     * @param filePath%0A     * @return%0A     */%0A
     private Stri
@@ -3533,20 +3533,20 @@
 ing read
-Json
+File
 Content(
@@ -3576,49 +3576,8 @@
 y %7B%0A
-%09%09%09JsonParser parser = new JsonParser();%0A
 %09%09%09b
@@ -3795,24 +3795,147 @@
 %0A    %7D%0A    %0A
+    /**%0A     * Read passed json string from command line and sends it to message service%0A     * @param commandLine%0A     */%0A
     private 
@@ -4122,31 +4122,52 @@
   * 
-Handle message from fil
+Send the given json string to message servic
 e%0A  

update class and method java doc