package seng302.Networking.MessageDecoders;

import org.junit.Assert;
import org.junit.Test;
import seng302.Networking.MessageEncoders.RaceVisionByteEncoder;
import seng302.Networking.Messages.BoatStatus;
import seng302.Networking.Messages.RaceStatus;

import java.util.ArrayList;

/**
 * Created by hba56 on 23/04/17.
 */
public class RaceStatusDecoderTest {
    @Test
    public void getByteArrayTest(){
        long time = System.currentTimeMillis();

        //Create data to serialize.
        int boat1SourceID = 5;
        int boat2SourceID = 8;
        byte boat1Status = 2;
        byte boat2Status = 2;
        byte boat1LegNumber = 5;
        byte boat2LegNumber = 3;
        byte boat1PenaltiesAwarded = 4;
        byte boat2PenaltiesAwarded = 0;
        byte boat1PenaltiesServed = 2;
        byte boat2PenaltiesServed = 0;
        long boat1TimeAtNextMark = time + (1000 * 3);
        long boat2TimeAtNextMark = time + (1000 * 2);
        long boat1TimeAtFinish = boat1TimeAtNextMark + (1000 * 15);
        long boat2TimeAtFinish = boat2TimeAtNextMark + (1000 * 7);

        BoatStatus boatStatus1 = new BoatStatus(boat1SourceID, boat1Status, boat1LegNumber, boat1PenaltiesAwarded, boat1PenaltiesServed, boat1TimeAtNextMark, boat1TimeAtFinish);
        BoatStatus boatStatus2 = new BoatStatus(boat2SourceID, boat2Status, boat2LegNumber, boat2PenaltiesAwarded, boat2PenaltiesServed, boat2TimeAtNextMark, boat2TimeAtFinish);

        int raceID = 585;
        int raceStatus = 3;
        long raceStartTime = time - (1000 * 31);
        int windDirection = 2341;
        int windSpeed = 10201;
        int raceType = 1;
        ArrayList<BoatStatus> boatStatuses = new ArrayList<>(2);
        boatStatuses.add(boatStatus1);
        boatStatuses.add(boatStatus2);

        RaceStatus raceStatusObject = new RaceStatus(time, raceID, raceStatus, raceStartTime, windDirection, windSpeed, raceType, boatStatuses);


        byte[] encodedRaceStatus = RaceVisionByteEncoder.raceStatus(raceStatusObject);

        RaceStatusDecoder decoderTest = new RaceStatusDecoder(encodedRaceStatus);

        Assert.assertEquals(0b10, decoderTest.getVersionNum());
        Assert.assertEquals(time, decoderTest.getTime());
        Assert.assertEquals(raceID, decoderTest.getRace());
        Assert.assertEquals(raceStatus, decoderTest.getRaceState());
        Assert.assertEquals(raceStartTime, decoderTest.getStartTime());
        Assert.assertEquals(windDirection, decoderTest.getRaceWindDir());
        Assert.assertEquals(windSpeed, decoderTest.getRaceWindSpeed());


        BoatStatus boat1 = decoderTest.getBoats().get(0);

        Assert.assertEquals(boat1SourceID, boat1.getSourceID());
        Assert.assertEquals(boat1Status, boat1.getBoatStatus());
        Assert.assertEquals(boat1LegNumber, boat1.getLegNumber());
        Assert.assertEquals(boat1PenaltiesAwarded, boat1.getNumPenaltiesAwarded());
        Assert.assertEquals(boat1PenaltiesServed, boat1.getNumPenaltiesServed());
        Assert.assertEquals(boat1TimeAtNextMark, boat1.getEstTimeAtNextMark());
        Assert.assertEquals(boat1TimeAtFinish, boat1.getEstTimeAtFinish());

    }
}
