package org.josast.CubesatSim;

import static org.junit.jupiter.api.Assertions.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.josast.CubesatSim.kaitai.generated.Cubesatsim;
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;

class CubesatSimeFrameTest {

    int cpt = 0;
    String generateRepository = "src/test/resources";
    String filetest = "src/test/resources/CubesatSim.bin";
    byte[] buff = null;
    Cubesatsim cube = null;

    @BeforeAll
    static void setUpBeforeClass() throws Exception {
    }

    @AfterAll
    static void tearDownAfterClass() throws Exception {
    }

    @BeforeEach
    void setUp() throws Exception {
    }

    @AfterEach
    void tearDown() throws Exception {
    }

    @Test
    void test() {

        Path p = Paths.get(filetest);
        byte[] kissbuff;
        try {
            kissbuff = Files.readAllBytes(p);

            kiss k = new kiss(kissbuff);
            try {
                buff = k.toRaw();
            } catch (KissException e) {
                e.printStackTrace();
            }
            cube = new Cubesatsim(new ByteBufferKaitaiStream(buff));
            System.out.print("Dest Adress : "
                    + cube.ax25Header().destAddress().destAddressStr());
            System.out.print("Dest SSID :" + cube.ax25Header().destSsid());
            System.out.print("SRC Adress : "
                    + cube.ax25Header().srcAddress().srcAddressStr());
            System.out.print("SRC SSID : " + cube.ax25Header().srcSsid());
            System.out.println("CTRL : " + cube.ax25Header().ctrl());
            assertEquals(3, cube.ax25Header().ctrl());
            assertEquals(1, cube.ax25Header().srcSsid());
            assertEquals(1, cube.ax25Header().destSsid());
            assertEquals("CQ    ",
                    cube.ax25Header().destAddress().destAddressStr());
            assertEquals("F4KLK ",
                    cube.ax25Header().srcAddress().srcAddressStr());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
