package org.josast.app.amicalsat.cli.option;

import java.util.Vector;
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;
import org.josast.app.amicalsat.cli.AmicalsatCliConf;

public class OptionsAnalysis {
	private Logger log = Logger.getLogger(getClass().getName());
	Vector<Cmd> listeCmd = null;
	Options options = null;
	CommandLine line = null;
	
	
	AmicalsatCliConf conf = new AmicalsatCliConf();

	public OptionsAnalysis(String[] args) {
		options = configParameters();
		CommandLineParser parser = new DefaultParser();

		try {
			line = parser.parse(options, args);
			listeCmd = interprete(line, args);
		} catch (ParseException e) {
			log.severe(e.toString());
		}
	}

	
	
	public Vector<Cmd> getCommandes() {
		return listeCmd;
	}



	public void setListeCmd(Vector<Cmd> listeCmd) {
		this.listeCmd = listeCmd;
	}



	private Options configParameters() {

		// -V Version

		final Option versionOption = Option.builder("V").longOpt("Version") //
				.desc("Return application version").hasArg(false).required(false).build();

		final Option helpFileOption = Option.builder("h").longOpt("help").desc("display help message").build();

		final Option configFileOption = Option.builder("C").longOpt("Config") //
				.desc("provide configuration file").hasArg(true).argName("configFile").required(false).build();

		final Option generateBinarieFileOption = Option.builder("SB").longOpt("SaveBinarie") //
				.desc("generate a file with all frame received in binarie (KISS)").hasArg(true).argName("FileName")
				.required(false).build();

		final Option generateTxtFileOption = Option.builder("ST").longOpt("SaveTextual") //
				.desc("generate a file with all frame received in textual format - data intepreated").hasArg(true)
				.argName("FileName").required(false).build();

		final Options options = new Options();

		options.addOption(versionOption);
		options.addOption(configFileOption);
		options.addOption(generateBinarieFileOption);
		options.addOption(generateTxtFileOption);
		options.addOption(helpFileOption);

		return options;

	}

	private Vector<Cmd> interprete(CommandLine line, String[] args) {

		Vector<Cmd> cmdList = new Vector<Cmd>();

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

		if (helpMode) {
// TODO : a mettre à jours
			final HelpFormatter formatter = new HelpFormatter();
			formatter.printHelp(args[0], options, true);
			System.exit(0);

		}

		boolean versionpMode = line.hasOption("Version");
		if (versionpMode) {
			cmdList.add(new VersionCmd());
		}

		boolean configMode = line.hasOption("SaveBinarie");
		if (configMode) {
			conf.setBinarieFile(line.getOptionValue("FileName", ""));

		}

		boolean generateMode = line.hasOption("SaveTextual");
		if (generateMode) {
			conf.setTextualFile(line.getOptionValue("FileName", ""));
		}

		cmdList.add(new receiveDataCmd());	
		return cmdList;
	}

}
