//
//  ScannerViewController.swift
//  Runner
//
//  Created by Ivan Murashov on 13/07/2017.
//  Copyright © 2017 The Chromium Authors. All rights reserved.
//

import UIKit

extension ZBarSymbolSet: Sequence {
    public typealias Element  = ZBarSymbol
    public typealias Iterator = NSFastEnumerationIterator

    public func makeIterator() -> NSFastEnumerationIterator {
        return NSFastEnumerationIterator(self)
    }
}

@objc class ScannerViewController: UIViewController, ZBarReaderDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let readerViewController = ZBarReaderViewController()
        readerViewController.readerDelegate = self
        readerViewController.readerView.zoom = 1.0
        readerViewController.showsZBarControls = false
        self.present(readerViewController, animated: true)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        guard let symbols = info[ZBarReaderControllerResults] as? ZBarSymbolSet else { return }

        for symbol in symbols {

            if let symbol = symbol as? ZBarSymbol, let data = symbol.data {

                let toast = UIAlertView()
                toast.message = data
                toast.show()

                navigationController?.popViewController(animated: true)
            }
        }
        navigationController?.popViewController(animated: true)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}
