package info.izumin.android.bletia;

import android.os.Message;

import java.util.UUID;

import info.izumin.android.bletia.action.Action;
import info.izumin.android.bletia.action.EnableNotificationAction;
import info.izumin.android.bletia.action.ReadCharacteristicAction;
import info.izumin.android.bletia.action.ReadDescriptorAction;
import info.izumin.android.bletia.action.ReadRemoteRssiAction;
import info.izumin.android.bletia.action.WriteCharacteristicAction;
import info.izumin.android.bletia.action.WriteDescriptorAction;

/**
 * Created by izumin on 10/3/15.
 */
enum ActionMessageHandler {
    READ_CHARACTERISTIC(Action.Type.READ_CHARACTERISTIC) {
        @Override
        public void enqueue(Action action, ActionQueueContainer container) {
            container.getReadCharacteristicActionQueue().enqueue((ReadCharacteristicAction) action);
        }

        @Override
        public boolean execute(Message msg, ActionQueueContainer container) {
            UUID uuid = (UUID) msg.getData().getSerializable(ReadCharacteristicAction.KEY_UUID);
            return container.getReadCharacteristicActionQueue().execute(uuid);
        }
    },
    WRITE_CHARACTERISTIC(Action.Type.WRITE_CHARACTERISTIC) {
        @Override
        public void enqueue(Action action, ActionQueueContainer container) {
            container.getWriteCharacteristicActionQueue().enqueue((WriteCharacteristicAction) action);
        }

        @Override
        public boolean execute(Message msg, ActionQueueContainer container) {
            UUID uuid = (UUID) msg.getData().getSerializable(WriteCharacteristicAction.KEY_UUID);
            return container.getWriteDescriptorActionQueue().execute(uuid);
        }
    },
    READ_DESCRIPTOR(Action.Type.READ_DESCRIPTOR) {
        @Override
        public void enqueue(Action action, ActionQueueContainer container) {
            container.getReadDescriptorActionQueue().enqueue((ReadDescriptorAction) action);
        }

        @Override
        public boolean execute(Message msg, ActionQueueContainer container) {
            UUID uuid = (UUID) msg.getData().getSerializable(ReadDescriptorAction.KEY_UUID);
            return container.getReadDescriptorActionQueue().execute(uuid);
        }
    },
    WRITE_DESCRIPTOR(Action.Type.WRITE_DESCRIPTOR) {
        @Override
        public void enqueue(Action action, ActionQueueContainer container) {
            container.getWriteDescriptorActionQueue().enqueue((WriteDescriptorAction) action);
        }

        @Override
        public boolean execute(Message msg, ActionQueueContainer container) {
            UUID uuid = (UUID) msg.getData().getSerializable(WriteDescriptorAction.KEY_UUID);
            return container.getWriteDescriptorActionQueue().execute(uuid);
        }
    },
    ENABLE_NOTIFICATION(Action.Type.ENABLE_NOTIFICATION) {
        @Override
        public void enqueue(Action action, ActionQueueContainer container) {
            container.getEnableNotificationActionQueue().enqueue((EnableNotificationAction) action);
        }

        @Override
        public boolean execute(Message msg, ActionQueueContainer container) {
            UUID uuid = (UUID) msg.getData().getSerializable(EnableNotificationAction.KEY_UUID);
            return container.getEnableNotificationActionQueue().execute(uuid);
        }
    },
    READ_REMOTE_RSSI(Action.Type.READ_REMOTE_RSSI) {
        @Override
        public void enqueue(Action action, ActionQueueContainer container) {
            container.getReadRemoteRssiActionQueue().enqueue((ReadRemoteRssiAction) action);
        }

        @Override
        public boolean execute(Message msg, ActionQueueContainer container) {
            return container.getReadRemoteRssiActionQueue().execute(null);
        }
    };

    private final Action.Type mType;

    ActionMessageHandler(Action.Type type) {
        mType = type;
    }

    public Action.Type getType() {
        return mType;
    }

    public static ActionMessageHandler valueOf(Action.Type type) {
        return valueOf(type.getCode());
    }

    public static ActionMessageHandler valueOf(int what) {
        for (ActionMessageHandler handler : values()) {
            if (what == handler.getType().getCode()) { return handler; }
        }
        throw new IllegalArgumentException();
    }

    public abstract void enqueue(Action action, ActionQueueContainer container);
    public abstract boolean execute(Message msg, ActionQueueContainer container);
}

@@ -522,16 +522,80 @@
 rAction;
+%0Aimport info.izumin.android.bletia.wrapper.BluetoothGattWrapper;
 %0A%0A/**%0A *
@@ -1007,32 +1007,66 @@
 tainer container
+, BluetoothGattWrapper gattWrapper
 ) %7B%0A            
@@ -1218,32 +1218,45 @@
 e().execute(uuid
+, gattWrapper
 );%0A        %7D%0A   
@@ -1613,32 +1613,66 @@
 tainer container
+, BluetoothGattWrapper gattWrapper
 ) %7B%0A            
@@ -1790,34 +1790,38 @@
 ner.getWrite
-Descriptor
+Characteristic
 ActionQueue(
@@ -1826,32 +1826,45 @@
 e().execute(uuid
+, gattWrapper
 );%0A        %7D%0A   
@@ -2201,32 +2201,66 @@
 tainer container
+, BluetoothGattWrapper gattWrapper
 ) %7B%0A            
@@ -2404,32 +2404,45 @@
 e().execute(uuid
+, gattWrapper
 );%0A        %7D%0A   
@@ -2783,32 +2783,66 @@
 tainer container
+, BluetoothGattWrapper gattWrapper
 ) %7B%0A            
@@ -2988,32 +2988,45 @@
 e().execute(uuid
+, gattWrapper
 );%0A        %7D%0A   
@@ -3379,32 +3379,66 @@
 tainer container
+, BluetoothGattWrapper gattWrapper
 ) %7B%0A            
@@ -3598,16 +3598,29 @@
 ute(uuid
+, gattWrapper
 );%0A     
@@ -3967,32 +3967,66 @@
 tainer container
+, BluetoothGattWrapper gattWrapper
 ) %7B%0A            
@@ -4085,16 +4085,29 @@
 ute(null
+, gattWrapper
 );%0A     
@@ -4737,24 +4737,24 @@
 container);%0A
-
     public a
@@ -4820,13 +4820,47 @@
 ontainer
+, BluetoothGattWrapper gattWrapper
 );%0A%7D%0A

Fix ActionMessageHandler