/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Little Robots
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package nl.littlerobots.bean.message;

import android.os.Parcelable;
import android.text.TextUtils;

import java.nio.charset.Charset;

import auto.parcel.AutoParcel;
import okio.Buffer;

@AutoParcel
public abstract class RadioConfig implements Parcelable, Message {

    public static RadioConfig create(String name, int advertisementInterval, int connectionInterval, TX_POWER power) {
        if (TextUtils.isEmpty(name)) {
            throw new IllegalArgumentException("Name must not be empty");
        }
        if (name.length() > 20) {
            throw new IllegalArgumentException("Name must be max 20 characters");
        }
        return new AutoParcel_RadioConfig(name, advertisementInterval, connectionInterval, power);
    }

    public static RadioConfig fromPayload(Buffer buffer) {
        int advertisementInterval = buffer.readShortLe() & 0xffff;
        int connectionInterval = buffer.readShortLe() & 0xffff;
        int power = buffer.readByte() & 0xff;
        String localName = buffer.readString(buffer.size() - 1, Charset.forName("UTF-8"));
        int size = buffer.readByte() & 0xff;
        localName = localName.substring(0, size);
        return new AutoParcel_RadioConfig(localName, advertisementInterval, connectionInterval, TX_POWER.values()[power]);
    }

    public abstract String name();

    public abstract int advertisementInterval();

    public abstract int connectionInterval();

    public abstract TX_POWER power();

    @Override
    public byte[] toPayload() {
        Buffer buffer = new Buffer();
        buffer.writeShortLe(advertisementInterval() & 0xffff);
        buffer.writeShortLe(connectionInterval() & 0xffff);
        buffer.writeByte(power().ordinal() & 0xff);
        StringBuilder sb = new StringBuilder(name());
        sb.setLength(20);
        buffer.writeString(sb.toString(), Charset.forName("UTF-8"));
        buffer.writeByte(name().length() & 0xff);
        return buffer.readByteArray();
    }

    public enum TX_POWER {TX_4DB, TX_0DB, TX_NEG6DB, TX_NEG23DB}
}

@@ -1460,73 +1460,405 @@
 ame,
- int advertisementInterval, int connectionInterval, TX_POWER powe
+%0A                                     int advertisementInterval,%0A                                     int connectionInterval,%0A                                     TxPower power,%0A                                     AdvertisementMode mode,%0A                                     int beaconUuid,%0A                                     int beaconMajor,%0A                                     int beaconMino
 r) %7B
@@ -2150,24 +2150,40 @@
 Config(name,
+%0A               
  advertiseme
@@ -2185,32 +2185,48 @@
 isementInterval,
+%0A               
  connectionInter
@@ -2229,21 +2229,145 @@
 nterval,
- powe
+%0A                power,%0A                mode,%0A                beaconUuid,%0A                beaconMajor,%0A                beaconMino
 r);%0A    
@@ -2597,32 +2597,247 @@
 dByte() & 0xff;%0A
+        int mode = buffer.readByte() & 0xff;%0A        int beaconUuid = buffer.readShortLe() & 0xffff;%0A        int beaconMajor = buffer.readShortLe() & 0xffff;%0A        int beaconMinor = buffer.readShortLe() & 0xffff;%0A
         String l
@@ -3058,16 +3058,32 @@
 calName,
+%0A               
  adverti
@@ -3097,16 +3097,32 @@
 nterval,
+%0A               
  connect
@@ -3137,209 +3137,538 @@
 val,
- TX_POWER.values()%5Bpower%5D);%0A    %7D%0A%0A    public abstract String name();%0A%0A    public abstract int advertisementInterval();%0A%0A    public abstract int connectionInterval();%0A%0A    public abstract TX_POWER powe
+%0A                TxPower.values()%5Bpower%5D,%0A                AdvertisementMode.values()%5Bmode%5D,%0A                beaconUuid,%0A                beaconMajor,%0A                beaconMinor);%0A    %7D%0A%0A    public abstract String name();%0A%0A    public abstract int advertisementInterval();%0A%0A    public abstract int connectionInterval();%0A%0A    public abstract TxPower power();%0A%0A    public abstract AdvertisementMode advertisementMode();%0A%0A    public abstract int beaconUuid();%0A%0A    public abstract int beaconMajor();%0A%0A    public abstract int beaconMino
 r();
@@ -3920,32 +3920,254 @@
 inal() & 0xff);%0A
+        buffer.writeByte(advertisementMode().ordinal() & 0xff);%0A        buffer.writeShortLe(beaconUuid() & 0xffff);%0A        buffer.writeShortLe(beaconMajor() & 0xffff);%0A        buffer.writeShortLe(beaconMinor() & 0xffff);%0A
         StringBu
@@ -4416,54 +4416,108 @@
 um T
-X_POWER %7BTX_4
+xPower %7BTX_NEG23
 DB, TX_
-0
+NEG6
 DB, TX_
-NEG6
+0
 DB, TX_
-NEG23DB
+4DB%7D%0A%0A    public enum AdvertisementMode %7BSTANDARD, IBEACON
 %7D%0A%7D%0A

Update to new radio config