module.exports = {
  getMacByDeviceId: getMacByDeviceId,
  getMacBySplitDeviceId: getMacBySplitDeviceId,
  getDeviceMacId: getDeviceMacId,
  getResponseByDeviceId: getResponseByDeviceId,
  getResponseByDeviceMacId: getResponseByDeviceMacId,
}

///获取设备mac相关 macId: deviceId.split(lexin_devices.leXinÏ)[1]
function getMacBySplitDeviceId(splitDeviceId) {
  if (splitDeviceId.length < 12) {
    return splitDeviceId;
  }
  return splitDeviceId.slice(-12);
};

function getMacByDeviceId(deviceId) {
  const lexin_devices = require('../lexin/devices');
  var deviceIdLeXin = deviceId.split(lexin_devices.leXin);
  var deviceIdJunZheng1 = deviceId.split(lexin_devices.junZheng1);
  var deviceIdJunZheng2 = deviceId.split(lexin_devices.junZheng2);
  var deviceIdJunZheng3 = deviceId.split(lexin_devices.junZheng13);
  var isLeXin = deviceIdLeXin.length == 2;
  var isJunZheng1 = deviceIdJunZheng1.length == 2;
  var isJunZheng2 = deviceIdJunZheng2.length == 2;
  var isJunZheng3 = deviceIdJunZheng3.length == 2;
  if (!isLeXin && !(isJunZheng1 && !isJunZheng3) && !(isJunZheng2 && !isJunZheng3)) {
    return "";
  }

  if (isLeXin) {
    return getMacBySplitDeviceId(deviceIdLeXin[1]);
  } else if ((isJunZheng1 && !isJunZheng3)) {
    return getMacBySplitDeviceId(deviceIdJunZheng1[1]);
  } else if ((isJunZheng2 && !isJunZheng3)) {
    return getMacBySplitDeviceId(deviceIdJunZheng2[1]);
  } else {
    return "";
  }
};

///获取设备mac相关
function getDeviceMacId(deviceId) {
  const lexin_devices = require('../lexin/devices');
  var deviceIdLeXin = deviceId.split(lexin_devices.leXin);
  var deviceIdJunZheng1 = deviceId.split(lexin_devices.junZheng1);
  var deviceIdJunZheng2 = deviceId.split(lexin_devices.junZheng2);
  var deviceIdJunZheng3 = deviceId.split(lexin_devices.junZheng13);
  var isLeXin = deviceIdLeXin.length == 2;
  var isJunZheng1 = deviceIdJunZheng1.length == 2;
  var isJunZheng2 = deviceIdJunZheng2.length == 2;
  var isJunZheng3 = deviceIdJunZheng3.length == 2;
  if (!isLeXin && !(isJunZheng1 && !isJunZheng3) && !(isJunZheng2 && !isJunZheng3)) {
    return "";
  }

  if (isLeXin) {
    var result = getMacBySplitDeviceId(deviceIdLeXin[1]);
    return `${lexin_devices.junZheng1}${result}`;
  } else if ((isJunZheng1 && !isJunZheng3)) {
    var result = getMacBySplitDeviceId(deviceIdJunZheng1[1]);
    return `${lexin_devices.junZheng1}${result}`;
  } else if ((isJunZheng2 && !isJunZheng3)) {
    var result = getMacBySplitDeviceId(deviceIdJunZheng2[1]);
    return `${lexin_devices.junZheng1}${result}`;
  } else {
    return "";
  }
};

///通过设备id获取mac地址
function getResponseByDeviceId(deviceId) {
  var deviceMacId = getDeviceMacId(deviceId);
  return getResponseByDeviceMacId(deviceMacId);
};

///通过mac地址过去请求 /AIrSMArT_7cdfa1fd3af0/user/pub_response
function getResponseByDeviceMacId(deviceMacId) {
  return `/${deviceMacId}/user/pub_response`;
};