package com.uxxu.konashi.lib;

import android.os.Handler;
import android.os.Looper;

import java.util.ArrayList;

import info.izumin.android.bletia.BletiaException;

/**
 * Created by izumin on 9/23/15.
 */
class EventEmitter extends ArrayList<KonashiListener> {

    private final EventEmitter self = this;
    private final Handler mHandler = new Handler(Looper.getMainLooper());

    public void emitConnect(final KonashiManager manager) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                for (KonashiListener listener : self) {
                    listener.onConnect(manager);
                }
            }
        });
    }

    public void emitDisconnect(final KonashiManager manager) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                for (KonashiListener listener : self) {
                    listener.onDisconnect(manager);
                }
            }
        });
    }

    public void emitError(final KonashiManager manager, final BletiaException e) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                for (KonashiListener listener : self) {
                    listener.onError(manager, e);
                }
            }
        });
    }

    public void emitUpdatePioOutput(final KonashiManager manager, final int value) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                for (KonashiListener listener : self) {
                    listener.onUpdatePioOutput(manager, value);
                }
            }
        });
    }

    public void emitUpdateUartRx(final KonashiManager manager, final byte[] value) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                for (KonashiListener listener : self) {
                    listener.onUpdateUartRx(manager, value);
                }
            }
        });
    }

    public void emitUpdateSpiMiso(final KonashiManager manager, final byte[] value) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                for (KonashiListener listener : self) {
                    listener.onUpdateSpiMiso(manager, value);
                }
            }
        });
    }

    public void emitUpdateBatteryLevel(final KonashiManager manager, final int level) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                for (KonashiListener listener : self) {
                    listener.onUpdateBatteryLevel(manager, level);
                }
            }
        });
    }
}

@@ -43,44 +43,134 @@
 oid.
-os.Handler;%0Aimport android.os.Looper
+bluetooth.BluetoothGattCharacteristic;%0Aimport android.os.Handler;%0Aimport android.os.Looper;%0A%0Aimport org.jdeferred.DoneCallback
 ;%0A%0Ai
@@ -2235,32 +2235,120 @@
 ew Runnable() %7B%0A
+            private byte mData%5B%5D;%0A            private KonashiListener mKonashiListener;%0A
             @Ove
@@ -2465,47 +2465,640 @@
     
-listener.onUpdateSpiMiso(manager, value
+mKonashiListener = listener;%0A                    manager.spiRead().then(new DoneCallback%3CBluetoothGattCharacteristic%3E() %7B%0A                        @Override%0A                        public void onDone(BluetoothGattCharacteristic result) %7B%0A                            mData = new byte%5Bresult.getValue().length%5D;%0A                            for (int i = 0; i %3C result.getValue().length; i++) %7B%0A                                mData%5Bi%5D = (byte) ((result.getValue()%5Bi%5D & 0xff));%0A                                mKonashiListener.onUpdateSpiMiso(manager, mData);%0A                            %7D%0A                        %7D%0A                    %7D
 );%0A 

Change onUpdateSpiMiso's value to spiRead's result