package org.josast.CubesatSim.TLM;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;

import org.josast.TLM.TlmData;
import org.josast.TLM.xEqx;
import org.josast.kaitai.generated.Cubesatsim.CubesatsimAo7;

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

	public static final String PlusXCurent = "+X Current";
	public static final String PlusYCurent = "+Y Current";
	public static final String PlusZCurent = "+Z Current";
	public static final String MoinsXCurent = "-X Current";
	public static final String MoinsYCurent = "-Y Current";
	public static final String MoinsZCurent = "-Z Current";
	public static final String BatteryCurent = "Battery Current";

	public static final String BatteryVoltage = "Battery Voltage";
	public static final String Temperature = "Temperature";

	/**
	 * dataDescription list with description of each telemetry data
	 */
	private Hashtable<String, TlmData> dataDescription = new Hashtable<String, TlmData>();
	/**
	 * dataValue list to store value of 1 frame
	 */
	private Hashtable<String, Double> dataValue = new Hashtable<String, Double>();

	/**
	 *
	 */
	public Telemetry() {
		init();
	}

	/**
	 * return string with all decoded value of the frame
	 * 
	 * @return String with all decoded data, ";' is used as separator
	 */
	/**
	 * @return
	 */
	public String afficheTLM() {

		StringBuilder stringBuilder = new StringBuilder();
		Set<String> keys = dataDescription.keySet();

		// Obtaining iterator over set entries
		Iterator<String> itr = keys.iterator();
		String strvalue;
		// Displaying Key and value pairs
		while (itr.hasNext()) {
			// Getting Key
			strvalue = itr.next();

			TlmData tlm = dataDescription.get(strvalue);
			Double value = dataValue.get(strvalue);
			String valueS = String.format(tlm.getFormat(), value);
			stringBuilder.append(valueS);
			stringBuilder.append(";\t");
		}

		stringBuilder.append("\r\n");
		return stringBuilder.toString();
	}

	/**
	 * Compute the raw value with formulea
	 * 
	 * @param dataName
	 * @return value interpreted
	 */
	public double getValueDec(String dataName) {

		Double value = dataValue.get(dataName);
		return value;
	}

	/**
	 *
	 */
	private void init() {
		dataDescription.put(PlusXCurent, new TlmData(PlusXCurent, new xEqx(), "", "%.0f"));
		dataDescription.put(PlusZCurent, new TlmData(PlusZCurent, new xEqx(), "", "%.0f"));
		dataDescription.put(PlusYCurent, new TlmData(PlusYCurent, new xEqx(), "", "%.0f"));
		dataDescription.put(MoinsXCurent, new TlmData(MoinsXCurent, new xEqx(), "", "%.0f"));
		dataDescription.put(MoinsYCurent, new TlmData(MoinsYCurent, new xEqx(), "", "%.0f"));
		dataDescription.put(MoinsZCurent, new TlmData(MoinsZCurent, new xEqx(), "", "%.0f"));
		dataDescription.put(BatteryCurent, new TlmData(BatteryCurent, new xEqx(), "", "%.0f"));
		dataDescription.put(BatteryVoltage, new TlmData(BatteryVoltage, new xEqx(), "", "%.0f"));
		dataDescription.put(Temperature, new TlmData(Temperature, new xEqx(), "", "%.0f"));

	}

	/**
	 * From an Ax25Frame (generated by Kaitai), the function extract data and fill
	 * hastable with value
	 * 
	 * @param ax Ax25Frame
	 */
	public void setValue(CubesatsimAo7 CubesatsimData) {

		dataValue.put(PlusXCurent, CubesatsimData.channel1bVal().doubleValue());
		dataValue.put(PlusYCurent, CubesatsimData.channel1dVal().doubleValue());
		dataValue.put(PlusZCurent, CubesatsimData.channel2bVal());
		dataValue.put(MoinsXCurent, CubesatsimData.channel1cVal().doubleValue());
		dataValue.put(MoinsYCurent, CubesatsimData.channel2aVal().doubleValue());
		dataValue.put(MoinsZCurent, CubesatsimData.channel2cVal());
		dataValue.put(BatteryCurent, CubesatsimData.channel2dVal().doubleValue());
		dataValue.put(BatteryVoltage, CubesatsimData.channel3aVal());
		dataValue.put(Temperature, CubesatsimData.channel4aVal());
	}

	/**
	 * display list of data decoded
	 * 
	 * @return name of data decoded separated by ","
	 */
	public String toStringHeader() {
		StringBuilder stringBuilder = new StringBuilder();
		Set<String> keys = dataDescription.keySet();

		// Obtaining iterator over set entries
		Iterator<String> itr = keys.iterator();
		String strvalue;
		// Displaying Key and value pairs
		while (itr.hasNext()) {
			// Getting Key
			strvalue = itr.next();

			stringBuilder.append(strvalue);
			stringBuilder.append(";\t");
		}

		stringBuilder.append("\r\n");
		return stringBuilder.toString();

	}

}
