//
//  TestViewControllerSwift.swift
//  MCPlus
//
//  Created by seo ha on 15/02/2019.
//  Copyright © 2019 KangSH. All rights reserved.
//

import Foundation
import UIKit

class BarcodeViewController: UIViewController {
    
    var reader:ZBarReaderViewController!
    
    var callBack:((String)->Void)?
    
    @IBOutlet weak var cameraOverlayView: UIView!
    
    @IBOutlet weak var closeButton: UIImageView!{
        didSet{
            closeButton.addTapGestureRecognizer(action: self.tapAction)
        }
    }
    
    
}

extension BarcodeViewController{
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.scanAction()
        // Do any additional setup after loading the view.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func scanAction(){
        // ADD: present a barcode reader that scans from the camera feed
        reader = ZBarReaderViewController()
        reader.readerDelegate = self
        reader.supportedOrientationsMask = (1 << UIInterfaceOrientation.portraitUpsideDown.rawValue)
        reader.showsZBarControls = false
        
//        let cameraOverlayView = UIView(frame: UIScreen.main.bounds)
//        cameraOverlayView.backgroundColor = UIColor.clear
//
//        let instructionLabel = UILabel(frame: UIScreen.main.bounds)
//        instructionLabel.textColor = UIColor.gray
//        instructionLabel.backgroundColor = UIColor.clear
//        instructionLabel.font = UIFont(name: "Trebuchet MS", size: 24.0)
//        instructionLabel.text = "취소"
//
//        cameraOverlayView.addSubview(instructionLabel)
//
//        instructionLabel.addTapGestureRecognizer(action: self.tapAction)
        
        let scanner = reader.scanner
        
        // TODO: (optional) additional reader configuration here
        
        // EXAMPLE: disable rarely used I2/5 to improve performance
        scanner?.setSymbology(ZBAR_I25, config: ZBAR_CFG_ENABLE, to: 0)
        
        // present and release the controller
        
//        self.present(reader, animated: true, completion: nil)
        reader.view.frame = self.cameraOverlayView.bounds
        self.cameraOverlayView.addSubview(reader.view)
        self.addChild(self.reader)
    }
}

extension BarcodeViewController: ZBarReaderDelegate{
    func tapAction(){
        guard let reader = self.reader else {
            return
        }
        
        self.navigationController?.popViewController(animated: false)
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak reader] in
            reader?.dismiss(animated: true, completion: nil)
        }
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        // ADD: get the decode results
        
        let key = UIImagePickerController.InfoKey.init(rawValue: ZBarReaderControllerResults)
        guard let results = info[key] as? NSFastEnumeration else{ return }
        
        var iterator = NSFastEnumerationIterator(results)
        
        var symbol:ZBarSymbol? = nil
        while let _symbol = iterator.next(){
            symbol = _symbol as? ZBarSymbol
        }
        
        if let data = symbol?.data{
            callBack?(data)
        }
        
        
        self.navigationController?.popViewController(animated: false)
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak reader] in
            reader?.dismiss(animated: true, completion: nil)
        }
    }
}

