//
//  PDCPCLViewController.swift
//  PrinterDemo
//
//  Created by ios on 2019/9/6.
//  Copyright © 2019 XiamenHY. All rights reserved.
//

import UIKit

class PDCPCLViewController: PDBaseViewController {

    lazy var tableView : UITableView = {
        let temp = UITableView.init(frame: .zero, style: .plain)
        temp.delegate = self
        temp.dataSource = self
        temp.backgroundColor = .white
        temp.showsVerticalScrollIndicator = false
        temp.separatorStyle = UITableViewCell.SeparatorStyle.singleLine
        return temp
    }()
    
    private var functions = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "CPCL function".localized
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints {
            $0.edges.equalTo(self.view.usnp.edges)
        }
        
        functions = ["Device Status Sample".localized,"Font Sample".localized,"Text Sample".localized,"Barcode Sample".localized,"QRcode Sample".localized,"Bitmap Sample".localized,"Set Paper Type Sample".localized,"Background Text Sample".localized]
    }

}

extension PDCPCLViewController : UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return functions.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "iden")
        if cell == nil {
            cell = UITableViewCell.init(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "iden")
        }
        cell?.textLabel?.text = functions[indexPath.row]
        cell?.textLabel?.textColor = UIColor.black
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 50.0
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        PTDispatcher.share()?.receiveDataBlock = nil
        PTDispatcher.share()?.sendSuccessBlock = nil
        
        switch indexPath.row {
        case 0:
            let cmd = PTCommandCPCL.init()
            cmd.cpclGetPaperStatus()
            PTDispatcher.share()?.send(cmd.cmdData as Data)
            PTDispatcher.share()?.whenReceiveData({ (data) in
                guard let data = data else { return }
                print(data.hexString)
                guard let statusData = data.read(to: UInt8.self) else { return }
                let message = PDPrinterStatusOptionSet.init(rawValue: statusData).description
                SVProgressHUD.showSuccess(withStatus: message)
            })
        case 1:
            PTDispatcher.share()?.send(PDCPCLTestFunctions.testPrinterFont())
        case 2:
            PTDispatcher.share()?.send(PDCPCLTestFunctions.printText())
        case 3:
            PDCPCLTestFunctions.testBarcode()
        case 4:
            PDCPCLTestFunctions.testQRcode()
        case 5:
            navigationController?.pushViewController(PDPhotoListViewController(), animated: true)
        case 6:
            PDCPCLTestFunctions.testPaperType()
        case 7:
            PDCPCLTestFunctions.printBackGroundText()
        default:
            break
        }
        
    }
}

//Printer Status
struct PDPrinterStatusOptionSet : OptionSet,CustomStringConvertible {
    
    var rawValue: UInt8
    typealias RawValue = UInt8
    
    static let busy = PDPrinterStatusOptionSet.init(rawValue: 1<<0)
    static let paperEnd = PDPrinterStatusOptionSet.init(rawValue: 1<<1)
    static let openCover = PDPrinterStatusOptionSet.init(rawValue: 1<<2)
    static let lowVoltage = PDPrinterStatusOptionSet.init(rawValue: 1<<3)
    
    var description: String {
        
        var messages = [String]()
        if contains(.busy) {
            messages.append("Busy".localized)
        }
        
        if contains(.paperEnd) {
            messages.append("Out of paper".localized)
        }
        
        if contains(.openCover) {
            messages.append("Open".localized)
        }
        
        if contains(.lowVoltage) {
            messages.append("Battery is Low".localized)
        }
        
        if messages.count == 0 {
            messages.append("Ready".localized)
        }

        return  messages.joined(separator: "--")
    }
}
