module.exports = {
  getPosition: getPosition,
  getDeviceInfo: getDeviceInfo,
  getBattery: getBattery,
  getPlay: getPlay,
}

//  payloads.other: {"songInfoID":"843265795768623127","songFrom":"4","songAlbumID":"","artist":"巴洛克古典乐","title":"巴洛克古典乐","url":"http://speed.radio1964.cn/hls/1fmottosbaroquemusicradiozug.m3u8","albumURI":"http://airsmart-photo1.oss-cn-shanghai.aliyuncs.com/img/20211122/98101225b3cb4a0fb5728159ce4ca6d5.png","realUrl":"http://speed.radio1964.cn/hls/1fmottosbaroquemusicradiozug.m3u8","channel":5,"Duration":0,"RelTime":0,"PlayState":1,"PlayMode":0,"SongType":2,"userid":"1","channelType":1,"platformId":4,"platformName":"海外电台","isVip":0,"playable":1,"unplayableMsg":"","unplayableCode":0,"vipIconType":0,"vipIconTypeName":""}

function getPosition(other, canllback) {
  try {
    var actionIndex = null;
    const strings = require('../strings');
    var pages = getCurrentPages();
    var channelData = pages[0].getChannelData();
    if (!strings.isEmpty(channelData)) {
      channelData.map((v, index) => {
        if (v.channelNum === other.channel) {
          actionIndex = index;
        }
      });
    }
    canllback(actionIndex);
  } catch (e) {}
};

// payloads.other： {"Guid":"AIrSMArT_7cdfa1fd3af0","ProdModel":"MW-2AX(WIFI)","sim_iccid":"","esim_iccid":"","SoftVer":"3.0.7","Firmware":"RTOS","Power":50,"lowBatteryNtf":0,"Volume":27,"Mute":0,"pauseShutdown":0,"PlayState":1,"PlayMode":0,"devName":"猫王小王子OTR-X","deviceId":"","deviceSecret":"","is_tts_enable":1,"SignalQuality":100,"CardIndex":0,"CardSum":0,"NetModel":2,"is_netmode_auto":2,"Imei":"","mid":"","deviceDSN":"1044250776739909632","userid":"1","user_id_white":"1039072","alarm":[{"alarm_id":"0","alarm_name":"open","current_timestamp":"2024-12-12 00:00:00","on_off_timestamp":"16:19:25","operation":"on","song_uri":"","weekly_repeat":"0","enable":"0","action":"update","week_actives":[0,0,0,0,0,0,0]},{"alarm_id":"1","alarm_name":"open","current_timestamp":"2024-12-11 20:00:48","on_off_timestamp":"07:00:00","operation":"on","song_uri":"","weekly_repeat":"0","enable":"1","action":"update","week_actives":[0,0,0,0,0,0,0]}]}

function getDeviceInfo(payloads) {
  var other = payloads.other;
  var SrcDeviceName = payloads.SrcDeviceName;

  ///连上就调用2次 payloads.SrcDeviceName:AIrSMArT_7cdfa1fcbb24
  const app = getApp();
  const strings = require('../strings');
  const lexin_util = require('../lexin/util');
  var pages = getCurrentPages();
  var deviceList = pages[0].getDeviceList();
  if (!strings.isEmpty(deviceList)) {
    var currentDeviceId = "";
    /// 获取需要连接的那个设备，获取设备名称
    for (var i = 0; i < deviceList.length; i++) {
      if (SrcDeviceName) {
        var device = deviceList[i];
        var deviceId = device.deviceId;
        var mac = lexin_util.getMacByDeviceId(deviceId);
        var index = SrcDeviceName.indexOf(mac);
        ///彻底连接成功
        if (index !== -1) {
          currentDeviceId = deviceId;
          var devName = other.devName;
          var ProdModel = other.ProdModel;

          if (device.devName != devName) {
            device.devName = devName;
          }

          if (!strings.isEmpty(ProdModel)) {
            ///设置图片
            var classifyProducts = app.globalData.classifyProducts;
            if (!strings.isEmpty(classifyProducts)) {
              for (var i = 0; i < classifyProducts.length; i++) {
                if (classifyProducts.clientType == ProdModel) {
                  if (!strings.isEmpty(classifyProducts.img) && device.imageUrl != classifyProducts.img) {
                    device.imageUrl = classifyProducts.img;
                  }
                  break;
                }
              }
            }
          }

          if (device.ProdModel != ProdModel) {
            device.ProdModel = ProdModel;
          }
          break;
        }
      }
    }

    /// 去更新选中目标
    if (!strings.isEmpty(currentDeviceId)) {
      for (var i = 0; i < deviceList.length; i++) {
        var device = deviceList[i];
        var deviceId = device.deviceId;
        if (deviceId == currentDeviceId) {
          pages[0].refreshCurrentDevice(device);
          break;
        }
      }
    }
  }
};

///获取电量
function getBattery(battery) {
  var b = 0;
  if (battery < 20) {
    _babttery = 0
  } else if (20 <= battery && battery < 40) {
    b = 1
  } else if (40 <= battery && battery < 60) {
    b = 2
  } else if (60 <= battery && battery < 80) {
    b = 3
  } else if (80 <= battery && battery <= 100) {
    b = 4
  } else if (battery > 100) {
    b = 5
  };
  return b;
};

///播放和播放状态  PlayState:  ///播放状态：0默认状态，1播放状态，2暂停状态， 3停止状态，4缓冲状态。
function getPlay() {
  var pages = getCurrentPages();
  var deviceList = pages[0].getDeviceList();
  var deviceListSelect = pages[0].getDeviceListSelect();
  if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
    return;
  };

  const app = getApp();
  const lexin_util = require('../lexin/util');
  var deviceId = deviceList[deviceListSelect].deviceId;
  // 接收设备当前播放状态
  var deviceMacId = lexin_util.getDeviceMacId(deviceId);
  const obj = {
    DstDeviceName: deviceMacId
  }
  app.PubMsg({
    type: "get_position",
    ...obj
  });
};