package com.device.comm.mylibrary;

import android.annotation.TargetApi;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanRecord;
import android.bluetooth.le.ScanResult;
import android.os.Build;
import android.util.Log;

import java.util.List;

import static com.facebook.react.bridge.UiThreadUtil.runOnUiThread;

import androidx.annotation.RequiresApi;

/**
 * Created by lvwang2002 on 16/1/22.
 */
public class BleDeviceListener {
    private String TAG = "BLE_DEVICE_LISTENER";
    private  BleDeviceManager m_mngr;

    BleDeviceListener(BleDeviceManager deviceManager){
        m_mngr = deviceManager;
    }


    final BluetoothAdapter.LeScanCallback m_scanCallback_preLollipop = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice device_native, final int rssi, final byte[] scanRecord)
        {
//            Log.d(TAG,"扫描到蓝牙设备");
            if( postNeeded() )
            {
                post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        onLeScan_mainThread(device_native, rssi, scanRecord);
                    }
                });
            }
            else
            {
                onLeScan_mainThread(device_native, rssi, scanRecord);
            }
        }
    };




    private boolean postNeeded()
    {
        return m_mngr.getUpdateLoop().postNeeded();
    }

    private void onLeScan_mainThread(final BluetoothDevice device_native, final int rssi, final byte[] scanRecord)
    {
//        m_mngr.getCrashResolver().notifyScannedDevice(device_native, m_scanCallback_preLollipop);

        m_mngr.onDiscoveredFromNativeStack(device_native, rssi, scanRecord);


    }

    private void post(final Runnable runnable)
    {
        final UpdateLoop updateLoop = m_mngr.getUpdateLoop();

        updateLoop.postIfNeeded(runnable);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    final ScanCallback scanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
//            RNLog.d(TAG,"scan result:"+result.getDevice().getName());
            super.onScanResult(callbackType, result);
            final BluetoothDevice device_native = result.getDevice();
            final byte[] scanRecord = result.getScanRecord().getBytes();
            final int rssi = result.getRssi();
            if (postNeeded()) {
                post(new Runnable() {
                    @Override
                    public void run() {
                        onLeScan_mainThread(device_native, rssi, scanRecord);
                    }
                });
            } else {
                onLeScan_mainThread(device_native, rssi, scanRecord);
            }
        }

        @Override
        public void onBatchScanResults(final List<ScanResult> results) {
            super.onBatchScanResults(results);
//            RNLog.d(TAG,"scan result size:"+results.size());

        }

        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);
            RNLog.d(TAG,"error code:"+errorCode);
        }
    };
}
