var clientId = '1234567890'
var deviceName = 'SR236'
var deviceType = 0
var signutil = {
  signWsStatus: false,
  // 主通道，用于打开设备、关闭设备等
  websocket: null,
  wsUrl: 'ws://127.0.0.1:32561/',
  // wsUrl: 'ws://192.168.200.56:32561/',
  // 用于签字坐标返回数据的通道
  websocketSc: null,
  wsUrlSc: 'ws://127.0.0.1:42561/',
  // wsUrlSc: 'ws://192.168.200.56:42561/',
  // 获取设备状态
  deviceStatusParams: {
    cmd: 'get_device_status',
    client_id: clientId,
    device_name: deviceName,
    device_type: 1
  },
  // 打开设备
  openParams: {
    cmd: 'open_device',
    client_id: clientId,
    device_name: deviceName,
    device_type: 1,
    recv_msg_port: 42561,
    'ui_type': 2,
    'x': 0,
    'y': 0,
    'show_text': 0,
    'time': 0
  },
  // 获取设备信息
  deviceInfoParams: {
    cmd: 'get_device_info',
    client_id: clientId,
    device_name: deviceName,
    device_type: 1
  },
  // 确认签字
  signConfirmParams: {
    cmd: 'sign_confirm',
    client_id: clientId,
    device_name: deviceName,
    device_type: deviceType
  },
  // 重签
  againSignParams: {
    cmd: 'revived_sign',
    client_id: clientId,
    device_name: deviceName,
    device_type: 1
  },
  // 取消签字
  signCancelParams: {
    cmd: 'sign_cancel',
    client_id: clientId,
    device_name: deviceName,
    device_type: 1
  },
  openDeviceCallback: null, // 打开设备
  connectCallback: null, // 链接回调
  getCoordinateCallback: null,
  deviceInfoCallback: null, // 设备信息回调
  signConfirmCallback: null,
  revivedSignCallback: null, // 重签
  getDeviceStatusCallback: null, // 获取设备状态
  deviceTriggerCallback: null, //
  logMessage: function(message) {
    if (typeof window.onHandleMessage !== 'undefined') { window.onHandleMessage(message) } else { console.log(message) }
  },
  // 连接主通道的websocket
  startWebSocket: function(callback) {
    signutil.connectCallback = callback
    if ('WebSocket' in window) {
      signutil.websocket = new WebSocket(signutil.wsUrl)
    } else if ('MozWebSocket' in window) {
      signutil.websocket = new WebSocket(signutil.wsUrl)
    } else {
      window.alert('浏览器不支持WebSocket')
      return
    }
    signutil.websocket.binaryType = 'arraybuffer'
    signutil.websocket.onopen = function() {
      console.log('Connected 主通道的URL: ', signutil.wsUrl)
      if (signutil.websocket.readyState === 1) {
        console.log('链接成功')
        signutil.connectCallback(true)
      }
    }
    signutil.websocket.onmessage = function(evt) {
      signutil.wsMessage(evt)
    }
    signutil.websocket.onclose = function(evt) {
      if (signutil.websocket.readyState === 3) {
        console.log('链接关闭', evt)
        signutil.connectCallback(false)
      }
    }
    signutil.websocket.onerror = function(evt) {
      if (signutil.websocket.readyState === 3) {
        console.log('链接报错', evt)
        signutil.connectCallback(false)
      }
    }
  },
  // 连接签字坐标通道的websocket
  startWebSocketSc: function(callback1, callback2) {
    const WebSocketScCallback = callback1
    signutil.getCoordinateCallback = callback2
    if ('WebSocket' in window) {
      signutil.websocketSc = new WebSocket(signutil.wsUrlSc)
    } else if ('MozWebSocket' in window) {
      signutil.websocketSc = new WebSocket(signutil.wsUrlSc)
    } else {
      window.alert('浏览器不支持WebSocket')
      return
    }
    signutil.websocketSc.binaryType = 'arraybuffer'
    signutil.websocketSc.onopen = function() {
      console.log('签字坐标URL链接成功ready')
      console.log('签字坐标URL链接成功', signutil.websocketSc)
      console.log('Connected 签字坐标URL: ', signutil.wsUrlSc)
      if (signutil.websocketSc.readyState === 1) {
        console.log('签字坐标URL链接成功')
        WebSocketScCallback(true)
      }
    }
    signutil.websocketSc.onmessage = function(evt) {
      signutil.wsMessageSc(evt)
    }
    signutil.websocketSc.onclose = signutil.onSocketClose
    signutil.websocketSc.onerror = signutil.onSocketError
  },
  // 发送信息
  sendMsg: function(param) {
    // console.log('发送信息', signutil.websocket, param)
    if (signutil.websocket && param) {
      signutil.websocket.send(JSON.stringify(param))
    }
  },
  // websocket主通道的数据返回
  wsMessage: function(res) {
    res = JSON.parse(res.data)
    // console.log('主通道的返回', res)
    var cmd = res.cmd
    switch (cmd) {
      case 'get_device_status':
        signutil.getDeviceStatusCallback(res)
        break
      case 'open_device':
        signutil.openDeviceCallback(res)
        break
      case 'get_device_info':
        signutil.deviceInfoCallback(res)
        break
      case 'sign_cancel':
        if (signutil.signCancelCallback != null) {
          signutil.signCancelCallback(res)
        } else {
          signutil.openDeviceCallback(res)
        }
        break
      case 'revived_sign':
        if (signutil.revivedSignCallback != null) {
          signutil.revivedSignCallback(res)
        } else {
          signutil.openDeviceCallback(res)
        }
        break
      case 'sign_pic':
        if (signutil.signConfirmCallback != null) {
          signutil.signConfirmCallback(res)
        } else {
          signutil.openDeviceCallback(res)
        }
        break
    }
  },
  // 签字坐标通道的数据返回
  wsMessageSc: function(res) {
    // console.log('签字坐标通道的数据返回', res)
    signutil.getCoordinateCallback(res)
  },
  // 断开检测服务器连接
  cwStopWebSocket: function() {
    if (signutil.websocket || signutil.websocketSc) {
      if (signutil.websocket.readyState === 1 || signutil.websocketSc.readyState === 1) { signutil.websocket.close() }
      signutil.websocketSc.close()
      signutil.websocket = null
      signutil.websocketSc = null
      return true
    } else {
      return false
    }
  },
  // 服务连接出错
  onSocketError: function(evt) {
    signutil.logMessage('连接检测服务有问题...')
  },
  // 服务连接关闭onSocketClose
  onSocketClose: function(evt) {
    // websocket = null;
    signutil.websocketSc = null
    signutil.logMessage('服务已断开...')
  },
  // 获取设备状态
  getDeviceStatus: function(callback) {
    signutil.getDeviceStatusCallback = callback
    signutil.sendMsg(signutil.deviceStatusParams)
  },
  // 打开设备
  openDevice: function(callback) {
    signutil.openDeviceCallback = callback
    signutil.sendMsg(signutil.openParams)
  },
  // 获取设备信息
  getDeviceInfo: function(callback) {
    signutil.deviceInfoCallback = callback
    signutil.sendMsg(signutil.deviceInfoParams)
  },
  // 取消签名
  signCancel: function(callback) {
    signutil.signCancelCallback = callback
    signutil.sendMsg(signutil.signCancelParams)
  },
  // 重签
  revivedSign: function(callback) {
    signutil.revivedSignCallback = callback
    signutil.sendMsg(signutil.againSignParams)
  },
  // 确认签名
  signConfirm: function(callback) {
    signutil.signConfirmCallback = callback
    signutil.sendMsg(signutil.signConfirmParams)
  }
}
export default signutil
