/* eslint-disable */
var clientId = '1234567890'
var videoutil = {
  videoWsStatus: false,
  // 主通道，用于打开设备、关闭设备等
  websocket: null,
  wsUrl: 'ws://127.0.0.1:34256/',
  // wsUrl: 'ws://192.168.200.56:34256/',
  connectCallback: null,
  // 用于视频图片返回数据的通道
  websocketSc: null,
  wsUrlSc: 'ws://127.0.0.1:34257/',
  // wsUrlSc: 'ws://192.168.200.56:34257/',
  WebSocketScCallback: null,
  // 拍照设备信息
  videoDeviceParams: {
    cmd: 'get_camera_info',
    client_id: clientId
  },
  // 打开视频
  singleVideoCheckParams: {
    cmd: 'playvideo',
    camera_type: 0,
    camera_index: -1,
    camera_name: '',
    client_id: clientId,
    camera_path: '',
    video_frame_w: '',
    video_frame_h: '',
    recv_video_port: 34257
  },
  // 静态边缘检测
  edgeParams: {
    cmd: 'show_static_edge',
    camera_type: 0,
    camera_index: -1,
    camera_name: '',
    client_id: clientId,
    statuCode: 1,
    camera_path: '',
    message: ''
  },
  // 拍照
  takeCutPicParams: {
    cmd: 'take_pic',
    camera_type: 0,
    camera_index: -1,
    camera_name: '',
    client_id: clientId,
    camera_path: '',
    rotate_angle: 0
  },
  // 关闭视频
  stopvideoParams: {
    cmd: 'stopvideo',
    camera_type: 0,
    camera_index: -1,
    camera_name: '',
    client_id: clientId,
    camera_path: ''
  },
  deviceInfoCallback: null, // 获取设备信息
  singleVideoCheckCallback: null, // 打开视频
  startEdgeCheckCallback: null, // 静态边缘
  takeCutPicCallback: null, // 拍照
  videoConnectCallback: null, // 拍照连接回调
  // 34257的链接回调
  startWebSocketCallback: null,
  playVideoCallback: null,
  stopVideoCallback: null, // 停止拍照
  logMessage: function(message) {
    if (typeof window.onHandleMessage !== 'undefined') { window.onHandleMessage(message) } else { console.log(message) }
  },
  // 连接主通道的websocket
  startWebSocket: function(callback) {
    videoutil.connectCallback = callback
    if ('WebSocket' in window) {
      videoutil.websocket = new WebSocket(videoutil.wsUrl)
    } else if ('MozWebSocket' in window) {
      videoutil.websocket = new MozWebSocket(videoutil.wsUrl)
    } else {
      window.alert('浏览器不支持WebSocket')
      return
    }
    videoutil.websocket.binaryType = 'arraybuffer'
    videoutil.websocket.onopen = function() {
      console.log('Connected 主通道的URL: ', videoutil.wsUrl)
      if (videoutil.websocket.readyState == 1) {
        console.log('链接成功')
        videoutil.connectCallback(true)
      }
    }
    videoutil.websocket.onmessage = function(evt) {
      videoutil.wsMessage(evt)
    }
    videoutil.websocket.onclose = function(evt) {
      if (videoutil.websocket.readyState == 3) {
        console.log('链接关闭', evt)
        videoutil.connectCallback(false)
      }
    }
    videoutil.websocket.onerror = function(evt) {
      if (videoutil.websocket.readyState == 3) {
        console.log('链接报错', evt)
        videoutil.connectCallback(false)
      }
    }
  },
  // 连接34257通道的websocket
  startWebSocketSc: function(callback1, callback2) {
    var WebSocketScCallback = callback1
    videoutil.getCoordinateCallback = callback2
    if ('WebSocket' in window) {
      videoutil.websocketSc = new WebSocket(videoutil.wsUrlSc)
    } else if ('MozWebSocket' in window) {
      videoutil.websocketSc = new MozWebSocket(videoutil.wsUrlSc)
    } else {
      window.alert('浏览器不支持WebSocket')
      return
    }
    videoutil.websocketSc.binaryType = 'arraybuffer'
    videoutil.websocketSc.onopen = function() {
      console.log('Connected 34257端口: ', videoutil.wsUrlSc)
      if (videoutil.websocketSc.readyState == 1) {
        console.log('34257端口链接成功')
        WebSocketScCallback(true)
      }
    }
    videoutil.websocketSc.onmessage = function(evt) {
      videoutil.wsMessageSc(evt)
    }
    videoutil.websocketSc.onclose = videoutil.onSocketClose
    videoutil.websocketSc.onerror = videoutil.onSocketError
  },
  // 发送信息
  sendMsg: function(param) {
    // console.log('发送信息', videoutil.websocket, param)
    if (videoutil.websocket && param) {
      videoutil.websocket.send(JSON.stringify(param))
    }
  },
  // websocket主通道的数据返回
  wsMessage: function(res) {
    // console.log('主通道的返回', res)
    var res = JSON.parse(res.data)
    var cmd = res.cmd
    switch (cmd) {
      case 'get_camera_info': // 获取设备信息
        videoutil.deviceInfoCallback(res)
        break
      case 'play_video_status': // 打开视频
        if (videoutil.singleVideoCheckCallback) {
          videoutil.singleVideoCheckCallback(res)
        }
        break
      case 'show_static_edge': // 静态边缘
        if (videoutil.startEdgeCheckCallback) {
          videoutil.startEdgeCheckCallback(res)
        }
        break
      case 'take_pic':
        videoutil.takeCutPicCallback(res)
        break
      case 'stopvideo': // 停止拍照
        videoutil.stopVideoCallback(res)
        break
    }
  },
  // 视频通道的数据返回
  wsMessageSc: function(res) {
    // console.log('视频通道的数据返回', res)
    videoutil.getCoordinateCallback(res)
  },
  // 断开检测服务器连接
  cwStopWebSocket: function() {
    if (videoutil.websocket || videoutil.websocketSc) {
      if (videoutil.websocket.readyState == 1 || videoutil.websocketSc.readyState == 1) { videoutil.websocket.close() }
      videoutil.websocketSc.close()
      videoutil.websocket = null
      videoutil.websocketSc = null
      return true
    } else {
      return false
    }
  },
  // 服务连接出错
  onSocketError: function(evt) {
    videoutil.logMessage('连接检测服务有问题...')
  },
  // 服务连接关闭onSocketClose
  onSocketClose: function(evt) {
    // websocket = null;
    videoutil.websocketSc = null
    videoutil.logMessage('服务已断开...')
  },
  // 获取设备信息
  getDeviceInfo: function(callback) {
    videoutil.deviceInfoCallback = callback
    videoutil.sendMsg(videoutil.videoDeviceParams)
  },
  // 打开视频
  singleVideoCheck: function(param, callback) {
    console.log('ws打开视频参数', param)
    videoutil.singleVideoCheckCallback = callback
    videoutil.singleVideoCheckParams.camera_type = param.camera_type
    videoutil.singleVideoCheckParams.camera_index = param.camera_index
    videoutil.singleVideoCheckParams.camera_name = param.camera_name
    videoutil.singleVideoCheckParams.camera_path = param.camera_path
    videoutil.singleVideoCheckParams.video_frame_w = parseInt(param.video_frame_w)
    videoutil.singleVideoCheckParams.video_frame_h = parseInt(param.video_frame_h)
    videoutil.sendMsg(videoutil.singleVideoCheckParams)
  },
  // 开启静态边缘检测
  startEdgeCheck: function(param, callback) {
    videoutil.singleVideoCheckCallback = callback
    videoutil.edgeParams.camera_type = param.camera_type
    videoutil.edgeParams.camera_index = param.camera_index
    videoutil.edgeParams.camera_name = param.camera_name
    videoutil.edgeParams.camera_path = param.camera_path
    videoutil.edgeParams.statuCode = 1
    videoutil.sendMsg(videoutil.edgeParams)
    videoutil.startEdgeCheckCallback = callback
  },
  // 关闭静态边缘检测
  stopEdgeCheck: function(param, callback) {
    videoutil.singleVideoCheckCallback = callback
    videoutil.edgeParams.camera_type = param.camera_type
    videoutil.edgeParams.camera_index = param.camera_index
    videoutil.edgeParams.camera_name = param.camera_name
    videoutil.edgeParams.camera_path = param.camera_path
    videoutil.edgeParams.statuCode = 0
    videoutil.sendMsg(videoutil.edgeParams)
    videoutil.startEdgeCheckCallback = callback
  },
  // 停止摄像头
  stopVideo: function(param, callback) {
    videoutil.stopVideoCallback = callback

    videoutil.stopvideoParams.camera_type = param.camera_type
    videoutil.stopvideoParams.camera_index = param.camera_index
    videoutil.stopvideoParams.camera_name = param.camera_name
    videoutil.stopvideoParams.camera_path = param.camera_path
    videoutil.sendMsg(videoutil.stopvideoParams)
  },
  // 拍照
  takeCutPic: function(param, callback) {
    videoutil.takeCutPicCallback = callback
    videoutil.takeCutPicParams.camera_type = param.camera_type
    videoutil.takeCutPicParams.camera_index = param.camera_index
    videoutil.takeCutPicParams.camera_name = param.camera_name
    videoutil.takeCutPicParams.camera_path = param.camera_path
    videoutil.takeCutPicParams.rotate_angle = param.rotate_angle * 1
    videoutil.sendMsg(videoutil.takeCutPicParams)
  }
}
export default videoutil
