package org.josast.EntrySat.app;

import java.io.IOException;

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.EntrySat.Telemetry;
import org.josast.kaikai.generated.FrameTlmEntrySat;

public class DecodeTLM {

	private static final String Version = "0.01";
	Options options = new Options();
	HelpFormatter formatter = new HelpFormatter();

	public DecodeTLM() {

		// automatically generate the help statement

		// create Options object

		// add File option
		options.addOption("v", "verbose", false, "display information during the process");
		options.addOption("V", "Version", false, Version);
		Option FileOption = Option.builder("f").argName("file").hasArg().desc("Telemery file  ").build();
		options.addOption(FileOption);
		formatter.printHelp("DecodeTLM", options);

	}

	public void execute(String[] args) {
		CommandLineParser cmdLineParser = new DefaultParser();
		CommandLine commandLineGlobal;
		try {
			commandLineGlobal = cmdLineParser.parse(options, args);
			if (commandLineGlobal.hasOption("f")) {
				System.out.println("file to extract : " + commandLineGlobal.getOptionValue("f"));
				FrameTlmEntrySat frame;
				try {
					frame = FrameTlmEntrySat.fromFile(commandLineGlobal.getOptionValue("f"));
					Telemetry tlm = new Telemetry();
					tlm.setValue(frame.frameTlmEntrySat());

					System.out.println(tlm.toStringHeader());
					System.out.println(tlm.afficheTLM());

					System.out.println(tlm.getValueDecoded(Telemetry.SID));
					System.out.println(tlm.getValueDecoded(Telemetry.EPS_VBATT));

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		DecodeTLM decode = new DecodeTLM();
		decode.execute(args);
	}

}
