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 = AmicalsatCliConf.getInstance();

	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(" Unrecognise option  " + e.toString());
			System.exit(0);
		}
	}

	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("CS").longOpt("ConfigSave") //
//				.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("FileNameBin")
				.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("FileNameTxt").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
			VersionCmd cmd = new VersionCmd();
			cmd.excute();
			final HelpFormatter formatter = new HelpFormatter();
			formatter.printHelp(args[0], options, true);
			System.exit(0);

		}
		cmdList.add(new VersionCmd());
		boolean versionpMode = line.hasOption("Version");
		if (versionpMode) {
			VersionCmd cmd = new VersionCmd();
			cmd.excute();
			System.exit(0);
		}

//		boolean confFile = line.hasOption("configFile");
//		if (confFile) {
//
//		}

		boolean binaryfile = line.hasOption("SaveBinarie");
		if (binaryfile) {
			conf.setSmBinarieOutputFile(line.getOptionValue("SaveBinarie", ""));
			conf.save();

		}

		boolean generateMode = line.hasOption("SaveTextual");
		if (generateMode) {
			conf.setTextualFile(line.getOptionValue("SaveTextual", ""));
			cmdList.add(new InitialiseTextualFileCmd());

		}

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

}
