//
//  PDESCTestFunctions.swift
//  PrinterDemo
//
//  Created by ios on 2019/10/28.
//  Copyright © 2019 XiamenHY. All rights reserved.
//

import UIKit
import PrinterSDK
import SVProgressHUD

class PDESCTestFunctions: NSObject {

    @objc class func printSelfTestPage() {
        
        let esc = PTCommandESC.init()
        esc.printSelfTest()
        PrinterManager.instance.executeCmd(esc)
    }
    
    @objc class func printImage() {
        let image = UIImage(contentsOfFile: Bundle.main.path(forResource: "print_2", ofType: "JPG")!)
        
        guard let temp = image else { return }
        guard var zybImage = PDResetImage.scaleImageForWidth(image: temp, width: CGFloat(kUserDefaults.integer(forKey: PDPrintDots))) else { return }
        zybImage = PTBitmapManager.image(toSharpen: zybImage)
        guard let cgImage = zybImage.cgImage else { return }
        
        let esc = PTCommandESC.init()
        esc.escPrintCounter()
        esc.kickCashdrawer(2)
        esc.escBeepIntegratedBeeper(withTimes: 10, t1: 0, t2: 0)
        esc.appendRasterImage(cgImage, mode: PTBitmapMode.binary, compress: PTBitmapCompressMode.none, package: false)
        esc.setFullCutWithDistance(100)
        esc.getPrinterStatus()
        PrinterManager.instance.executeCmd(esc)
    }
    
    @objc class func getPrinterStatus() {
        
        let esc = PTCommandESC.init()
        esc.getPrinterStatus()
        PrinterManager.instance.cmdRequest(esc) { result in
            switch (result) {
            case .success(let data):
                let byte = data
                var status = ""

                if byte[0] & 4 == 4 {
                    status += "Cover opened".localized
                }else {
                    status += "Cover closed".localized
                }

                if byte[1] & 96 == 96 {
                    status += "Out of paper".localized
                }else {
                    status += "Paper remaining".localized
                }
                SVProgressHUD.showSuccess(withStatus: status)
            case .failure(let error):
                SVProgressHUD.showError(withStatus: S1ErrorDescription(error: error))
            }
        }
    }
    
    @objc class func printBarcode() {
                
        let esc = PTCommandESC.init()
        esc.initializePrinter()
        
//        esc.appendText("UPCA:", mode: ESCText.normal)
//        esc.append(ESCBarcode.B_UPCA, data: "075678164125", justification: 0, width: 2, height: 60, hri: 2)
//        esc.appendText("\n", mode: ESCText.normal)
//
//        esc.appendText("UPCE:", mode: ESCText.normal)
//        esc.append(ESCBarcode.B_UPCE, data: "01230000045", justification: 0, width: 2, height: 60, hri: 2)
//        esc.appendText("\n", mode: ESCText.normal)
//
//        esc.appendText("EAN8:", mode: ESCText.normal)
//        esc.append(ESCBarcode.B_EAN8, data: "04210009", justification: 0, width: 2, height: 60, hri: 2)
//        esc.appendText("\n", mode: ESCText.normal)
//
//        esc.appendText("EAN13:", mode: ESCText.normal)
//        esc.append(ESCBarcode.B_EAN13, data: "6901028075831", justification: 0, width: 2, height: 60, hri: 2)
//        esc.appendText("\n", mode: ESCText.normal)
//
//        esc.appendText("CODE39:", mode: ESCText.normal)
//        esc.append(ESCBarcode.B_CODE39, data: "123456789", justification: 0, width: 2, height: 60, hri: 2)
//        esc.appendText("\n", mode: ESCText.normal)
//
//        esc.appendText("TIF:", mode: ESCText.normal)
//        esc.append(ESCBarcode.B_ITF, data: "9876543210", justification: 0, width: 2, height: 60, hri: 2)
//        esc.appendText("\n", mode: ESCText.normal)
//
//        esc.appendText("CODEBAR:", mode: ESCText.normal)
//        esc.append(ESCBarcode.B_CODEBAR, data: "A40156B", justification: 0, width: 2, height: 60, hri: 2)
//        esc.appendText("\n", mode: ESCText.normal)
//
//        esc.appendText("CODE93:", mode: ESCText.normal)
//        esc.append(ESCBarcode.B_CODE93, data: "TEST93", justification: 0, width: 2, height: 60, hri: 2)
//        esc.appendText("\n", mode: ESCText.normal)
        
        /// P831220913000001  A123456789012
//        esc.appendText("CODE128:", mode: ESCText.normal)
        esc.append(ESCBarcode.B_CODE128, data: "{CP831220913000001", justification: 0, width: 2, height: 60, hri: 2)
//        esc.appendText("\n", mode: ESCText.normal)
//        esc.appendText("QR code:", mode: ESCText.normal)
//        esc.appendQRCodeData("hello world", justification: 0, leftMargin: 10, eccLevel: 48, model: 49, size: 5)
        PrinterManager.instance.executeCmd(esc, showActivity: true)
    }
    
    @objc class func testPageModeSample() {
        
        let esc = PTCommandESC.init()
        
        esc.initializePrinter()
        
        esc.setPageMode()
        esc.pageSetPrintArea(withXPos: 0, yPos: 0, width: kUserDefaults.integer(forKey: PDPrintDots), height: 200)
        
        esc.pageSetPrintDirection(0)
        esc.setAbsolutePosition(10)
        esc.pageSetAbsoluteYPos(10)
        esc.appendQRCodeData("hello world", justification: 0, leftMargin: 30, eccLevel: 48, model: 49, size: 5)
        
        esc.pageSetPrintDirection(0)
        esc.setAbsolutePosition(100)
        esc.pageSetAbsoluteYPos(10)
        esc.appendQRCodeData("hello world", justification: 0, leftMargin: 200, eccLevel: 48, model: 49, size: 5)
        
        esc.printDataInPageMode()
        
        esc.positionNextLabel()        
        PrinterManager.instance.executeCmd(esc, showActivity: true)
    }
    
    @objc class func testBillSample() {
        
        let esc = PTCommandESC.init()
        
        esc.initializePrinter()
        esc.appendText("TEST TITLE\n\n", mode: ESCText.doubleWidth)
        
        let companyMessage = "2F,8#,Gaoqi Nan Shi'er Road\n(Aide Industiral Park)\nXiamen,China 361006\n\nTel:+(86)-(0)592-5885993,5756958\nWebsite:http://www.prttech.com\n\n"
        esc.appendText(companyMessage, mode: ESCText.normal)
        
        var receiptText = ""
        receiptText.append("------------------------------\n")
        receiptText.append("           NO.1008            \n")
        receiptText.append("\(Date.init())\n")
        receiptText.append("------------------------------\n")
        receiptText.append("MPT-II          1      $700.00\n")
        receiptText.append("MPT-III         1      $700.00\n")
        receiptText.append("LPQ58           1      $800.00\n")
        receiptText.append("BLE1            1      $800.00\n")
        receiptText.append("BLE123          1      $900.00\n")
        receiptText.append("POS80A          1      $900.00\n")
        receiptText.append("POS80B          1      $950.00\n")
        receiptText.append("POS80C          1      $950.00\n")
        receiptText.append("SMP-R381        1      $850.00\n")
        receiptText.append("SMP-R385        1      $850.00\n")
        receiptText.append("SMP-R386        1      $850.00\n")
        receiptText.append("MPT-II          1      $700.00\n")
        receiptText.append("MPT-III         1      $700.00\n")
        receiptText.append("LPQ58           1      $800.00\n")
        receiptText.append("BLE-14          1      $800.00\n")
        receiptText.append("BLE-23          1      $900.00\n")
        receiptText.append("E300            1      $900.00\n")
        receiptText.append("E200            1      $900.00\n")
        receiptText.append("T300            1      $900.00\n")
        receiptText.append("POS80C          1      $900.00\n")
        receiptText.append("POS80E          1      $900.00\n")
        receiptText.append("POS808          1      $900.00\n")
        receiptText.append("POS801          1      $900.00\n")
        receiptText.append("------------------------------\n\n")
        receiptText.append("Total                $18500.00\n")
        receiptText.append("-------------------------------\n\n")
        esc.appendText(receiptText, mode: ESCText.normal)
        
        esc.append(ESCBarcode.B_UPCA, data: "075678164125", justification: 0, width: 3, height: 30, hri: 2)
        esc.appendQRCodeData("https://itunes.apple.com/lookup?id=1472096775", justification: 0, leftMargin: 0, eccLevel: 48, model: 49, size: 10)
        
        var copyright = ""
        copyright.append("\(Date.init())\n")
        copyright.append("POS Editor:V1.2.0\n")
        copyright.append("Copyright 2018 POS Editor.\n")
        copyright.append("All rights reserved\n\n\n\n")
        esc.appendText(copyright, mode: ESCText.normal)
        
        esc.setFullCutWithDistance(100)
        PrinterManager.instance.executeCmd(esc, showActivity: true)
    }
    
    @objc class func testPOSPrinterSample() {
        
        let esc = PTCommandESC.init()
        
        esc.initializePrinter()
        esc.appendText("Test POS Printer First\n\n", mode: ESCText.doubleWidth)
        
        var receiptText = ""
        receiptText.append("TP801----------------$900.00\n")
        receiptText.append("TP802----------------$900.00\n")
        receiptText.append("TP808----------------$900.00\n")
        receiptText.append("TP809----------------$900.00\n")
        receiptText.append("TP805----------------$900.00\n")
        receiptText.append("TP806----------------$900.00\n")
        esc.appendText(receiptText, mode: ESCText.normal)
        
        esc.append(ESCBarcode.B_UPCA, data: "075678164125", justification: 0, width: 3, height: 30, hri: 2)
        //cut
        esc.setFullCutWithDistance(100)
        
        esc.appendText("\n\n")
        esc.appendText("Test POS Printer Second\n\n", mode: ESCText.doubleWidth)
        var copyright = ""
        copyright.append("Apple------------------$900.00\n")
        copyright.append("Apricot----------------$900.00\n")
        copyright.append("Avocado----------------$900.00\n")
        copyright.append("Banana-----------------$900.00\n")
        copyright.append("Cherry-----------------$900.00\n")
        copyright.append("Coconut----------------$900.00\n")
        esc.appendText(copyright, mode: ESCText.normal)
        //cut
        esc.setFullCutWithDistance(100)
        
        //open cashdrawer
        esc.kickCashdrawer(0)
        
        PrinterManager.instance.executeCmd(esc, showActivity: true)
    }
    
    @objc class func testPOSImageSample() {
        
        guard let imgaePath = Bundle.main.path(forResource: "PDPrinterResource.bundle/cainiao", ofType: "jpg") else {return}
        guard let cgTemp = UIImage.init(contentsOfFile: imgaePath)?.cgImage else {return}
        
        let cmd = PTCommandESC.init()
        cmd.appendRasterImage(cgTemp, mode: .binary, compress: .LZO, package: true)
        
        //如果打印机开启了黑标侦测和黑标自动定位后切刀功能，那么加上这条自动定位再切刀就有效果
        //自动定位
        cmd.setPrinterAutomaticPosition()
        //全切
        cmd.setFullCut()
//        cmd.setPartialCut()
        PrinterManager.instance.executeCmd(cmd, showActivity: true)
    }
    
    @objc class func setSesFontForChinese() {
        let fontSizes = ["33","34","35"]
        let pick = PDPickerView.init(with: fontSizes)
        PDAppWindow.addSubview(pick)
        pick.selectItemClosure = {(code) in
            guard let size = Int(code) else { return }
            let cmd = PTCommandESC.init()
            cmd.setChineseSesBlackFontWithSize(size)
            PrinterManager.instance.executeCmd(cmd)
        }
    }
    
    @objc class func setSesFontForEnglish() {
        let fontSizes = ["33","34","35"]
        let pick = PDPickerView.init(with: fontSizes)
        PDAppWindow.addSubview(pick)
        pick.selectItemClosure = {(code) in
            guard let size = Int(code) else { return }
            let cmd = PTCommandESC.init()
            cmd.setEnglishSesBlackFontWithSize(size)
            PrinterManager.instance.executeCmd(cmd)
        }
    }
    
    @objc class func testDrawer() {
        let esc = PTCommandESC()
        esc.kickCashdrawer(2)
        PrinterManager.instance.executeCmd(esc)
    }
    
    @objc class func batchSendPrintSelfTest() {
        let cmd = PTCommandESC.init()
        cmd.printSelfTest()
        PrinterManager.instance.writeData(data: cmd.generate()) {
            SVProgressHUD.showInfo(withStatus: "Print Success".localized)
        }
    }
    
    class func applyFontSize(_ fontSize: Int, sizeText: String) {
        guard let printer = PrinterManager.instance.accessory, printer.connected else {
            SVProgressHUD.showError(withStatus: "打印机未连接".localized)
            return
        }
        let esc = PTCommandESC.init()
        esc.addFontMultiplier(fontSize, heightMultiplier: fontSize)
        esc.appendText("Font Size Test: ")
        esc.appendText("0123456789")
        esc.appendText("ABCDEFGHIJK")
        esc.appendText("Hello World!")
        esc.addFontMultiplier(0, heightMultiplier: 0)
        esc.appendText("--------------------------------")
        PrinterManager.instance.executeCmd(esc)
        
        SVProgressHUD.showSuccess(withStatus: "字体大小设置完成".localized + "：" + sizeText)
    }
}
