module.exports = {
  addBlueDevice: addBlueDevice,
  addWifiDevice: addWifiDevice,
  updateDeviceList: updateDeviceList,
}

// {"applicationType":"[0, 1]","deviceId":"DB:45:DD:76:42:15","img":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100711728016597.png","offlineImg":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100714667384264.png","connectImg":null,"name":"猫王音响·小王子 OTR-X","bluetoothName":"猫王音响·小王子 OTR-X","bluetoothNames":["猫王音响·小王子 OTR-X"],"isChannelsPlatforms":0,"platform":-1,"typeList":[{"is5g":0,"type":1,"connectType":1,"functionList":[1,3,6],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100644913162836.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220909100648938942906.png","icon":null,"guideUrl":null}},{"is5g":0,"type":2,"connectType":3,"functionList":[1,3],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230313155903515728925.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20230313155910706032704.png","icon":null,"guideUrl":null}}],"clientType":"MW-2AX(WIFI)","firstVersion":"0.0.1","filter":null,"guideUrl":null,"manufacturer":"ShanJing","deviceType":0,"mac":"9b45dd76e2150000",

/// clientType 连接方式：bt-0,ble-1,upnp-2,mqtt-3
// [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
///新添加蓝牙设备
function addBlueDevice(device) {
  // 同一个设备处理
  var pages = getCurrentPages();
  var deviceList = pages[0].getDeviceList();
  var finalList = deviceList.filter((v) => {
    return v.deviceId != device.deviceId;
  });

  /// 添加到第一个
  finalList.unshift({
    connectType: 1,
    devName: device.name,
    state: "online",
    name: device.name,
    deviceId: device.deviceId,
    mac: device.mac,
    ProdModel: device.ProdModel || device.clientType,
    clientType: device.clientType || device.ProdModel,
    imageUrl: device.imageUrl,
  });
  return finalList;
};

// 新添加Wifi设备  猫王小王子OTR-X
// [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
function addWifiDevice(device) {
  // 同一个设备处理
  var pages = getCurrentPages();
  var deviceList = pages[0].getDeviceList();
  const strings = require('../strings');
  if (!strings.isEmpty(deviceList)) {
    deviceList = deviceList.filter((v) => v.deviceId != device.deviceId);
  }
  /// 添加到第一个
  deviceList.unshift(device);
  return deviceList;
};

///更新列表排序
function updateDeviceList(list, isInit, closeAllBlue) {
  const strings = require('../strings');
  if (strings.isEmpty(list)) {
    return [];
  }

  const app = getApp();
  var finalList = [];
  console.log("初始化蓝牙设备", list);

  if (isInit) {
    let device = list[0] ?? {};
    device.state = "offline";
  }

  /// 让所有蓝牙设备离线
  if (closeAllBlue) {
    list.forEach(element => {
      if (element.connectType != 3) {
        element.state = "offline";
      }
    });
  }

  ///挑选出第一个在线设备
  var isFirstOnline = false;
  if (list[0].state == "online") {
    isFirstOnline = true;
    finalList.push(list[0]);
  }

  ///区分在线和离线
  for (var i = isFirstOnline ? 1 : 0; i < list.length; i++) {
    if (isInit) {
      list[i].state = "offline";
    }
  }

  var onLineList = [];
  var onNoLineList = [];

  ///添加在线的
  for (var i = isFirstOnline ? 1 : 0; i < list.length; i++) {
    var device = list[i];
    if (device.state == "online") {
      onLineList.push(device)
    }
  }

  ///添加离线的
  for (var i = isFirstOnline ? 1 : 0; i < list.length; i++) {
    var device = list[i];
    if (device.state != "online") {
      onNoLineList.push(device)
    }
  }

  // 区分在线wifi和蓝牙 wifi在前 离线在后
  var onLineWifiList = [];
  var onLineBlueList = [];

  ///添加在线wifi
  onLineList.forEach(element => {
    if (element.connectType == 3) {
      onLineWifiList.push(element);
    }
  });

  ///添加在线蓝牙
  onLineList.forEach(element => {
    if (element.connectType != 3) {
      onLineBlueList.push(element)
    }
  });

  finalList = finalList.concat(onLineWifiList);
  finalList = finalList.concat(onLineBlueList);

  ///只需要蓝牙和wifi在线的
  var mDeviceList = []
  mDeviceList = mDeviceList.concat(onLineWifiList);
  mDeviceList = mDeviceList.concat(onLineBlueList);
  app.globalData.mDeviceList = mDeviceList;

  ///区分离线wifi和蓝牙 wifi在前 离线在后
  var onNoLineWifiList = [];
  var onNoLineBlueList = [];

  ///添加离线wifi
  onNoLineList.forEach(element => {
    if (element.connectType == 3) {
      onNoLineWifiList.push(element)
    }
  });

  ///添加离线蓝牙
  onNoLineList.forEach(element => {
    if (element.connectType != 3) {
      onNoLineBlueList.push(element)
    }
  });

  finalList = finalList.concat(onNoLineWifiList);
  finalList = finalList.concat(onNoLineBlueList);

  return finalList;
};