package seng302.Networking.MessageDecoders;

import seng302.Networking.Messages.BoatLocation;

import java.util.Arrays;

import static seng302.Networking.Utils.ByteConverter.*;

/**
 * Created by hba56 on 21/04/17.
 */
public class BoatLocationDecoder {
    private byte messageVersionNumber;
    private byte[] time;
    private byte[] sourceID;
    private byte[] seqNum;
    private byte deviceType;
    private byte[] latitude;
    private byte[] longitude;
    private byte[] altitude;
    private byte[] heading;
    private byte[] pitch;
    private byte[] roll;
    private byte[] boatSpeed;
    private byte[] cog;
    private byte[] sog;
    private byte[] apparentWindSpeed;
    private byte[] apparentWindAngle;
    private byte[] trueWindSpeed;
    private byte[] trueWindDirection;
    private byte[] trueWindAngle;
    private byte[] currentDrift;
    private byte[] currentSet;
    private byte[] rudderAngle;

    private BoatLocation message;

    public BoatLocationDecoder(byte[] encodedBoatLocation) {
        messageVersionNumber = encodedBoatLocation[0];
        time = Arrays.copyOfRange(encodedBoatLocation, 1, 7);
        sourceID = Arrays.copyOfRange(encodedBoatLocation, 7, 11);
        seqNum = Arrays.copyOfRange(encodedBoatLocation, 11, 15);
        deviceType = encodedBoatLocation[15];
        latitude = Arrays.copyOfRange(encodedBoatLocation, 16, 20);
        longitude = Arrays.copyOfRange(encodedBoatLocation,20, 24);
        altitude = Arrays.copyOfRange(encodedBoatLocation, 24, 28);
        heading = Arrays.copyOfRange(encodedBoatLocation,28, 30);
        pitch =Arrays.copyOfRange(encodedBoatLocation,30,32);
        roll = Arrays.copyOfRange(encodedBoatLocation,32,34);
        boatSpeed = Arrays.copyOfRange(encodedBoatLocation,34,36);
        cog = Arrays.copyOfRange(encodedBoatLocation,36,38);
        sog = Arrays.copyOfRange(encodedBoatLocation,38, 40);
        apparentWindSpeed = Arrays.copyOfRange(encodedBoatLocation, 40, 42);
        apparentWindAngle = Arrays.copyOfRange(encodedBoatLocation, 42, 44);
        trueWindSpeed = Arrays.copyOfRange(encodedBoatLocation,44, 46);
        trueWindDirection = Arrays.copyOfRange(encodedBoatLocation, 46, 48);
        trueWindAngle = Arrays.copyOfRange(encodedBoatLocation, 48, 50);
        currentDrift = Arrays.copyOfRange(encodedBoatLocation,50,52);
        currentSet = Arrays.copyOfRange(encodedBoatLocation,52, 54);
        rudderAngle = Arrays.copyOfRange(encodedBoatLocation,54, 56);

//        System.out.println(bytesToInt(sourceID));
//        System.out.println(bytesToInt(boatSpeed));

        message = new BoatLocation(messageVersionNumber, bytesToLong(time),
                                        bytesToInt(sourceID), bytesToInt(seqNum),
                                        deviceType, bytesToInt(latitude),
                                        bytesToInt(longitude), bytesToInt(altitude),
                                        bytesToInt(heading), bytesToShort(pitch),
                                        bytesToShort(roll), bytesToInt(boatSpeed),
                                        bytesToInt(cog), bytesToInt(sog),
                                        bytesToInt(apparentWindSpeed), bytesToShort(apparentWindAngle),
                                        bytesToInt(trueWindSpeed), bytesToShort(trueWindDirection),
                                        bytesToShort(trueWindAngle), bytesToInt(currentDrift),
                                        bytesToInt(currentSet), bytesToShort(rudderAngle)
        );
    }


    public BoatLocation getMessage() {
        return message;
    }
}
