/**
 *
 */
package org.josast.CubesatSim.TLM;

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

import io.kaitai.struct.ByteBufferKaitaiStream;

/**
 * @author chris
 *
 */
public class SoundmodemToInfluxDB {

	private static String version = "1.1";
	private static String configurationfile = "config.ini";

	/**
	 *
	 */
	public SoundmodemToInfluxDB() {

	}

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

				.longOpt("help")

				.desc("display help message")

				.build();

		final Options options = new Options();

		options.addOption(versionOption);

		options.addOption(configFileOption);

		options.addOption(helpFileOption);

		return options;

	}

	/**
	 * @param configurationfile
	 * @param args
	 */
	public void execute(String configurationfile) {

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

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

		SoundModemClient sm = new SoundModemClient(configurationfile);

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

			System.out.println(cube.ax25Header().srcAddress().srcAddressStr());
			System.out.println(cube.ax25Header().destAddress().destAddressStr());
			System.out.println(cube.ax25Header().ctrl());
			CubesatsimData data = cube.frametype().ax25Info();
			CubesatsimAo7 AO7 = data.payload();

			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);
			System.out.println(datasim);

		}

		// decoding

		// write to data base

	}

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

		}

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

		if (configMode) {

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

		}
		SoundmodemToInfluxDB smd = new SoundmodemToInfluxDB();
		smd.execute(configurationfile);
	}

}
