/*
 * @Company: 思宏科技
 * @Author: lvdexin
 * @Date: 2022-08-24 15:17:08
 * @LastEditors: lvdexin
 * @LastEditTime: 2022-08-25 14:46:55
 */
// 需求：用户停留在页面，到了探视开始时间 进入探视按钮可以被点击
import dayjs from 'dayjs'
let nableBtnTimerId
function setEnableBtnTimer(detailInfo, settings, callback) {
  console.info('info setEnableBtnTimer params', detailInfo, settings)
  if (detailInfo.visitState !== 'TO_BE_START') return
  if (nableBtnTimerId) clearTimeout(nableBtnTimerId)
  const aheadEnterTime = (settings.aheadEnterSwitch ? 1000 * 60 * settings.aheadEnterTime : 0)
  const now = Date.now()
  const startTime = dayjs(detailInfo.visitStartTime, 'YYYY-MM-DD HH:mm:ss').valueOf()
  const restTimestamp = startTime - aheadEnterTime - now
  nableBtnTimerId = setTimeout(() => {
    callback()
    console.info('info run setEnableBtnTimer callback')
    clearTimeout(nableBtnTimerId)
    nableBtnTimerId = undefined
    console.info('info clear setEnableBtnTimer timer:', nableBtnTimerId)
  }, restTimestamp)
  const info = {
    visitStartTime: detailInfo.visitStartTime,
    now,
    startTime,
    aheadEnterTime,
    restTimestamp,
    nableBtnTimerId
  }
  console.info('info setEnableBtnTimer ', info)
  return info
}
module.exports = {
  setEnableBtnTimer
}
