package seng302.Networking.MessageDecoders;

import seng302.Networking.Utils.ByteConverter;
import seng302.Networking.Messages.MarkRounding;

import java.util.Arrays;

/**
 * Created by hba56 on 23/04/17.
 */
public class MarkRoundingDecoder {
    byte messageVersionNumber;
    byte[] byteTime;
    byte[] byteAck;
    byte[] byteRaceID;
    byte[] byteSourceID;
    byte byteBoatStatus;
    byte byteRoundingSide;
    byte byteMarkType;
    byte byteMarkID;

    MarkRounding markRounding;

    public MarkRoundingDecoder(byte[] encodedMarkRounding) {
        messageVersionNumber = encodedMarkRounding[0];
        byteTime = Arrays.copyOfRange(encodedMarkRounding, 1, 7);
        byteAck = Arrays.copyOfRange(encodedMarkRounding, 7, 9);
        byteRaceID = Arrays.copyOfRange(encodedMarkRounding, 9, 13);
        byteSourceID = Arrays.copyOfRange(encodedMarkRounding, 13, 17);
        byteBoatStatus = encodedMarkRounding[17];
        byteRoundingSide = encodedMarkRounding[18];
        byteMarkType = encodedMarkRounding[19];
        byteMarkID = encodedMarkRounding[20];

        int intMsgVer = ByteConverter.bytesToInt(messageVersionNumber);
        long lngTime = ByteConverter.bytesToLong(byteTime);
        int intAck = ByteConverter.bytesToInt(byteAck);
        int intRaceID = ByteConverter.bytesToInt(byteRaceID);
        int intSourceID = ByteConverter.bytesToInt(byteSourceID);
        int intBoatState = ByteConverter.bytesToInt(byteBoatStatus);
        int intRoundingSide = ByteConverter.bytesToInt(byteRoundingSide);
        int intMarkType = ByteConverter.bytesToInt(byteMarkType);
        int intMarkID = ByteConverter.bytesToInt(byteMarkID);

        markRounding = new MarkRounding(intMsgVer, lngTime, intAck, intRaceID, intSourceID, intBoatState, intRoundingSide, intMarkType, intMarkID);
    }

    public MarkRounding getMarkRounding() {
        return markRounding;
    }
}
