package com.eveningoutpost.dexdrip.ImportedLibraries.dexcom;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class PacketBuilder {
    public static final int MAX_PAYLOAD = 1584;
    public static final int MIN_LEN = 6;
    public static final int MAX_LEN = MAX_PAYLOAD + MIN_LEN;
    public static final byte SOF = 0x01;
    public static final int OFFSET_SOF = 0;
    public static final int OFFSET_LENGTH = 1;
    public static final int OFFSET_NULL = 2;
    public static final byte NULL = 0x00;
    public static final int OFFSET_CMD = 3;
    public static final int OFFSET_PAYLOAD = 4;
    public static final int CRC_LEN = 2;
    public static final int HEADER_LEN = 4;
    public ArrayList<Byte> packet;
    public int command;
    public ArrayList<Byte> payload;

    public PacketBuilder(int command) {
        this.command = command;
    }

    public PacketBuilder(int command, ArrayList<Byte> payload) {
        this.command = command;
        this.payload = payload;
    }

    public byte[] compose() {
        packet = new ArrayList<Byte>();
        packet.add(OFFSET_SOF, SOF);
        packet.add(OFFSET_LENGTH, getLength());
        packet.add(OFFSET_NULL, NULL);
        packet.add(OFFSET_CMD, (byte) command);
        if (this.payload != null) { this.packet.addAll(OFFSET_PAYLOAD, this.payload); }
        byte[] crc16 = CRC16.calculate(toBytes(), 0, this.packet.size());
        this.packet.add(crc16[0]);
        this.packet.add(crc16[1]);

        Log.d("ShareTest", "About to start adding to Byte, size: " + this.packet.size());
        Log.d("ShareTest", "About to start adding to Byte: " + this.packet.toString());
        return this.toBytes();
    }

    public List<byte[]> composeList() {
        packet = new ArrayList<Byte>();
        packet.add(OFFSET_SOF, SOF);
        packet.add(OFFSET_LENGTH, getLength());
        packet.add(OFFSET_NULL, NULL);
        packet.add(OFFSET_CMD, (byte) command);
        if (this.payload != null) { this.packet.addAll(OFFSET_PAYLOAD, this.payload); }
        byte[] crc16 = CRC16.calculate(toBytes(), 0, this.packet.size());
        this.packet.add(crc16[0]);
        this.packet.add(crc16[1]);

        Log.d("ShareTest", "About to start adding to ByteList, size: " + this.packet.size());
        Log.d("ShareTest", "About to start adding to ByteList, size: " + this.packet.toString());
        return this.toBytesList();
    }

    private byte getLength() {
        int packetSize = payload == null ? MIN_LEN : payload.size() + CRC_LEN + HEADER_LEN;

        if (packetSize > MAX_LEN) {
            throw new IndexOutOfBoundsException(packetSize + " bytes, but packet must between "
                    + MIN_LEN + " and " + MAX_LEN + " bytes.");
        }

        return (byte) packetSize;
    }

    public byte[] toBytes() {
        byte[] b = new byte[this.packet.size()];
        for (int i = 0; i < this.packet.size(); i++) {
            b[i] = this.packet.get(i).byteValue();
        }
        return b;
    }

    public List<byte[]> toBytesList() {
        List<byte[]> byteMessages = new ArrayList<byte[]>();
        double totalPacketSize = packet.size();
        int messages =(int) Math.ceil(totalPacketSize/18);

        Log.d("ShareTest", "Adding to ByteList, size: " + messages);
        Log.d("ShareTest", "Adding to ByteList, size: " + packet.size());
        for(int m = 0; m < messages; m++) {
            int thisPacketSize;
            if (m == messages - 1) {
                thisPacketSize = ((this.packet.size()+2) % 18);
            } else {
                thisPacketSize = (20);
            }
            int offset = m * 18;
            Log.d("ShareTest", "This packet size: " + thisPacketSize);
            byte[] b = new byte[thisPacketSize];
            b[0] = (byte) (m + 1);

            Log.d("ShareTest", "Adding byte: " + b[0]);
            b[1] = (byte) (messages);
            Log.d("ShareTest", "Adding byte: " + b[1]);
            for (int i = 2; i < thisPacketSize; i++) {
                b[i] = packet.get(offset + i - 2).byteValue();
                Log.d("ShareTest", "Adding byte: " + packet.get(offset + i - 2));
            }
            Log.d("ShareTest", "Adding to Bytelist: " + b.toString());
            byteMessages.add(b);
        }
        return byteMessages;
    }
}

@@ -1485,33 +1485,32 @@
 .add(crc16%5B1%5D);%0A
-%0A
         Log.d(%22S
@@ -1587,96 +1587,8 @@
 ));%0A
-        Log.d(%22ShareTest%22, %22About to start adding to Byte: %22 + this.packet.toString());%0A
     
@@ -2105,17 +2105,16 @@
 16%5B1%5D);%0A
-%0A
         
@@ -2203,106 +2203,8 @@
 ));%0A
-        Log.d(%22ShareTest%22, %22About to start adding to ByteList, size: %22 + this.packet.toString());%0A
     
@@ -3044,152 +3044,8 @@
 18);
-%0A%0A        Log.d(%22ShareTest%22, %22Adding to ByteList, size: %22 + messages);%0A        Log.d(%22ShareTest%22, %22Adding to ByteList, size: %22 + packet.size());
 %0A   
@@ -3484,443 +3484,177 @@
 1);%0A
-%0A            Log.d(%22ShareTest%22, %22Adding byte: %22 + b%5B0%5D);%0A            b%5B1%5D = (byte) (messages);%0A            Log.d(%22ShareTest%22, %22Adding byte: %22 + b%5B1%5D);%0A            for (int i = 2; i %3C thisPacketSize; i++) %7B%0A                b%5Bi%5D = packet.get(offset + i - 2).byteValue();%0A                Log.d(%22ShareTest%22, %22Adding byte: %22 + packet.get(offset + i - 2));%0A            %7D%0A            Log.d(%22ShareTest%22, %22Adding to Bytelist: %22 + b.toString());
+            b%5B1%5D = (byte) (messages);%0A            for (int i = 2; i %3C thisPacketSize; i++) %7B%0A                b%5Bi%5D = packet.get(offset + i - 2).byteValue();%0A            %7D
 %0A   

remove unncessary logging