/**
 * 
 */
package org.josast.ModuleSoundModem;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Logger;

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.commons.cli.ParseException;

/**
 * @author christophe
 *
 */
public class SoundModemCli {
	
	private Logger log = Logger.getLogger(getClass().getName());

	private static String version = "0.2";
	private String configurationfile = "defaultConfigFile.ini";
	/*
	 * Utilisation d'un répertoire par défaut 
	 */
	private String generateRepository = "SoundModemOuput";

	/**
	 * 
	 */
	public SoundModemCli() {

	}

	public String getConfigurationfile() {
		return configurationfile;
	}

	public void setConfigurationfile(String configurationfile) {
		this.configurationfile = configurationfile;
	}

	public String getGenerateRepository() {
		return generateRepository;
	}

	public void setGenerateRepository(String generateRepository) {
		this.generateRepository = generateRepository;
	}

	private static Options configParameters() {

		final Option versionOption = Option.builder("V")

				.longOpt("Version") //

				.desc("Return application version")

				.hasArg(false)

				.required(false)

				.build();

		final Option configFileOption = Option.builder("C")

				.longOpt("Config") //

				.desc("Return application version")

				.hasArg(true)

				.argName("configFile")

				.required(false)

				.build();

		final Option generateFileOption = Option.builder("G")

				.longOpt("Generate") //

				.desc("generate binary file for each frame received")

				.hasArg(true)

				.argName("GenerateRepository")

				.required(false)

				.build();

		final Option helpFileOption = Option.builder("h")

				.longOpt("help")

				.desc("display help message")

				.build();

		final Options options = new Options();

		options.addOption(versionOption);

		options.addOption(configFileOption);

		options.addOption(generateFileOption);

		options.addOption(helpFileOption);

		return options;

	}

	public void execute() {

		// use soudmodem configuration file 
	
		SoundModemClient sm = new SoundModemClient(configurationfile);
		
		// tant que connecté à SoundModem => reçoit des trames 
		int cpt = 0;
		Path path = Paths.get(generateRepository);
	       //if directory exists?
	        if (!Files.exists(path)) {
	            try {
	                Files.createDirectories(path);
	            } catch (IOException e) {
	               //fail to create directory
	                e.printStackTrace();
	            }
	        }

		
		while (true) {

			
			byte[] temp =	sm.receivedKissData();


			try {
				Files.write(Paths.get(generateRepository + "\\Frame_" + cpt+++".bin"), temp);
			} catch (IOException e) {
				log.severe(e.toString());
			}
			System.out.println("compteur" + cpt++);
		}

	}

	/**
	 * @param args
	 * @throws ParseException
	 */
	public static void main(String[] args) throws ParseException {

		final Options options = configParameters();

		final CommandLineParser parser = new DefaultParser();

		final CommandLine line = parser.parse(options, args);

		SoundModemCli smc = new SoundModemCli();
		
		System.out.println(SoundModemCli.class.getSimpleName() + " " + version);

		// Si mode aide

		boolean helpMode = line.hasOption("help");

		if (helpMode) {

			final HelpFormatter formatter = new HelpFormatter();

			formatter.printHelp(args[0], options, true);

			System.exit(0);

		}

		boolean versionpMode = line.hasOption("Version");

		if (versionpMode) {

			System.out.println(SoundModemCli.class.getSimpleName() + " " + version);

			System.exit(0);

		}

		boolean configMode = line.hasOption("Config");

		if (configMode) {

			smc.setConfigurationfile(line.getOptionValue("Config", ""));

		}

		boolean generateMode = line.hasOption("Generate");

		if (generateMode) {

			smc.setGenerateRepository(line.getOptionValue("Generate", ""));

		}
		
		smc.execute();
	}

}
