// 刷脸初始化
const faceInit = () => {
  let params = {
    "action": "initFace",
    "event": "open",
    "taskId": "2",
    "params": {
      "scope": "auth_user,order_service"
    }
  }
  my.call("ampeHHCommunication", params, res => {
    console.log(res)
    return res
  });
}
// 刷脸返回
const faceScan = () => {
  let params1 = {
    "action": "getFaceInfo",
    "event": "open",
    "taskId": "2"
  }
  my.showLoading();
  my.call("ampeHHCommunication", params1, res => {
    console.log(res)
    my.hideLoading()
    return res
  });
};
// 获取设备信息
const deviceInformation = () => {
  let params = {
    "action": "getDeviceInfo",
    "event": "open",
    "taskId": "4"
  }
  my.call('ampeHHCommunication', params, res => {
    console.log(res);
    return res
  });
}
// 刷证
const brushingCard = async () => {
  let params = {
    "action": "getIDCardInfo",
    "event": "open",
    "taskId": "3",
    "params": {
      "infoType": "base,photo,other"
    }
  }
  await my.call('ampeHHCommunication', params, res => {
    console.log(res);
    return res
  });
}
// 刷码
// const brushingCard = () => {
//   let params = {
//     "action": "getIDCardInfo",
//     "event": "open",
//     "taskId": "3",
//     "params": {
//       "infoType": "base,photo,other"
//     }
//   }
//   my.call('ampeHHCommunication', params, res => {
//     return res
//   });
// }
// 播报语音
const viceBroadcast = (text, speechQueue = 0) => {
  let params = {
    "action": "speech",
    "event": "open",
    "taskId": "8",
    "params": {
      "text": text,
      "pitch": '1.0f',
      "rate": '1.0f',
      speechQueue
    }
  }
  try {
    my.call("ampeHHCommunication", params, res => {
      // console.log(res);
    })
  } catch (error) {
    console.log(error);
  }
}
// 暂停语音
const stopVoiceBroadcast = () => {
  let params = {
    "action": "speechStop",
    "event": "open",
    "taskId": "8"
  }
  try {
    my.call("ampeHHCommunication", params, res => {
      // console.log(res);
    })
  } catch (error) {
    console.log(error);
  }
}
const openCycleSecondaryScreen = () => {
  const app = getApp()
  console.log(app)
  app.data.secondaryPageImagesIndex = 0
  app.data.cycleIntervalSwitch = true
  let url = app.globalData.snDisposition.secondaryPageImages[app.data.secondaryPageImagesIndex] || ''
  if (url) {
    openSecondaryScreen(url)
  }
  app.data.cycleIntervalId && clearInterval(app.data.cycleIntervalId);
  app.data.cycleIntervalId = setInterval(() => {
    if (app.data.cycleIntervalSwitch) {
      console.log( app.data.secondaryPageImagesIndex, app.globalData.snDisposition.secondaryPageImages.length, new Date())
      // closeSecondaryScreen()
      let cycleUrl = app.globalData.snDisposition.secondaryPageImages[app.data.secondaryPageImagesIndex] || ''
      if (cycleUrl) {
        openSecondaryScreen(cycleUrl)
      }
      app.data.secondaryPageImagesIndex++
      if (app.data.secondaryPageImagesIndex == app.globalData.snDisposition.secondaryPageImages.length) {
        app.data.secondaryPageImagesIndex = 0
      }
    }
  }, 10* 1000)
}

const closeCycleSecondaryScreen = () => {
  const app = getApp()
  app.data.secondaryPageImagesIndex = 0
  app.data.cycleIntervalSwitch = false
  app.data.cycleIntervalId && clearInterval(app.data.cycleIntervalId);
  closeSecondaryScreen()
}

const openSecondaryScreen = (url) => {
  let imgBase64 = url
  if(!imgBase64) {
    imgBase64 = 'data:image/png;base64,[encoded data, 714272 characters]'
  }
  let params = {
  "action": "openSecondaryScreen",
  "event": "open",
  "taskId": "10",
  "params": {
    "secondaryScreenBase64": imgBase64
  }
}
  try {
    my.call("ampeHHCommunication", params, res => {
      // console.log(res);
    })
  } catch (error) {
    console.log(error);
  }
}

const closeSecondaryScreen = () => {
  let params = {
    "action": "closeSecondaryScreen",
    "event": "open",
    "taskId": "10",
  }
  try {
    my.call("ampeHHCommunication", params, res => {
      // console.log(res);
    })
  } catch (error) {
    console.log(error);
  }
}
module.exports = {
  faceInit: faceInit,
  faceScan: faceScan,
  deviceInformation: deviceInformation,
  brushingCard: brushingCard,
  viceBroadcast: viceBroadcast,
  stopVoiceBroadcast: stopVoiceBroadcast,
  openSecondaryScreen: openSecondaryScreen,
  openCycleSecondaryScreen: openCycleSecondaryScreen,
  closeSecondaryScreen: closeSecondaryScreen,
  closeCycleSecondaryScreen: closeCycleSecondaryScreen
};
