const {
  BtHelper
} = require('../../devices/bt_helper.js');

module.exports = {
  compareList: compareList,
  compareList2: compareList2,
}

var deviceListIndex = 0
async function compareList2(deviceList, callback) {
  let isHasWifi = deviceList.find(item => {
    return item.connectType == 3;
  });
  if (isHasWifi) {
    //有wifi
    callback()
    return;
  }

  if (deviceList.length == 1) {
    // 只有一个直接连接
    let device = deviceList[0];
    if (device.state === 'online') {
      return;
    }
    getApp().getBluetoothStatus();
    callback(deviceList[0])
    return;
  }

  let hasOnline = deviceList.find(item => {
    return item.state === 'online';
  });

  if (hasOnline) {
    //有在线的
    callback(hasOnline)
    return;
  }

  callback(deviceList[deviceListIndex])
  deviceListIndex++

  // _startScan(deviceListIndex, deviceList, callback);
}

function _startScan(index, deviceList, callback) {
  console.log("_startScan", index);
  if (index >= deviceList.length) {
    deviceListIndex = 0
    callback()
    return
  }
  let element = deviceList[index];

  BtHelper.getInstance().startScan(element, function (boolean) {
    if (!boolean) {
      index++;
      if (index < deviceList.length) {
        element = deviceList[index];
        // _startScan(index, deviceList, callback);
      } else {
        deviceListIndex = 0
        // callback()
        // return
      }
    }
  }, function (res) {
    deviceListIndex = 0
    console.log("自动连接上的", element.deviceId, res.deviceId);
    BtHelper.getInstance().stopSearch();
    callback(element)
  });
}

function compareList(changeCallback, addBlueCallback) {
  var pages = getCurrentPages();
  var deviceList = pages[0].getDeviceList()
  if (deviceList.length > 0) {
    const {
      BtHelper
    } = require('../../devices/bt_helper.js');
    if (BtHelper.getInstance().getCallBackConnect() == null) {
      var compareList = BtHelper.getInstance().getCompareList();
      var dissmissDevice = BtHelper.getInstance().getDissmissDevice();
      console.log("compareList", compareList);
      console.log("dissmissDevice", dissmissDevice);
      ///去掉未连接的离线的
      for (var i = 0; i < dissmissDevice.length; i++) {
        for (var j = 0; j < compareList.length; j++) {
          if (compareList[j].deviceId == dissmissDevice[i].deviceId) {
            compareList.splice(j, 1);
            break;
          }
        }
      }

      if (compareList.length > 0) {
        ///对比在线的蓝牙设备
        var isChanged = false;
        for (var i = 0; i < deviceList.length; i++) {
          var tempItem = deviceList[i];
          if (tempItem.connectType != 3) {
            var has = false;
            for (var j = 0; j < compareList.length; j++) {
              // "state":"online" "offline" MW-SR1(4G_WIFI)
              if (tempItem.deviceId === compareList[j].deviceId) {
                has = true;
                break;
              }
            }

            if (has) {
              if (tempItem.state != "online") {
                isChanged = true;
                tempItem.state = "online";
              }
            } else {
              if (tempItem.state != "offline") {
                isChanged = true;
                tempItem.state = "offline";
              }
            }
          }
        }

        if (isChanged) {
          changeCallback(deviceList);
        }

        ///没有连接则连接第一个在线的蓝牙
        var autoConnected = pages[0].getAutoConnected();
        var deviceListSelect = pages[0].getDeviceListSelect();
        if (!autoConnected && deviceListSelect == null) {
          var list = pages[0].getDeviceList();
          for (var i = 0; i < list.length; i++) {
            var item = list[i];
            if (item.connectType == 1 && item.state == "online") {
              item.name = item.devName;
              BtHelper.getInstance().connect(item, function (isConnected, device) {
                if (isConnected) {
                  addBlueCallback(device);
                }
              });
              break;
            }
          }
        }
      }
      BtHelper.getInstance().startScan(null, null, null);
    }
  }
};