// Path: app/src/main/java/com/dataart/btle_android/BTLEApplication.java
// public class BTLEApplication extends MultiDexApplication {
// 
//     private static BTLEApplication application;
// 
//     public static BTLEApplication getApplication() {
//         return application;
//     }
// 
//     @Override
//     public void onCreate() {
//         super.onCreate();
//         application = this;
//         MultiDex.install(this);
//         BTLEDevicePreferences.getInstance().init(this);
//         if (BuildConfig.DEBUG){
//             Timber.plant(new Timber.DebugTree());
//         }
//     }
// 
// }
// 
// Path: app/src/main/java/com/dataart/btle_android/helpers/BleHelpersFactory.java
// public class BleHelpersFactory {
// 
//     public static BleInitializer getInitializer(Activity activity, BleInitializer.InitCompletionCallback initCompletionCallback) {
//         final int osVersion = Build.VERSION.SDK_INT;
// 
//         if (osVersion >= Build.VERSION_CODES.M) {
//             return new BleInitializerM(activity, initCompletionCallback);
//         } else if (osVersion >= Build.VERSION_CODES.LOLLIPOP) {
//             return new BleInitializerL(activity, initCompletionCallback);
//         } else if (osVersion >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
//             return new BleInitializerJ(activity, initCompletionCallback);
//         }
// 
// //        There is no BLE support in Android versions below Jelly Bean
//         return null;
//     }
// 
//     public static BleScanner getScanner(BleScanner.ScanCallback scanCallback, BluetoothAdapter bluetoothAdapter) {
//         final int osVersion = Build.VERSION.SDK_INT;
// 
//         if (osVersion >= Build.VERSION_CODES.LOLLIPOP) {
//             return new BleScannerL(scanCallback, bluetoothAdapter);
//         } else if (osVersion >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
//             return new BleScannerJ(scanCallback, bluetoothAdapter);
//         }
// 
// //        There is no BLE support in Android versions below Jelly Bean
//         return null;
//     }
// }
// 
// Path: app/src/main/java/com/dataart/btle_android/helpers/ble/base/BleScanner.java
// public abstract class BleScanner {
//     protected final ScanCallback scanCallback;
//     protected final BluetoothAdapter bluetoothAdapter;
// 
//     public BleScanner(ScanCallback scanCallback, BluetoothAdapter bluetoothAdapter) {
//         this.scanCallback = scanCallback;
//         this.bluetoothAdapter = bluetoothAdapter;
//     }
// 
//     public abstract void startScan();
// 
//     public abstract void stopScan();
// 
//     public interface ScanCallback {
//         void onDeviceFound(BluetoothDevice device, int rssi, byte[] scanRecord);
//     }
// }


import android.bluetooth.BluetoothDevice;
import com.dataart.btle_android.BTLEApplication;
import com.dataart.btle_android.R;
import com.dataart.btle_android.helpers.BleHelpersFactory;
import com.dataart.btle_android.helpers.ble.base.BleScanner;
import timber.log.Timber;
package com.dataart.btle_android.btle_gateway.server;




class ScanCallbacks {
    private final ConnectionOperation operation;
    private final BleScanner.ScanCallback localCallback;
    private BleScanner scanner;
    private final BluetoothServer server;

    public ScanCallbacks(BluetoothServer server, final String address, final ConnectionOperation operation) {
        this.operation = operation;
        this.server = server;

        this.localCallback = new BleScanner.ScanCallback() {
            private boolean found = false;

            @Override
            public void onDeviceFound(BluetoothDevice device, int rssi, byte[] scanRecord) {
                if (!found && device.getAddress().equals(address)) {
                    found = true;
                    stop();
                    onDeviceFound(device, rssi, scanRecord);

                    server.connectAndSave(address, device, () -> {
                        Timber.d("on connected - calling operation");
                        server.applyForConnection(address, operation);
                    });
                }
            }
        };
    }

    public void start() {
        Timber.d("no device or connection - scanning for device");
//              startScan for device, add it to discovered devices, connect and call operation

// Path: app/src/main/java/com/dataart/btle_android/BTLEApplication.java
// public class BTLEApplication extends MultiDexApplication {
// 
//     private static BTLEApplication application;
// 
//     public static BTLEApplication getApplication() {
//         return application;
//     }
// 
//     @Override
//     public void onCreate() {
//         super.onCreate();
//         application = this;
//         MultiDex.install(this);
//         BTLEDevicePreferences.getInstance().init(this);
//         if (BuildConfig.DEBUG){
//             Timber.plant(new Timber.DebugTree());
//         }
//     }
// 
// }
// 
// Path: app/src/main/java/com/dataart/btle_android/helpers/BleHelpersFactory.java
// public class BleHelpersFactory {
// 
//     public static BleInitializer getInitializer(Activity activity, BleInitializer.InitCompletionCallback initCompletionCallback) {
//         final int osVersion = Build.VERSION.SDK_INT;
// 
//         if (osVersion >= Build.VERSION_CODES.M) {
//             return new BleInitializerM(activity, initCompletionCallback);
//         } else if (osVersion >= Build.VERSION_CODES.LOLLIPOP) {
//             return new BleInitializerL(activity, initCompletionCallback);
//         } else if (osVersion >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
//             return new BleInitializerJ(activity, initCompletionCallback);
//         }
// 
// //        There is no BLE support in Android versions below Jelly Bean
//         return null;
//     }
// 
//     public static BleScanner getScanner(BleScanner.ScanCallback scanCallback, BluetoothAdapter bluetoothAdapter) {
//         final int osVersion = Build.VERSION.SDK_INT;
// 
//         if (osVersion >= Build.VERSION_CODES.LOLLIPOP) {
//             return new BleScannerL(scanCallback, bluetoothAdapter);
//         } else if (osVersion >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
//             return new BleScannerJ(scanCallback, bluetoothAdapter);
//         }
// 
// //        There is no BLE support in Android versions below Jelly Bean
//         return null;
//     }
// }
// 
// Path: app/src/main/java/com/dataart/btle_android/helpers/ble/base/BleScanner.java
// public abstract class BleScanner {
//     protected final ScanCallback scanCallback;
//     protected final BluetoothAdapter bluetoothAdapter;
// 
//     public BleScanner(ScanCallback scanCallback, BluetoothAdapter bluetoothAdapter) {
//         this.scanCallback = scanCallback;
//         this.bluetoothAdapter = bluetoothAdapter;
//     }
// 
//     public abstract void startScan();
// 
//     public abstract void stopScan();
// 
//     public interface ScanCallback {
//         void onDeviceFound(BluetoothDevice device, int rssi, byte[] scanRecord);
//     }
// }


// Path: app/src/main/java/com/dataart/btle_android/btle_gateway/server/ScanCallbacks.java
import android.bluetooth.BluetoothDevice;
import com.dataart.btle_android.BTLEApplication;
import com.dataart.btle_android.R;
import com.dataart.btle_android.helpers.BleHelpersFactory;
import com.dataart.btle_android.helpers.ble.base.BleScanner;
import timber.log.Timber;

package com.dataart.btle_android.btle_gateway.server;




class ScanCallbacks {
    private final ConnectionOperation operation;
    private final BleScanner.ScanCallback localCallback;
    private BleScanner scanner;
    private final BluetoothServer server;

    public ScanCallbacks(BluetoothServer server, final String address, final ConnectionOperation operation) {
        this.operation = operation;
        this.server = server;

        this.localCallback = new BleScanner.ScanCallback() {
            private boolean found = false;

            @Override
            public void onDeviceFound(BluetoothDevice device, int rssi, byte[] scanRecord) {
                if (!found && device.getAddress().equals(address)) {
                    found = true;
                    stop();
                    onDeviceFound(device, rssi, scanRecord);

                    server.connectAndSave(address, device, () -> {
                        Timber.d("on connected - calling operation");
                        server.applyForConnection(address, operation);
                    });
                }
            }
        };
    }

    public void start() {
        Timber.d("no device or connection - scanning for device");
//              startScan for device, add it to discovered devices, connect and call operation

        scanner = BleHelpersFactory.getScanner(localCallback, server.getBluetoothAdapter());