export default {
  methods: {
    async openCamera(imContain) {
      var cameraLoaded = false
      const devicesList = await navigator.mediaDevices.enumerateDevices()
      this.videoDeviceAvailable = devicesList.some( d => d.kind == "videoinput")
      if (this.videoDeviceAvailable) {
        var vidConstraint = {
          video: {
            width: {
              ideal: imContain.offsetWidth
            },
            height: {
              ideal: imContain.offsetHeight
            },
            facingMode: 'environment'
          },
          audio: false
        }
        const stream = await navigator.mediaDevices.getUserMedia(vidConstraint);
        cameraLoaded = true
        this.cameraStream = stream
      }
      return cameraLoaded
    },
    closeCamera () {
      this.cameraStream.getTracks().forEach( t => t.stop())
      this.videoAvailable = false
    },
    captureVidFrame() {
      const vidViewer = this.$refs.vid_viewer
      vidViewer.pause()
      let tempCVS = document.createElement('canvas')
      tempCVS.height = vidViewer.videoHeight || parseInt(vidViewer.style.height)
      tempCVS.width = vidViewer.videoWidth || parseInt(vidViewer.style.width)
      const tempCtx = tempCVS.getContext('2d')
      tempCtx.drawImage(vidViewer, 0, 0)
      this.getImage(tempCVS.toDataURL())
    }
  }
}