package org.josast.ModuleSoundModem;
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.josast.AX25.AX25Frame;
import org.josast.AX25.KissData;
import org.josast.AX25.KissFrame;
public class SoundModemClient {
	
	private Logger log = Logger.getLogger(getClass().getName());
	private FileOutputStream dataFile ;
	private SoundModemConfiguration smConfiguration = new SoundModemConfiguration("config.ini");
	private Socket skt=null;
	private  InputStream in =null;
	private boolean open =false;
	
	public SoundModemClient() {
        try {
			skt = new Socket(smConfiguration.getIPadress(), smConfiguration.getPort());
			in =  skt.getInputStream();
			openDataFile();
			open=true;
		} catch (IOException e) {
			log.log(Level.SEVERE, "create stream " + e.toString());
		}
		
	}
	
	public SoundModemClient( SoundModemConfiguration smConfiguration) {
        try {
			skt = new Socket(smConfiguration.getIPadress(), smConfiguration.getPort());
			in =  skt.getInputStream();
			openDataFile();
			open=true;
		} catch (IOException e) {
			log.log(Level.SEVERE, "create stream " + e.toString());
		}
        
		
	}
	
	public int receiveData (byte[] cbuf)
	{
		try {
			return in.read(cbuf);
		} catch (IOException e) {

			log.log(Level.SEVERE, "Error Read data " + e.toString());

			
			try {
				in.close();
			} catch (IOException e1) {
				log.log(Level.SEVERE, "Closing " + e1.toString());
			}
		}
		return 0;
		
	}
	
	

	
	
	 public AX25Frame decode (byte[] frame ,int nb)
	{
		writeFile(frame, nb);
		KissFrame kf=new KissFrame();
		KissData kissData = kf.extactMessage(frame, nb);

//		if (type==kf.AX25)
//		{
			AX25Frame ax= new AX25Frame();
			ax.decodeAX25Frame(kissData.getData(), kissData.getSize());
//		}
			System.out.println(kissData.toString());
			System.out.println(ax.toString());
			return ax;
		
	}

	
	
	 public static void main(String[] args) {
			// TODO Auto-generated method stub

			 byte[] cbuf= new  byte[1000];	
			 SoundModemClient sm = new SoundModemClient();
				int cpt=0;
			 System.out.println("client Sound Modem");
		         while(true)
		         {
		        


				int nb=sm.receiveData(cbuf);
				sm.decode(cbuf,nb);
	
				System.out.println("compteur"+cpt++);
		         }
		      
		   }

	public boolean isOpen() {
		return open;
	}
	
	private void writeFile(byte[] data, int nb)
	{
		try {
			dataFile.write(data, 0, nb);
			dataFile.write(1);
			dataFile.write(2);
			dataFile.write(3);
			dataFile.write(4);
			dataFile.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private void openDataFile()
	{
		Date aujourdhui = new Date();
		SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
		

		try {
			 dataFile = new FileOutputStream (formater.format(aujourdhui)+"-"+".bin.log");
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
}

