module.exports = {
  searchOnlineDevice: searchOnlineDevice,
}

// [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
/// payloads：{"uuid":"AIrSMArT_7cdfa1fcbb24","state":"online","userid":"1"}
// [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]

///更新在线状态，连接第一个在线设备
// deviceId: BLUFI_7cdfa1fd3af0
// uuid: AIrSMArT_7cdfa1fd3af0
function searchOnlineDevice(payloads, changeCallback, offlineConnectedCallback, resetCallback) {
  var isChanged = false;
  const strings = require('../strings');
  const lexin_util = require('../lexin/util');
  var pages = getCurrentPages();
  var deviceList = pages[0].getDeviceList();

  /// 处理在线设备
  if (!strings.isEmpty(deviceList)) {
    for (var i = 0; i < deviceList.length; i++) {
      if (payloads && payloads.uuid) {
        var device = deviceList[i];
        var connectType = device.connectType;
        if (connectType == 3) {
          var deviceId = device.deviceId;
          var mac = lexin_util.getMacByDeviceId(deviceId);
          var index = payloads.uuid.indexOf(mac);
          if (index !== -1) {
            var connectDeviceIding = pages[0].getConnectDeviceIding();
            if (connectDeviceIding != deviceId && device.state != payloads.state) {
              isChanged = true;
              device.state = payloads.state;
              /// 离线的设备正好是当前设备
              if (device.state == "offline") {
                var currentConnnectDeviceId = pages[0].getCurrentDeviceId();
                if (currentConnnectDeviceId == deviceId) {
                  offlineConnectedCallback();
                }
              }
            }
            break;
          }
        }
      }
    }
  }
  ///更新数据
  if (isChanged) {
    changeCallback(deviceList);
  }
  _connectToDevice(resetCallback);
};


/// 连接设备
function _connectToDevice(resetCallback) {
  ///是否已登录
  var pages = getCurrentPages();
  var isLogin = pages[0].getIsLogin();
  if (!isLogin) {
    return;
  }

  ///当前没有连接设备，则去连接第一个wifi设备
  var deviceList = pages[0].getDeviceList();
  var deviceListSelect = pages[0].getDeviceListSelect();
  ///去连接第一个
  if (deviceListSelect === null) {
    const strings = require('../strings');
    if (!strings.isEmpty(deviceList)) {
      var autoConnected = pages[0].getAutoConnected();
      var connectDeviceIding = pages[0].getConnectDeviceIding();
      /// 还没有自动连接采用第一个
      if (!autoConnected) {
        for (var i = 0; i < deviceList.length; i++) {
          var device = deviceList[i];
          var deviceId = device.deviceId;
          if (device.connectType == 3 && device.state === "online") {
            connectDeviceIding = deviceId;
            break;
          }
        }
      }

      for (var i = 0; i < deviceList.length; i++) {
        var device = deviceList[i];
        var deviceId = device.deviceId;
        if (deviceId == connectDeviceIding) {
          if (device.connectType == 3 && device.state === "online") {
            pages[0].actionDevice(device);
          }
          break;
        }
      }
    }
  } else {
    // 当前播放设备离线
    if (deviceList.length > deviceListSelect && deviceList[deviceListSelect].state !== "online") {
      resetCallback();
    };
  }
}