

package com.neuromd.neurosdk;

import android.content.Context;

import com.neuromd.common.SubscribersNotifier;

/**
 * Provides common methods for device scanning and
 */
public class DeviceScanner {
    private long mNativeObjPtr;

    static {
        System.loadLibrary("android-neurosdk");
    }

    public final SubscribersNotifier<Device> deviceFound = new SubscribersNotifier<>();

    /**
     * Notifies about changing device scanning state
     */
    public final SubscribersNotifier<Boolean> scanStateChanged = new SubscribersNotifier<>();

    /**
     * Creates new instance of DeviceScanner class with current application context
     *
     * @param context Current application context
     */
    public DeviceScanner(Context context) {
        mNativeObjPtr = create(context);
    }

    public void finalize() throws Throwable {
        if (mNativeObjPtr != 0) {
            deleteNative(mNativeObjPtr);
            mNativeObjPtr = 0;
        }
        super.finalize();
    }

    /**
     * Starts Neurotech© devices scanning. Scan stops after @timeout milliseconds
     *
     * @param timeout Duration of scanning. Less or equal to zero for infinity
     */
    public void startScan(int timeout) {
        startScan(mNativeObjPtr, timeout);
    }

    /**
     * Stops device scan process
     */
    public void stopScan() {
        stopScan(mNativeObjPtr);
    }

    public Device getDeviceByAddress(String address){
        return findDeviceByAddress(mNativeObjPtr, address);
    }

    private void onDeviceFound(Device device) {
        deviceFound.sendNotification(this, device);
    }

    private void onScanStateChanged(boolean isScanning) {
        scanStateChanged.sendNotification(this, isScanning);
    }

    private native long create(Context appContext);

    private native void deleteNative(long objPtr);

    private native void startScan(long objPtr, int timeout);

    private native void stopScan(long objPtr);

    private native Device findDeviceByAddress(long objPtr, String address);
}
