package org.josast.EntrysatDecoder;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Date;
import java.util.Vector;

import org.josast.AX25.KissData;
import org.josast.kaikai.generated.FrameTlmEntrySat;
import org.josast.kaikai.generated.FrameTlmEntrySat.Ax25Frame;

import io.kaitai.struct.ByteBufferKaitaiStream;

public class ModelFrame {
	
	long numberFrame = 0;
	Vector<FrameTlmEntrySat> vect=new Vector<FrameTlmEntrySat>();
	Vector<EntryFrame> vecKiss =  new Vector<EntryFrame>();
	private PropertyChangeSupport listenerList = new PropertyChangeSupport(this);
	
	  public void addPropertyChangeListener(PropertyChangeListener listener) {
	      listenerList.addPropertyChangeListener(listener);
	  }
	  
	  public void removePropertyChangeListener(PropertyChangeListener listener) {
	      listenerList.removePropertyChangeListener(listener);
	  }
	  
	  public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
	      listenerList.addPropertyChangeListener(propertyName, listener);
	  }
	  
	  public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
	      listenerList.removePropertyChangeListener(propertyName, listener);
	  }
	  
	  
	
	public long getNumberFrame() {
		return numberFrame;
	}



	public FrameTlmEntrySat getLastFrame() {
		return vect.lastElement();
	}

	public EntryFrame getLastKissFrame() {
		return vecKiss.lastElement();
	}

	public void addFrame(int nb, byte[] cbuf) {
		long oldNumberFrame = numberFrame;
		numberFrame++;
		try {
			// try to decode 
			FrameTlmEntrySat frame = new FrameTlmEntrySat(new ByteBufferKaitaiStream (cbuf) );
			Ax25Frame ax = frame.frameTlmEntrySat();
			System.out.println(ax.ax25Header().srcCallsignRaw().srcCallsignRor().srcCallsign());
			vect.addElement(frame);
			EntryFrame EntryFrame = new EntryFrame(new Date(), new KissData(cbuf, nb));
			vecKiss.addElement(EntryFrame);
			listenerList.firePropertyChange("numberFrame", oldNumberFrame, numberFrame);
			
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}

	public String getFrame(int i) {
		
		return vecKiss.get(i).toString();
	}

}
