package org.josast.application.amicalsat.cli;

import org.josast.AX25.AX25Frame;
import org.josast.AX25.KissData;
import org.josast.AX25.KissFrame;
import org.josast.ModuleSoundModem.SoundModemClient;
import org.josast.ModuleSoundModem.SoundModemConfiguration;
import org.josast.amicalsat.Frame;
import org.josast.amicalsat.FrameFactory;
import org.josast.amicalsat.FrameTlmAmicalsat;
import org.josast.amicalsat.KissException;
import org.josast.amicalsat.kiss;

import io.kaitai.struct.ByteBufferKaitaiStream;

public class AmicalsatCli {

	private  String version = "0.1";
	
	byte[] cbuf = new byte[1000];

	public String getVersion() {
		return version;
	}


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

	
	public void execute()
	{
		FrameFactory ff = new FrameFactory();
		SoundModemConfiguration smconf = new SoundModemConfiguration("config");
		SoundModemClient sm = new SoundModemClient(smconf);
		
		
		while (true) {
			int nb = sm.receiveData(cbuf);
			KissFrame kf = new KissFrame();
			KissData kissData = kf.extactMessage(cbuf, nb);
			kiss k = new kiss(kissData.getData());
			System.out.println(kissData);

			FrameTlmAmicalsat cube=null;
			try {
				cube = new FrameTlmAmicalsat(new ByteBufferKaitaiStream(k.toRaw()));
			} catch (KissException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			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) {
		
		AmicalsatCli cli = new AmicalsatCli();
		
		System.out.println(cli.getVersion());
		cli.execute();

	}

}
