/* eslint-disable */
import cardutil from './cardWebsocket'
var readCert = {
  // 一体式读证
  rcOpenCertDevice: function(callback) {
    if (cardutil.certWsStatus) {
      cardutil.wsReadIntegratedCard(function(openCallback) {
        console.log('openCallback', openCallback)
        callback(openCallback)
      })
    } else {
      readCert.rcReconnect('rcOpenCertDevice', function(resp) {
        callback(resp)
      })
    }
  },
  // 分离式获取秘钥
  rcSetAppParamEx: function(appKey, appSecret, password, rcSetAppParamExCallback) {
    if (cardutil.certWsStatus) {
      console.log(appKey, appSecret, password)
      cardutil.setAppParamEx(appKey, appSecret, password, function(openCallback) {
        console.log('openCallback', openCallback)
        rcSetAppParamExCallback(openCallback)
      })
    } else {
      cardutil.startWebSocket(function(res) {
        console.log('读证读卡ws链接状态1111', res)
        if (res) {
          cardutil.certWsStatus = true
          readCert.rcSetAppParamEx(appKey, appSecret, password, rcSetAppParamExCallback)
        } else {
          cardutil.certWsStatus = false
          var retcode = {
            err_code: -1,
            err_msg: 'ws连接失败，查看服务是否启动！！！',
            data: ''
          }
          rcSetAppParamExCallback(retcode)
        }
      })
    }
  },
  // 分离式读证
  rcReadCardEx: function(rcReadCardExCallback) {
    if (cardutil.certWsStatus) {
      cardutil.readCardEx(function(openCallback) {
        console.log('openCallback', openCallback)
        rcReadCardExCallback(openCallback)
      })
    } else {
      readCert.rcReconnect('rcReadCardEx', function(resp) {
        callback(resp)
      })
    }
  },
  rcReconnect: function(params, rcReconnectCallback) {
    cardutil.startWebSocket(function(res) {
      console.log('读证读卡ws链接状态1111', res)
      if (res) {
        cardutil.certWsStatus = true
        if (params == 'rcOpenCertDevice') {
          readCert.rcOpenCertDevice(rcReconnectCallback)
        }
        if (params == 'rcReadCardEx') {
          readCert.rcReadCardEx(rcReconnectCallback)
        }
      } else {
        cardutil.certWsStatus = false
        var retcode = {
          err_code: -1,
          err_msg: 'ws连接失败，查看服务是否启动！！！',
          data: ''
        }
        rcReconnectCallback(retcode)
      }
    })
  }
}
export default readCert
