package seng302.Networking;

import org.junit.Assert;
import org.junit.Test;
import seng302.Networking.Exceptions.InvalidMessageException;
import seng302.Networking.MessageDecoders.XMLMessageDecoder;
import seng302.Networking.MessageEncoders.XMLMessageEncoder;
import seng302.Networking.Messages.AC35Data;
import seng302.Networking.Messages.Enums.MessageType;
import seng302.Networking.Messages.XMLMessage;

import java.io.*;

/**
 * Created by hba56 on 21/04/17.
 */
public class BinaryMessageDecoderTest {
    @Test
    public void decodeTest(){
        try{
            StringBuilder xmlString;
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    this.getClass().getResourceAsStream(("../../raceXML/Regatta.xml"))));
            String line;
            xmlString = new StringBuilder();
            while((line=br.readLine())!= null){
                xmlString.append(line.trim());
            }
            long time = System.currentTimeMillis();
            XMLMessageEncoder testEncoder = new  XMLMessageEncoder((short)1, time, (byte)7, (short)1, (short)xmlString.length(), xmlString.toString());

            byte[] encodedMessage = testEncoder.encode();

            BinaryMessageEncoder testMessage = new BinaryMessageEncoder(MessageType.XMLMESSAGE, time, 1, (short)encodedMessage.length, encodedMessage);

            BinaryMessageDecoder decoder = new BinaryMessageDecoder(testMessage.getFullMessage());

            AC35Data message = null;
            try {
                message = decoder.decode();
            }
            catch (InvalidMessageException e) {
                Assert.assertFalse(e.getMessage(), true);
            }

            if (!(message instanceof XMLMessage)){
                Assert.assertFalse(true);
            }
            XMLMessage xmlMessage = (XMLMessage) message;


            //message length
            Assert.assertEquals((short) encodedMessage.length, decoder.getMessageBodyLength());
            //time stamp
            Assert.assertEquals(time, decoder.getHeaderTimeStamp());
            //source ID
            Assert.assertEquals((short) 1, decoder.getHeaderSourceID());
            //message type
            Assert.assertEquals(26, decoder.getHeaderMessageType());

            XMLMessageDecoder decoderXML = new XMLMessageDecoder(decoder.getMessageBody());
            decoderXML.decode();

            //tests from seng302.Networking.MessageDecoders.XMLMessageDecoderTest to make sure the file is still good
            Assert.assertEquals((byte)1, decoderXML.getMessageVersionNumber());
            Assert.assertEquals((short)1, decoderXML.getAckNumber());
            Assert.assertEquals(time, decoderXML.getTimeStamp());
            Assert.assertEquals((byte)7, decoderXML.getXmlMsgSubType());
            Assert.assertEquals((short)1, decoderXML.getSequenceNumber());
            Assert.assertEquals((short)xmlString.length(), decoderXML.getXmlMsgLength());

//            Reader reader = decoderXML.getXmlMessageInputStream().getCharacterStream();
//            int c;
//            String contents = "";
//            while((c = reader.read()) != -1) {
//                contents += (char)c;
//            }
//            Assert.assertEquals(xmlString.toString(), contents);

        }catch (IOException e){
            Assert.assertFalse(e.getMessage(), true);
        }
    }
}
