 
 private let accountOrderKey = "persistentRefs"
 
-class AccountsViewController: UITableViewController {
+class AccountsViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
     @IBOutlet weak var emptyView: UIView!
 
     private let keychain = Keychain()
     private var accounts = [Account]()
+    private let qrImporter = UIImagePickerController()
+
     private lazy var searchController = makeSearchController()
     private lazy var addAccountAlertController = makeAddAccountAlertController()
 


    private func makeAddAccountAlertController() -> UIAlertController {
        let title = "Add Account"
        let message = "Add an account by scanning a QR code or enter a secret manually."
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)

        let scanQRCode = UIAlertAction(title: "Scan QR Code", style: .default) { [unowned self] _ in
            self.performSegue(withIdentifier: "ScanSegue", sender: self)
        }

        let enterManually = UIAlertAction(title: "Enter Manually", style: .default) { [unowned self] _ in
            self.performSegue(withIdentifier: "EnterManuallySegue", sender: self)

            tableView.separatorStyle = .singleLine
            navigationItem.leftBarButtonItem = editButtonItem
        }
    }
    
    // MARK: UIImagePickerControlDelegate
    
    func imagePickerController(_ picker: UIImagePickerController,
                               didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let selectedQRCode = info[UIImagePickerController.InfoKey.originalImage] as? UIImage,
            let detector = CIDetector(ofType: CIDetectorTypeQRCode,
                                      context: nil,
                                      options: [CIDetectorAccuracy: CIDetectorAccuracyHigh]),
            let ciImage = CIImage(image: selectedQRCode),
            let features = detector.features(in: ciImage) as? [CIQRCodeFeature] else { return }

        let qrCodeURL = URL(string: features.reduce("") { $0 + ($1.messageString ?? "") })

        if qrCodeURL != nil, let account = Account(url: qrCodeURL!) {
            self.createAccount(account)
        }
        
        dismiss(animated: true, completion: nil)
    }

    // MARK: UITableViewDataSource
