/* eslint-disable */
import videoutil from './videoWebsocket'
var takePhoto = {
  videoParams: {
    camera_type: 0,
    camera_index: 0,
    camera_name: '',
    camera_path: '',
    video_frame_w: 0,
    video_frame_h: 0,
    rotate_angle: 0
  },
  cameraInfo: [],
  cutStatus: 0, // //0代表裁切，1代表不裁切
  // 获取设备信息
  tpGetVideoInfo: function(tpGetVideoInfocallback) {
    if (videoutil.videoWsStatus) {
      videoutil.getDeviceInfo(function(res) {
        // console.log('获取设备信息返回', res)
        if (res.statuCode == 0) {
          takePhoto.cameraInfo = res.camera_info
          // console.log('sfsdfe sd s ', takePhoto.cameraInfo)
        }
        var recote = {
          err_code: res.statuCode,
          err_msg: res.message,
          data: res.camera_info
        }
        tpGetVideoInfocallback(recote)
      })
    } else {
      takePhoto.tpReconnect('tpGetVideoInfo', function(resp) {
        tpGetVideoInfocallback(resp)
      })
    }
  },
  tpMasterOpenVideo: function(videoParams, cutStatus, tpMasterOpenVideocallback) { // 打开视频
    takePhoto.cutStatus = cutStatus
    takePhoto.videoParams.camera_type = videoParams.camera_type
    takePhoto.videoParams.video_frame_w = videoParams.video_frame_w
    takePhoto.videoParams.video_frame_h = videoParams.video_frame_h
    var cameraInfo = takePhoto.cameraInfo
    for (var i = 0; i < cameraInfo.length; i++) {
      if (cameraInfo[i].camera_type == videoParams.camera_type) {
        var resIndex = i
        takePhoto.videoParams.camera_index = cameraInfo[resIndex].device_index
        takePhoto.videoParams.camera_name = cameraInfo[resIndex].camera_name
        takePhoto.videoParams.camera_path = cameraInfo[resIndex].camera_path
        break
      }
    }
    console.log('打开视频videoParams', takePhoto.videoParams)
    if (videoutil.videoWsStatus) {
      // console.log('1111', takePhoto.videoParams)
      videoutil.singleVideoCheck(takePhoto.videoParams, function(singleVideoCheckCallback) {
        console.log('打开视频的回调', singleVideoCheckCallback)
        if (singleVideoCheckCallback && singleVideoCheckCallback.statuCode == 0) {
          videoutil.startWebSocketSc(function(startWebSocketCallback) {
            console.log('34257ws连接回调', startWebSocketCallback)
            if (!startWebSocketCallback) {
              var recote = {
                err_code: -1,
                err_msg: '34257ws连接关闭',
                data: ''
              }
            } else {
              if (takePhoto.cutStatus == 0) {
                // 主头开启静态边缘检测
                console.log('摄像头', takePhoto.videoParams.camera_type)
                if (takePhoto.videoParams.camera_type == 0) {
                  videoutil.startEdgeCheck(takePhoto.videoParams, function(resp) {
                    console.log('打开静态边缘的回调', resp)
                  })
                }
              }
            }
          },
          function(playVideoCallback) {
            // console.log('视频返回', playVideoCallback)
            var recote = {
              err_code: 0,
              err_msg: '打开视频成功',
              data: playVideoCallback.data
            }
            tpMasterOpenVideocallback(recote)
          })
        } else {
          var recote = {
            err_code: singleVideoCheckCallback.statuCode,
            err_msg: singleVideoCheckCallback.message,
            data: ''
          }
          tpMasterOpenVideocallback(recote)
        }
      })
    } else {
      // 连接ws
      videoutil.startWebSocket(function(res) {
        console.log('打开视频ws链接状态1111', res)
        if (res) {
          videoutil.videoWsStatus = true
          takePhoto.tpMasterOpenVideo(takePhoto.videoParams, tpMasterOpenVideocallback)
        } else {
          videoutil.videoWsStatus = false
          var retcode = {
            err_code: -1,
            err_msg: 'ws连接失败，查看服务是否启动！！！',
            data: ''
          }
          tpMasterOpenVideocallback(retcode)
        }
      })
    }
  },
  // 主头拍照
  tpMasterTakePhoto: function(tpMasterTakePhotocallback) {
    if (videoutil.videoWsStatus) {
      videoutil.takeCutPic(takePhoto.videoParams, function(openCallback) {
        console.log('拍照', openCallback)
        var recote = {
          err_code: openCallback.statuCode,
          err_msg: openCallback.message,
          data: ''
        }
        if (openCallback.statuCode == 0) {
          recote.data = openCallback.pic_data_arr[0].pic_data
        }
        tpMasterTakePhotocallback(recote)
      })
    } else {
      takePhoto.tpReconnect('tpMasterTakePhoto', function(resp) {
        tpMasterTakePhotocallback(resp)
      })
    }
  },
  // 副头拍照
  tpslaveTakePhoto: function(tpslaveTakePhotocallback) {
    if (videoutil.videoWsStatus) {
      videoutil.takeCutPic(takePhoto.videoParams, function(openCallback) {
        console.log('拍照', openCallback)
        var recote = {
          err_code: openCallback.statuCode,
          err_msg: openCallback.message,
          data: ''
        }
        if (openCallback.statuCode == 0) {
          recote.data = openCallback.pic_data_arr[0].pic_data
        }
        tpslaveTakePhotocallback(recote)
      })
    } else {
      takePhoto.tpReconnect('tpslaveTakePhoto', function(resp) {
        tpslaveTakePhotocallback(resp)
      })
    }
  },
  // 旋转角度
  tpRoteAngle: function(rotate_angle) {
    console.log('角度', rotate_angle)
    if (rotate_angle == 90) {
      rotate_angle = 1
    }
    if (rotate_angle == 180) {
      rotate_angle = 2
    }
    if (rotate_angle == 270) {
      rotate_angle = 3
    }
    takePhoto.videoParams.rotate_angle = rotate_angle
  },
  tpCloseVideo: function(tpCloseVideoCallback) {
    if (videoutil.videoWsStatus) {
      if (takePhoto.cutStatus == 0) {
        videoutil.stopEdgeCheck(takePhoto.videoParams)
      }
      videoutil.stopVideo(takePhoto.videoParams, function(res) {
        console.log('关闭视频回调', res)
        if (res.statuCode == 0) {
          if (videoutil.websocketSc) {
            videoutil.websocketSc.close()
          }
        }
        var recote = {
          err_code: res.statuCode,
          err_msg: res.message
        }
        tpCloseVideoCallback(recote)
      })
    } else {
      takePhoto.tpReconnect('tpCloseVideo', function(resp) {
        tpCloseVideoCallback(resp)
      })
    }
  },
  tpReconnect: function(params, tpReconnectCallback) {
    // 连接ws
    videoutil.startWebSocket(function(res) {
      console.log('读证读卡ws链接状态1111', res)
      if (res) {
        videoutil.videoWsStatus = true
        if (params == 'tpGetVideoInfo') {
          takePhoto.tpGetVideoInfo(tpReconnectCallback)
        }
        // if (params == 'tpMasterOpenVideo') {
        //     takePhoto.tpMasterOpenVideo(tpReconnectCallback);
        // }
        if (params == 'tpMasterTakePhoto') {
          takePhoto.tpMasterTakePhoto(tpReconnectCallback)
        }
        if (params == 'tpslaveTakePhoto') {
          takePhoto.tpslaveTakePhoto(tpReconnectCallback)
        }
        if (params == 'tpCloseVideo') {
          takePhoto.tpCloseVideo(tpReconnectCallback)
        }
      } else {
        videoutil.videoWsStatus = false
        var retcode = {
          err_code: -1,
          err_msg: 'ws连接失败，查看服务是否启动！！！',
          data: ''
        }
        tpReconnectCallback(retcode)
      }
    })
  }
}

export default takePhoto
