<commit_before>//
//  BRCameraPlugin.swift
//  BreadWallet
//
//  Created by <NAME> on 10/9/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

@available(iOS 8.0, *)
@objc open class BRCameraPlugin: NSObject, BRHTTPRouterPlugin, UIImagePickerControllerDelegate,
                                   UINavigationControllerDelegate, CameraOverlayDelegate {
    
    let controller: UIViewController
    var response: BRHTTPResponse?
    var picker: UIImagePickerController?
    
    init(fromViewController: UIViewController) {
        self.controller = fromViewController
        super.init()
    }
    
    open func hook(_ router: BRHTTPRouter) {
        // GET /_camera/take_picture
        //
        // Optionally pass ?overlay=<id> (see overlay ids below) to show an overlay
        // in picture taking mode
        //
        // Status codes:
        //   - 200: Successful image capture
        //   - 204: User canceled image picker
        //   - 404: Camera is not available on this device
        //   - 423: Multiple concurrent take_picture requests. Only one take_picture request may be in flight at once.
        //
        router.get("/_camera/take_picture") { (request, match) -> BRHTTPResponse in
            if self.response != nil {
                print("[BRCameraPlugin] already taking a picture")
                return BRHTTPResponse(request: request, code: 423)
            }
            if !UIImagePickerController.isSourceTypeAvailable(.camera)
                || UIImagePickerController.availableCaptureModes(for: .rear) == nil {
                print("[BRCameraPlugin] no camera available")
                return BRHTTPResponse(request: request, code: 404)
            }
            let response = BRHTTPResponse(async: request)
            self.response = response
            
            DispatchQueue.main.async {
                let picker = UIImagePickerController()
                picker.delegate = self
                picker.sourceType = .camera
                picker.cameraCaptureMode = .photo
                
                // set overlay
                if let overlay = request.query["overlay"] , overlay.count == 1 {
                    print(["BRCameraPlugin] overlay = \(overlay)"])
                    let screenBounds = UIScreen.main.bounds
                    if overlay[0] == "id" {
                        picker.showsCameraControls = false
                        picker.allowsEditing = false
                        picker.hidesBarsOnTap = true
                        picker.isNavigationBarHidden = true
                        
                        let overlay = IDCameraOverlay(frame: screenBounds)
                        overlay.delegate = self
                        overlay.backgroundColor = UIColor.clear
                        picker.cameraOverlayView = overlay
                    }
                }
                self.picker = picker
                self.controller.present(picker, animated: true, completion: nil)
            }
            <commit_msg>return test response on simulator from GET /_camera/take_picture<commit_after>//
//  BRCameraPlugin.swift
//  BreadWallet
//
//  Created by <NAME> on 10/9/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

@available(iOS 8.0, *)
@objc open class BRCameraPlugin: NSObject, BRHTTPRouterPlugin, UIImagePickerControllerDelegate,
                                   UINavigationControllerDelegate, CameraOverlayDelegate {
    
    let controller: UIViewController
    var response: BRHTTPResponse?
    var picker: UIImagePickerController?
    
    init(fromViewController: UIViewController) {
        self.controller = fromViewController
        super.init()
    }
    
    open func hook(_ router: BRHTTPRouter) {
        // GET /_camera/take_picture
        //
        // Optionally pass ?overlay=<id> (see overlay ids below) to show an overlay
        // in picture taking mode
        //
        // Status codes:
        //   - 200: Successful image capture
        //   - 204: User canceled image picker
        //   - 404: Camera is not available on this device
        //   - 423: Multiple concurrent take_picture requests. Only one take_picture request may be in flight at once.
        //
        router.get("/_camera/take_picture") { (request, match) -> BRHTTPResponse in
            if self.response != nil {
                print("[BRCameraPlugin] already taking a picture")
                return BRHTTPResponse(request: request, code: 423)
            }
            if !UIImagePickerController.isSourceTypeAvailable(.camera)
                || UIImagePickerController.availableCaptureModes(for: .rear) == nil {
                print("[BRCameraPlugin] no camera available")
                guard let resp = try? BRHTTPResponse(request: request, code: 200, json: ["id": "test"]) else {
                    return BRHTTPResponse(request: request, code: 404)
                }
                return resp
            }
            let response = BRHTTPResponse(async: request)
            self.response = response
            
            DispatchQueue.main.async {
                let picker = UIImagePickerController()
                picker.delegate = self
                picker.sourceType = .camera
                picker.cameraCaptureMode = .photo
                
                // set overlay
                if let overlay = request.query["overlay"] , overlay.count == 1 {
                    print(["BRCameraPlugin] overlay = \(overlay)"])
                    let screenBounds = UIScreen.main.bounds
                    if overlay[0] == "id" {
                        picker.showsCameraControls = false
                        picker.allowsEditing = false
                        picker.hidesBarsOnTap = true
                        picker.isNavigationBarHidden = true
                        
                        let overlay = IDCameraOverlay(frame: screenBounds)
                        overlay.delegate = self
                        overlay.backgroundColor = UIColor.clear
                        picker.cameraOverlayView = overlay
                    }
                }
                self.picker = picker
                self.controller.present(picker, animated: true, completion: nil)
            }
            
{'fraction_non_alphanumeric': 0.0800886356441912, 'fraction_numerical': 0.0102880658436214, 'mean_word_length': 1.5510698425514735, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4100069', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1450, 'n_words': 397}