package fr.upem.net.udp;

import org.junit.Test;

import java.nio.ByteBuffer;
import java.util.Optional;

import static fr.upem.net.udp.ClientBetterUpperCaseUDP.decodeMessage;
import static fr.upem.net.udp.ClientBetterUpperCaseUDP.encodeMessage;
import static org.junit.Assert.*;

public class ClientBetterUpperCaseUDPTest {

    private static ByteBuffer byteBufferFromHexaString(String content,int size){
        ByteBuffer bb = ByteBuffer.allocate(size);
        putHexaString(bb,content);
        bb.flip();
        return bb;
    }

    private static ByteBuffer byteBufferFromHexaString(String content){
        return byteBufferFromHexaString(content,content.length()/2);
    }


    // put the bytes described by the content string in hexa in the ByteBuffer
    private static void putHexaString(ByteBuffer bb,String content){
        while (!content.isEmpty()) {
            bb.put((byte) Integer.parseInt(content.substring(0, 2), 16));
            content = content.substring(2);
        }
    }

    // get the hexa string representing the ByteBuffer workspace
    private static String getHexaString(ByteBuffer bb){
        StringBuilder sb = new StringBuilder();
        while (bb.hasRemaining()) {
            sb.append(String.format("%02X",bb.get()));
         }
         return sb.toString();
    }

    @Test
    public void decodeMessageBasic1()  {
        ByteBuffer bb = byteBufferFromHexaString("000000066C6174696E31612424242121E9");
        bb.compact();
        // bb contains encodeMessage("a$$$!!é","latin1");
        Optional<String> res = ClientBetterUpperCaseUDP.decodeMessage(bb);
        assertTrue(res.isPresent());
        assertEquals("a$$$!!é", res.get());
    }

    @Test
    public void decodeMessageBasic2() {
        ByteBuffer bb = byteBufferFromHexaString("000000066C6174696E31612424242121E9", 100);
        bb.compact();
        // bb contains encodeMessage("a$$$!!é","latin1");
        Optional<String> res = ClientBetterUpperCaseUDP.decodeMessage(bb);
        assertTrue(res.isPresent());
        assertEquals("a$$$!!é", res.get());
    }

    @Test
    public void decodeMessageBasic3() {
        ByteBuffer bb = byteBufferFromHexaString("000000055554462D3861E282AC");
        bb.compact();
        // bb contains encodeMessage("a€","UTF-8");
        Optional<String> res = ClientBetterUpperCaseUDP.decodeMessage(bb);
        assertTrue(res.isPresent());
        assertEquals("a€", res.get());
    }

    @Test
    public void decodeMessageWrongEncoding1() {
        ByteBuffer bb = byteBufferFromHexaString("000000066C6174696E31612424242121E9");
        bb.position(2);
        assertFalse(decodeMessage(bb).isPresent());
    }

    @Test
    public void decodeMessageWrongEncoding2() {
        ByteBuffer bb2 = byteBufferFromHexaString("FFFFFFFF6C6174696E31612424242121E9");
        bb2.compact();
        assertFalse(decodeMessage(bb2).isPresent());
    }

    @Test
    public void decodeMessageWrongEncoding3() {
        ByteBuffer bb3 = byteBufferFromHexaString("000000FF6C6174696E31612424242121E9");
        bb3.compact();
        assertFalse(decodeMessage(bb3).isPresent());
    }

    @Test
    public void decodeMessageWrongEncoding4() {
        ByteBuffer bb4 = byteBufferFromHexaString("00000006746174696E31612424242121E9");
        bb4.compact();
        assertFalse(decodeMessage(bb4).isPresent());
    }

    @Test
    public void encodeMessageBasic() throws Exception {
        ByteBuffer bb = encodeMessage("a$$$!!é", "latin1");
        bb.flip();
        assertEquals("000000066C6174696E31612424242121E9", getHexaString(bb));
    }

    @Test
    public void encodeMessageBasic2() throws Exception {
        ByteBuffer bb = encodeMessage("a€","UTF-8");
        bb.flip();
        assertEquals("000000055554462D3861E282AC",getHexaString(bb));

    }

}