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

import UIKit

class PDCPCLTestFunctions: NSObject {

    public class func testPrinterFont() -> Data {
        
        let cmd = PTCommandCPCL.init()
        cmd.cpclLabel(withOffset: 0, hRes: PTCPCLLabelResolution.resolution200, vRes: PTCPCLLabelResolution.resolution200, height: 1100, quantity: 1)
        cmd.cpclPageWidth(832)
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font1, fontSize: 1, x: 20, y: 50, text: "保护环境，24x24world")
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font2, fontSize: 2, x: 20, y: 150, text: "保护环境，24x24world")
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font3, fontSize: 0, x: 20, y: 250, text: "保护环境，20x20world")
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font4, fontSize: 0, x: 20, y: 350, text: "保护环境，32x32world")
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font5, fontSize: 0, x: 20, y: 450, text: "保护环境，24x24world")
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font7, fontSize: 0, x: 20, y: 550, text: "保护环境，24x24world")
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font8, fontSize: 0, x: 20, y: 650, text: "保护环境，24x24world")
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font20, fontSize: 0, x: 20, y: 750, text: "保护环境，16x16world")
//        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font28, fontSize: 0, x: 20, y: 850, text: "保护环境，28x28world")
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font55, fontSize: 0, x: 20, y: 850, text: "保护环境，16x16world")
        if UserDefaults.standard.bool(forKey: PDPaperType) {
            cmd.cpclForm()
        }
        cmd.cpclPrint()
        return cmd.cmdData as Data
    }
    
    public class func printText() -> Data {
        
        let cmd = PTCommandCPCL.init()
        cmd.cpclLabel(withOffset: 0, hRes: PTCPCLLabelResolution.resolution200, vRes: PTCPCLLabelResolution.resolution200, height: 600, quantity: 1)
        cmd.cpclPageWidth(832)
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font8, fontSize: 0, x: 10, y: 10, text: "天本无情，所以不老，人为情苦，如何不老？情愁便似黄叶无风自落，飘扫之不尽，去之不绝，更堪秋风频催，断人弦肠。梦里哪知身是客，恣情贪欢，哪晓得，无限欢情，反作无穷苦因。不能追寻，偏要追寻，人情矛盾于此。往日欢乐，恰似一梦，而今才知，欢乐是苦。觉来却似未觉，午醉醒来，愁还未醒。未醒之际，辗转留连，如丝之未尽，如藕之未断，却是更深的梦了。天何不老？天本无梦。")
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font8, fontSize: 0, x: 10, y: 300, text: "Apart from tears, only time could wear everything away. While feeling is being processed by time, conflicts would be reconciled as time goes by, just like a cup of tea that is being continuously diluted.")
        if UserDefaults.standard.bool(forKey: PDPaperType) {
            cmd.cpclForm()
        }
        cmd.cpclPrint()
        return cmd.cmdData as Data
    }
    
    public class func testBarcode() {
        
        let cmd = PTCommandCPCL.init()
        cmd.cpclLabel(withOffset: 0, hRes: PTCPCLLabelResolution.resolution200, vRes: PTCPCLLabelResolution.resolution200, height: 1200, quantity: 1)
        cmd.cpclPageWidth(832)
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font8, fontSize: 0, x: 30, y: 10, text: "CODE128")
        cmd.cpclBarcodeText(withFont: PTCPCLTextFontName.font8, fontSize: 0, offset: 5)
        cmd.cpclBarcode(PTCPCLBarcodeStyle.code128, width: 2, ratio: PTCPCLBarcodeBarRatio.ratio3, height: 100, x: 30, y: 50, barcode: "123456789")
        cmd.cpclBarcodeTextOff()
        
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font8, fontSize: 0, x: 30, y: 200, text: "UPCA")
        cmd.cpclBarcodeText(withFont: PTCPCLTextFontName.font8, fontSize: 0, offset: 5)
        cmd.cpclBarcode(PTCPCLBarcodeStyle.UPCA, width: 2, ratio: PTCPCLBarcodeBarRatio.ratio3, height: 100, x: 30, y: 240, barcode: "123456789012")
        cmd.cpclBarcodeTextOff()
        
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font8, fontSize: 0, x: 30, y: 390, text: "UPCE")
        cmd.cpclBarcodeText(withFont: PTCPCLTextFontName.font8, fontSize: 0, offset: 5)
        cmd.cpclBarcode(PTCPCLBarcodeStyle.UPCE, width: 2, ratio: PTCPCLBarcodeBarRatio.ratio3, height: 100, x: 30, y: 430, barcode: "1234567")
        cmd.cpclBarcodeTextOff()
        
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font8, fontSize: 0, x: 30, y: 580, text: "EAN8")
        cmd.cpclBarcodeText(withFont: PTCPCLTextFontName.font8, fontSize: 0, offset: 5)
        cmd.cpclBarcode(PTCPCLBarcodeStyle.EAN8, width: 2, ratio: PTCPCLBarcodeBarRatio.ratio3, height: 100, x: 30, y: 620, barcode: "12345670")
        cmd.cpclBarcodeTextOff()
        
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font8, fontSize: 0, x: 30, y: 770, text: "CODE93")
        cmd.cpclBarcodeText(withFont: PTCPCLTextFontName.font8, fontSize: 0, offset: 5)
        cmd.cpclBarcode(PTCPCLBarcodeStyle.code93, width: 2, ratio: PTCPCLBarcodeBarRatio.ratio3, height: 100, x: 30, y: 810, barcode: "12345")
        cmd.cpclBarcodeTextOff()
        
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font8, fontSize: 0, x: 30, y: 950, text: "CODE39")
        cmd.cpclBarcodeText(withFont: PTCPCLTextFontName.font8, fontSize: 0, offset: 5)
        cmd.cpclBarcode(PTCPCLBarcodeStyle.code39, width: 2, ratio: PTCPCLBarcodeBarRatio.ratio3, height: 100, x: 30, y: 1000, barcode: "12345")
        cmd.cpclBarcodeTextOff()
        
        if UserDefaults.standard.bool(forKey: PDPaperType) {
            cmd.cpclForm()
        }
        cmd.cpclPrint()
        PTDispatcher.share()?.send(cmd.cmdData as Data)
    }
    
    public class func testQRcode() {
        
        let cmd = PTCommandCPCL.init()
        cmd.cpclLabel(withOffset: 0, hRes: PTCPCLLabelResolution.resolution200, vRes: PTCPCLLabelResolution.resolution200, height: 1400, quantity: 1)
        cmd.cpclPageWidth(832)
        cmd.cpclBarcodeQRcode(withXPos: 10, yPos: 10, model: PTCPCLQRCodeModel.model1, unitWidth: PTCPCLQRCodeUnitWidth._5)
        cmd.cpclBarcodeQRCodeCorrectionLecel(PTCPCLQRCodeCorrectionLevel.H, characterMode: PTCPCLQRCodeDataInputMode.M, context: "123456789")
        cmd.cpclBarcodeQRcodeEnd()
        
        cmd.cpclBarcodeQRcode(withXPos: 10, yPos: 200, model: PTCPCLQRCodeModel.model1, unitWidth: PTCPCLQRCodeUnitWidth._10)
        cmd.cpclBarcodeQRCodeCorrectionLecel(PTCPCLQRCodeCorrectionLevel.Q, characterMode: PTCPCLQRCodeDataInputMode.M, context: "123456789")
        cmd.cpclBarcodeQRcodeEnd()
        
        cmd.cpclBarcodeQRcode(withXPos: 10, yPos: 500, model: PTCPCLQRCodeModel.model1, unitWidth: PTCPCLQRCodeUnitWidth._15)
        cmd.cpclBarcodeQRCodeCorrectionLecel(PTCPCLQRCodeCorrectionLevel.M, characterMode: PTCPCLQRCodeDataInputMode.M, context: "123456789")
        cmd.cpclBarcodeQRcodeEnd()
        
        cmd.cpclBarcodeQRcode(withXPos: 10, yPos: 900, model: PTCPCLQRCodeModel.model1, unitWidth: PTCPCLQRCodeUnitWidth._17)
        cmd.cpclBarcodeQRCodeCorrectionLecel(PTCPCLQRCodeCorrectionLevel.L, characterMode: PTCPCLQRCodeDataInputMode.M, context: "123456789")
        cmd.cpclBarcodeQRcodeEnd()
        
        if UserDefaults.standard.bool(forKey: PDPaperType) {
            cmd.cpclForm()
        }
        cmd.cpclPrint()
        PTDispatcher.share()?.send(cmd.cmdData as Data)
    }
    
    public class func testPaperType() {
        
        let paperArray = ["Continuous paper".localized,"Label".localized,"2 inch label".localized,"3 inch label".localized,"4 inch label".localized]
        let pickerView = PDPickerView.init(with: paperArray)
        PDAppWindow.addSubview(pickerView)
        
        pickerView.selectItemClosure = { (item) in
            let cmd = PTCommandCPCL.init()
            if item == "Continuous paper".localized {
                UserDefaults.standard.set(false, forKey: PDPaperType)
                cmd.setPrinterPaperTypeFor4Inch(0)
            }else if item == "Label".localized {
                UserDefaults.standard.set(true, forKey: PDPaperType)
                cmd.setPrinterPaperTypeFor4Inch(2)
            }else if item == "2 inch label".localized {
                UserDefaults.standard.set(true, forKey: PDPaperType)
                cmd.setPrinterPaperTypeFor4Inch(4)
            }else if item == "3 inch label".localized {
                UserDefaults.standard.set(true, forKey: PDPaperType)
                cmd.setPrinterPaperTypeFor4Inch(5)
            }else if item == "4 inch label".localized {
                UserDefaults.standard.set(true, forKey: PDPaperType)
                cmd.setPrinterPaperTypeFor4Inch(6)
            }
            
            PTDispatcher.share()?.send(cmd.cmdData as Data)
            PTDispatcher.share()?.whenReceiveData({ (data) in
                
                guard let tempData = data else { return }
                if tempData.first == 1 {
                    SVProgressHUD.showSuccess(withStatus: "Setup succeeded".localized)
                }else {
                    SVProgressHUD.showError(withStatus: "Setup failed".localized)
                }
                print("\(tempData.hexString)")
            })
        }
        
    }
    
    public class func printBackGroundText() {
        
        let cmd = PTCommandCPCL.init()
        cmd.cpclLabel(withOffset: 0, hRes: PTCPCLLabelResolution.resolution200, vRes: PTCPCLLabelResolution.resolution200, height: 1000, quantity: 1)
        cmd.cpclPageWidth(832)
        cmd.cpclCenter()
        
        cmd.cpclSetMag(withWidth: PTCPCLFontScale._2, height: PTCPCLFontScale._2)
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font8, fontSize: 0, x: 0, y: 50, text: "blackness50")
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font8, fontSize: 0, x: 0, y: 400, text: "blackness150")
        cmd.cpclText(withRotate: PTCPCLStyleRotation.rotation0, font: PTCPCLTextFontName.font8, fontSize: 0, x: 0, y: 700, text: "blackness200")
        cmd.cpclSetMag(withWidth: PTCPCLFontScale._1, height: PTCPCLFontScale._1)
        
        cmd.cpclSetMag(withWidth: PTCPCLFontScale._10, height: PTCPCLFontScale._10)
        cmd.cpclSetBackgroundBlacknessValue(50)
        cmd.cpclBackText(withFont: PTCPCLTextFontName.font8, rotate: PTCPCLStyleRotation.rotation0, fontSize: 1, xPos: 0, yPos: 0, text: "508")
        cmd.cpclSetBackgroundBlacknessValue(150)
        cmd.cpclBackText(withFont: PTCPCLTextFontName.font8, rotate: PTCPCLStyleRotation.rotation0, fontSize: 1, xPos: 0, yPos: 300, text: "508")
        cmd.cpclSetBackgroundBlacknessValue(200)
        cmd.cpclBackText(withFont: PTCPCLTextFontName.font8, rotate: PTCPCLStyleRotation.rotation0, fontSize: 1, xPos: 0, yPos: 600, text: "508")
        cmd.cpclSetMag(withWidth: PTCPCLFontScale._1, height: PTCPCLFontScale._1)
        cmd.cpclSetBackgroundBlacknessValue(0)
        
        if UserDefaults.standard.bool(forKey: PDPaperType) {
            cmd.cpclForm()
        }
        cmd.cpclPrint()
        PTDispatcher.share()?.send(cmd.cmdData as Data)
    }
    
}


