/**
 * 
 */
package org.josast.amicalsat;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;

import org.josast.ModuleSoundModem.KissException;
import org.josast.ModuleSoundModem.kiss;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import io.kaitai.struct.ByteBufferKaitaiStream;

/**
 * @author christophe
 *
 */
class FrameFactoryTest {
	
	int cpt=0;
	String generateRepository="src/test/resources/kissfiles";
	byte[] buff = null;
	FrameTlmAmicalsat cube=null;

	/**
	 * @throws java.lang.Exception
	 */
	@BeforeAll
	static void setUpBeforeClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@AfterAll
	static void tearDownAfterClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@BeforeEach
	void setUp() throws Exception {
		


	}

	/**
	 * @throws java.lang.Exception
	 */
	@AfterEach
	void tearDown() throws Exception {
	}

	/**
	 * Test method for {@link org.josast.amicalsat.FrameFactory#creerFrame(java.lang.String, java.lang.String)}.
	 */
	@Test
	void testCreerFrame() {
		
		FrameFactory ff = new FrameFactory();

	        Path path = Paths.get(generateRepository);

	        DirectoryStream<Path> stream;
			try {
				stream = Files.newDirectoryStream(path);

	        try { 
	          Iterator<Path> iterator = stream.iterator();
	          while(iterator.hasNext()) {
	            Path p = iterator.next();
	            System.out.println(p); 
				byte [] kissbuff = Files.readAllBytes(p);
				kiss k = new kiss(kissbuff);
				buff =k.toRaw();
				 cube = new FrameTlmAmicalsat(new ByteBufferKaitaiStream(buff));
					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());
				
	          }
	        } catch (KissException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally { 
	          stream.close(); 
	        } 
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}

}
