package com.ZMPrinter.Demo;

import com.ZMPrinter.*;
import com.ZMPrinter.UsbUtility.UsbConnection;

import java.util.ArrayList;
import java.util.List;

public class USB_Write_NFC_and_Print {
    public static void main(String[] args) {
        //region Define the printer object (interface type, resolution, printing blackness and speed)
        zmprinter thiszmprinter = new zmprinter();//Define the printer object
        thiszmprinter.printerinterface = printerstyle.USB;//interface type
        thiszmprinter.printerdpi = 300;//Printer resolution, 203dpi, 300dpi or 600dpi
        thiszmprinter.printSpeed = 4;//print speed
        thiszmprinter.printDarkness = 12;//print darkness
        //endregion

        //region Define label object (size)
        zmlabel thiszmlabel = new zmlabel();//Define the label object
        thiszmlabel.labelwidth = 30;//The width of the label, the unit is mm
        thiszmlabel.labelheight = 15;//The height of the label, the unit is mm
        thiszmlabel.labelrowgap = 2;//row gap between labels, the unit is mm
        //endregion

        //region Create a USB printer connection
        UsbConnection usbConnection = new UsbConnection();
        if (!usbConnection.Open()) {
            System.out.println("Failed to connect to the printer, please check.");
            return;
        }
        //endregion

        //region Define each object that needs to be printed in the label

        String writedata="http://www.zmin.com.cn";

        //一个标签上可以有多个对象，如文字、一维条码、二维条码、线条、矩形、图片、RFID写入数据等
        //a label can have multiple objects, such as text, one-dimensional bar code, two-dimensional bar code, lines, rectangles, pictures, RFID write data, etc.
        List<zmlabelobject> thiszmlabelobjectlist = new ArrayList<>();//定义标签上需要打印的对象列表//Define the list of objects that need to be printed on the label

        zmlabelobject thiszmlabelobject01 = new zmlabelobject();
        thiszmlabelobject01.objectName = "truetype";//该对象是TrueType文字//The object is TrueType text
        thiszmlabelobject01.objectdata = "NFC write and print";//文字内容//Text content
        thiszmlabelobject01.Xposition = 3;//文字X坐标，单位mm//Text X coordinate, unit mm
        thiszmlabelobject01.Yposition = 3;//文字Y坐标，单位mm//Text Y coordinate, unit mm
        thiszmlabelobject01.textfont = "Arial";//文字的字体//Text font name
        thiszmlabelobject01.fontstyle = 1;//0正常体，1粗体//0 normal, 1 bold
        thiszmlabelobject01.fontsize = 7;//文字的字号//The font size of the text
        thiszmlabelobjectlist.add(thiszmlabelobject01);//将文字对象添加进对象列表//Add the text object to the object list

        zmlabelobject thiszmlabelobject02 = new zmlabelobject();
        thiszmlabelobject02.objectName = "truetype";//该对象是TrueType文字//The object is TrueType text
        thiszmlabelobject02.objectdata = "Write NDEF URL:"+writedata;//文字内容//Text content
        thiszmlabelobject02.Xposition = 3;//文字X坐标，单位mm//Text X coordinate, unit mm
        thiszmlabelobject02.Yposition = 10;//文字Y坐标，单位mm//Text Y coordinate, unit mm
        thiszmlabelobject02.textfont = "Arial";//文字的字体//Text font name
        thiszmlabelobject02.fontstyle = 1;//0正常体，1粗体//0 normal, 1 bold
        thiszmlabelobject02.fontsize = 7;//文字的字号//The font size of the text
        thiszmlabelobjectlist.add(thiszmlabelobject02);//将文字对象添加进对象列表//Add the text object to the object list

        zmlabelobject nfclobject01 = new zmlabelobject();
        nfclobject01.objectName = "rfiduhf";//该对象是RFID写入//The object is RFID write
        nfclobject01.RFIDEncodertype = 2;//0:UHF, 1:HF 15693, 2:HF 14443A, 3:HF NFC/UltraLight
        nfclobject01.RFIDerrortimes = 1;//错误重试次数//Number of error retries
        nfclobject01.RFIDDatatype = 2;//数据类型：0:文本，1:16进制，2:网址链接（NDEF），3:纯文本（NDEF）//Data type: 0: text, 1: hexadecimal, 2: URL link (NDEF), 3: plain text (NDEF)
        nfclobject01.objectdata = writedata;//需要写入的内容//What needs to be written
        nfclobject01.HFstartblock = 1;//数据写入的起始块，一般情况下高频15693起始块为0，14443A起始块为1，NFC起始块为4//The starting block of data writing, under normal circumstances the high frequency 15693 starting block is 0, the 14443A starting block is 1, and the NFC starting block is 4
        thiszmlabelobjectlist.add(nfclobject01);//将RFID对象添加进对象列表//Add the RFID object to the object list
        //endregion

        //region Generate print data according to the list of objects to be printed on the label
        byte[] zmpclecommand = new printutility().CreateLabelCommand(thiszmprinter, thiszmlabel, thiszmlabelobjectlist);//生成标签数据//Generate label data
        if (zmpclecommand == null) {
            System.out.println("生成标签数据异常为空.The generated tag data is abnormally empty.");
            return;
        }
        //endregion

        //region Send print data to the printer
        usbConnection.write(zmpclecommand);//发送数据给打印机//Send print data to the printer
        //endregion

        //region Close the USB printer connection
        usbConnection.Close();
        //endregion
    }
}
