package info.izumin.android.bletia;

import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.os.Bundle;
import android.os.Message;

import org.jdeferred.Deferred;

import java.util.UUID;

import info.izumin.android.bletia.wrapper.BluetoothGattWrapper;

/**
 * Created by izumin on 9/7/15.
 */
public class BleEvent<T> {
    enum Type {
        WRITE_CHARACTERISTIC(1) {
            @Override
            public void handle(BluetoothGattWrapper gattWrapper, BleEvent event) {
                gattWrapper.writeCharacteristic((BluetoothGattCharacteristic) event.getValue());
            }
        },
        READ_CHARACTERISTIC(2) {
            @Override
            public void handle(BluetoothGattWrapper gattWrapper, BleEvent event) {
                gattWrapper.readCharacteristic((BluetoothGattCharacteristic) event.getValue());
            }
        },
        WRITE_DESCRIPTOR(3) {
            @Override
            public void handle(BluetoothGattWrapper gattWrapper, BleEvent event) {
                gattWrapper.writeDescriptor((BluetoothGattDescriptor) event.getValue());
            }
        },
        READ_DESCRIPTOR(4) {
            @Override
            public void handle(BluetoothGattWrapper gattWrapper, BleEvent event) {
                gattWrapper.readDescriptor((BluetoothGattDescriptor) event.getValue());
            }
        };

        private final int mWhat;

        Type(int what) {
            mWhat = what;
        }

        public int getWhat() {
            return mWhat;
        }

        public abstract void handle(BluetoothGattWrapper gattWrapper, BleEvent event);

        public static Type valueOf(int what) {
            for (Type type : values()) {
                if (type.getWhat() == what) { return type; }
            }
            throw new IllegalArgumentException();
        }
    }

    public static final String KEY_UUID = "event uuid";

    private final UUID mUuid;
    private final Deferred mDeferred;
    private BluetoothGattCharacteristic mCharacteristic;

    public BleEvent(UUID uuid, Deferred deferred) {
        mUuid = uuid;
        mDeferred = deferred;
    }

    public UUID getUuid() {
        return mUuid;
    }

    public <D, F, P> Deferred<D, F, P> getDeferred() {
        return mDeferred;
    }

    public BluetoothGattCharacteristic getCharacteristic() {
        return mCharacteristic;
    }

    public void setCharacteristic(BluetoothGattCharacteristic characteristic) {
        mCharacteristic = characteristic;
    }

    public Message obtainMessage() {
        Message message = Message.obtain();
        message.what = mType.getWhat();
        Bundle bundle = new Bundle();
        bundle.putSerializable(KEY_UUID, mUuid);
        message.setData(bundle);
        return message;
    }
}

@@ -1970,218 +1970,305 @@
 nal 
-UUID mUuid;%0A    private final Deferred mDeferred;%0A    private BluetoothGattCharacteristic mCharacteristic;%0A%0A    public BleEvent(UUID uuid, Deferred deferred) %7B%0A        mUuid = uuid;%0A        mDeferred = deferred
+Type mType;%0A    private final UUID mUuid;%0A    private final T mValue;%0A%0A    private Deferred%3CT, BleStatus, ?%3E mDeferred;%0A%0A    public BleEvent(Type type, UUID uuid, T value) %7B%0A        mType = type;%0A        mUuid = uuid;%0A        mValue = value;%0A    %7D%0A%0A    public Type getType() %7B%0A        return mType
 ;%0A  
@@ -2344,47 +2344,18 @@
 lic 
-%3CD, F, P%3E Deferred%3CD, F, P%3E getDeferred
+T getValue
 () %7B
@@ -2371,24 +2371,21 @@
 return m
-Deferred
+Value
 ;%0A    %7D%0A
@@ -2400,53 +2400,45 @@
 lic 
-BluetoothGattCharacteristic getCharacteristic
+Deferred%3CT, BleStatus, ?%3E getDeferred
 () %7B
@@ -2454,30 +2454,24 @@
 return m
-Characteristic
+Deferred
 ;%0A    %7D%0A
@@ -2494,109 +2494,83 @@
  set
-Characteristic(BluetoothGattCharacteristic characteristic) %7B%0A        mCharacteristic = characteristic
+Deferred(Deferred%3CT, BleStatus, ?%3E deferred) %7B%0A        mDeferred = deferred
 ;%0A  

Fix BleEvent