         
         guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else {
             // TODO: Handle the case without permission "Permission"
+            failed(QRReaderError.NO_PERMISSION)
             return
         }
         

        captureSession.stopRunning()
    }
    
    func failed() {
        print("Scanning unsupported")
        captureSession = nil
    }
    
    // override func viewWillAppear(_ animated: Bool) {
    //     super.viewWillAppear(animated)
    
    //     if (captureSession?.isRunning == false) {
    //         captureSession.startRunning()
    //     }
    // }
    
    // override func viewWillDisappear(_ animated: Bool) {
    //     super.viewWillDisappear(animated)
    
    //     if (captureSession?.isRunning == true) {
    //         captureSession.stopRunning()
    //     }
    // }
    
    func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
        captureSession.stopRunning()

            return
        }
        
        guard captureSession.canAddInput(videoInput) else {
            failed(QRReaderError.SCANNING_UNSUPPORTED)
            return
        }
        captureSession.addInput(videoInput)

        let metadataOutput = AVCaptureMetadataOutput()
        
        guard captureSession.canAddOutput(metadataOutput) else {
            failed(QRReaderError.SCANNING_UNSUPPORTED)
            return
        }
        captureSession.addOutput(metadataOutput)

        metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        metadataOutput.metadataObjectTypes = [.qr]
        
        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        previewLayer.frame = self.layer.bounds
        previewLayer.videoGravity = .resizeAspectFill

        self.layer.addSublayer(previewLayer)
        
        captureSession.startRunning()
