package info.izumin.android.bletia.action;

import android.os.Bundle;
import android.os.Message;

import org.jdeferred.Deferred;
import org.jdeferred.impl.DeferredObject;

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

/**
 * Created by izumin on 9/15/15.
 */
public abstract class Action<T, I> {
    public enum Type {
        WRITE_CHARACTERISTIC(1),
        READ_CHARACTERISTIC(2),
        WRITE_DESCRIPTOR(3),
        READ_DESCRIPTOR(4),
        ENABLE_NOTIFICATION(5),
        READ_REMOTE_RSSI(6);

        private final int mCode;

        Type(int code) {
            mCode = code;
        }

        public int getCode() {
            return mCode;
        }

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

    public static final String KEY_UUID = "key_uuid";

    private final I mIdentity;
    private final Deferred<T, BletiaException, Void> mDeferred;

    public Action(I identity) {
        mIdentity = identity;
        mDeferred = new DeferredObject<>();
    }

    public Deferred<T, BletiaException, Void> getDeferred() {
        return mDeferred;
    }

    public I getIdentity() {
        return mIdentity;
    }

    public Message obtainMessage() {
        Message message = Message.obtain();
        message.what = getType().getCode();
        message.setData(getBundle());
        return message;
    }

    public Bundle getBundle() {
        return new Bundle();
    }

    public abstract Type getType();
    public abstract boolean execute(BluetoothGattWrapper gattWrapper);
}

@@ -41,62 +41,8 @@
 n;%0A%0A
-import android.os.Bundle;%0Aimport android.os.Message;%0A%0A
 impo
@@ -165,77 +165,8 @@
 ion;
-%0Aimport info.izumin.android.bletia.core.wrapper.BluetoothGattWrapper;
 %0A%0A/*
@@ -243,696 +243,67 @@
  I%3E 
-%7B%0A    public enum Type %7B%0A        WRITE_CHARACTERISTIC(1),%0A        READ_CHARACTERISTIC(2),%0A        WRITE_DESCRIPTOR(3),%0A        READ_DESCRIPTOR(4),%0A        ENABLE_NOTIFICATION(5),%0A        READ_REMOTE_RSSI(6);%0A%0A        private final int mCode;%0A%0A        Type(int code) %7B%0A            mCode = code;%0A        %7D%0A%0A        public int getCode() %7B%0A            return mCode;%0A        %7D%0A%0A        public static Type valueOf(int code) %7B%0A            for (Type type : values()) %7B%0A                if (type.getCode() == code) %7B return type; %7D%0A            %7D%0A            throw new IllegalArgumentException();%0A        %7D%0A    %7D%0A%0A    public static final String KEY_UUID = %22key_uuid%22;%0A%0A    private final I mIdentity;
+extends info.izumin.android.bletia.core.action.Action%3CI%3E %7B%0A
 %0A   
@@ -408,28 +408,22 @@
     
-mIdentity = 
+super(
 identity
 ;%0A  
@@ -414,24 +414,25 @@
 per(identity
+)
 ;%0A        mD
@@ -569,439 +569,7 @@
    %7D
-%0A%0A    public I getIdentity() %7B%0A        return mIdentity;%0A    %7D%0A%0A    public Message obtainMessage() %7B%0A        Message message = Message.obtain();%0A        message.what = getType().getCode();%0A        message.setData(getBundle());%0A        return message;%0A    %7D%0A%0A    public Bundle getBundle() %7B%0A        return new Bundle();%0A    %7D%0A%0A    public abstract Type getType();%0A    public abstract boolean execute(BluetoothGattWrapper gattWrapper);
 %0A%7D%0A

Make Action extend core Action