/**
 *
 */
package org.josast.application.cubesatsim;

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.CubesatSim.TLM.ManageFrame;
import org.josast.CubesatSim.influxDB.DataCubesatSim;
import org.josast.CubesatSim.influxDB.InfluxDBInstance;
import org.josast.CubesatSim.influxDB.InfluxDatabaseConfig;
import org.josast.CubesatSim.kaitai.generated.Cubesatsim.CubesatsimAo7;
import org.josast.ModuleSoundModem.SoundModemClient;

/**
 * @author christophe
 *
 */
public class SoundmodemToInfluxDbCli {

    /** Application version. */
    private static String version = "1.2";
    /** configuration filename by default. */
    private static String configurationfile = "config.ini";
    /** Verbose mode diseable by default. */
    private boolean verboseMode = false;

    /**
     *
     */
    public SoundmodemToInfluxDbCli() {

    }

    private static Options configParameters() {

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

				.longOpt("Version") //

				.desc("Return application version")

				.hasArg(false)

				.required(false)

				.build();
		final Option verboseOption = Option.builder("v")

				.longOpt("verbose") //

				.desc("display  data received")

				.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 helpFileOption = Option.builder("h")

				.longOpt("help")

				.desc("display help message")

				.build();

		final Options options = new Options();

		options.addOption(versionOption);
		options.addOption(verboseOption);
		options.addOption(configFileOption);

		options.addOption(helpFileOption);

		return options;

	}

	/**
	 * @param conffile
	 */
	public void execute(final String conffile) {

		// connection to Sound Modem
		InfluxDBInstance database = InfluxDBInstance.getInstance();
		InfluxDatabaseConfig config = new InfluxDatabaseConfig(conffile);

		database.conect(config.getAdresse(), config.getLogin(), config.getPassword());
		database.createDataBase("CubesatSim");

		SoundModemClient sm = new SoundModemClient(configurationfile);

		// waiting data
		while (true) {
			byte[] temp = sm.receivedRawData();

			ManageFrame mngt = new ManageFrame(temp);

			if (verboseMode) {

				System.out.print(mngt.HeaderToString());

			} else {
				System.out.print(".");
			}

			CubesatsimAo7 ao7 = mngt.getCubesatsimAo7();

			DataCubesatSim datasim = new DataCubesatSim();
			datasim.setBatteryVoltage(ao7.channel3aVal());
			datasim.setTotalCurrent(ao7.channel1aVal());
			datasim.setpXCurent(ao7.channel1bVal());
			datasim.setmXcurrent(ao7.channel1cVal());
			datasim.setpYcurrent(ao7.channel1dVal());
			datasim.setmYcurrent(ao7.channel2aVal());
			datasim.setpZcurrent(ao7.channel2bVal());
			datasim.setmZcurrent(ao7.channel2cVal());
			datasim.setBaseTemperature(ao7.channel4aVal());

			datasim.writetoInfluxDB(database);
			if (verboseMode) {
				System.out.println(datasim);

			}

		}

		// decoding

		// write to data base

	}

    /**
     * Main function.
     * @param args command line option
     * @throws ParseException
     */
	public static void main(final String[] args) throws ParseException {

		final Options options = configParameters();

		final CommandLineParser parser = new DefaultParser();

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

		// 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(args[0] + version);

			System.exit(0);

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

		boolean configMode = line.hasOption("Config");
		boolean verboseMode = line.hasOption("verbose");
		if (configMode) {

			configurationfile = line.getOptionValue("Config", "");

		}
		SoundmodemToInfluxDbCli smd = new SoundmodemToInfluxDbCli();
		smd.setVerbose(verboseMode);
		smd.execute(configurationfile);
	}

	/**
	 * Set verbose mode.
	 * @param verbose
	 */
	public void setVerbose(final boolean verbose) {
		verboseMode = verbose;

	}

}
