package org.josast.application.amicalsat.cli;

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.ModuleSoundModem.SoundModemCli;
import org.josast.ModuleSoundModem.SoundModemClient;
import org.josast.amicalsat.Frame;
import org.josast.amicalsat.FrameFactory;
import org.josast.amicalsat.FrameTlmAmicalsat;

import io.kaitai.struct.ByteBufferKaitaiStream;

public class AmicalsatCli {
	// private Logger log = Logger.getLogger(getClass().getName());

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



	public String getVersion() {
		return version;
	}

	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;
	}

	public AmicalsatCli() {
		// TODO Auto-generated constructor stub
	}

	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  file  frame received - not implemented")

				.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() {
		FrameFactory ff = new FrameFactory();

		SoundModemClient sm = new SoundModemClient(configurationfile);

		while (true) {

			byte[] temp = sm.receivedRawData();

			FrameTlmAmicalsat cube = new FrameTlmAmicalsat(new ByteBufferKaitaiStream(temp));

			System.out.print(
					"Source : " + cube.frameTlmEntrySat().ax25Header().srcCallsignRaw().srcCallsignRor().srcCallsign());
			System.out.print("destination : "
					+ cube.frameTlmEntrySat().ax25Header().destCallsignRaw().destCallsignRor().destCallsign());
			System.out.print("PID : " + cube.frameTlmEntrySat().payload().pid());
			System.out.println(" SSID : " + cube.frameTlmEntrySat().ax25Header().srcSsidRaw());
			String tlmArea = cube.frameTlmEntrySat().payload().tlmArea();
			String typeFrame = cube.frameTlmEntrySat().payload().tlmType();
			System.out.println(tlmArea + " " + typeFrame);
			Frame fm = ff.creerFrame(tlmArea, typeFrame);
			fm.setUp(cube);

			System.out.println(fm.toString());

		}

	}

	public static void main(String[] args) throws ParseException {

		final Options options = configParameters();

		final CommandLineParser parser = new DefaultParser();

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

		AmicalsatCli cli = new AmicalsatCli();

		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() + " " + cli.getVersion());

			System.exit(0);

		}

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

		if (configMode) {

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

		}

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

		if (generateMode) {

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

		}
		System.out.println(cli.getVersion());
		cli.execute();

	}

}
