package com.ZMPrinter.Demo;

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

public class USB_Read_NFC_UID_to_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.RFID_USB;//interface type
        thiszmprinter.printerdpi = 203;//Printer resolution, 203dpi, 300dpi or 600dpi
        thiszmprinter.printSpeed = 4;//print speed
        thiszmprinter.printDarkness = 10;//print darkness
        //endregion

        //region Define label object (size)
        zmlabel thiszmlabel = new zmlabel();//Define the label object
        thiszmlabel.labelwidth = 50;//The width of the label, the unit is mm
        thiszmlabel.labelheight = 50;//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 Read the UID of the tag
        String tagData = usbConnection.readHFTag(3, 1, 1);//Read the UID of the high-frequency NFC tag, try again once an error occurs, and stop at the print position after reading.
        if (tagData.isEmpty() || tagData.equals("X"))//Read error, feed the current label, continue to the next label.
        {
            System.out.println("Failed to read the high frequency tag UID.");
            return;
        }
        //endregion

        //region Print the tag
        try
        {
            String printcommandstring = "I8,1,001\r\nS40\r\nH10\r\nq440\r\nQ440,48\r\nN\r\nHS2,3,1,0\r\nT104,16,0,3,1,1,N,\"CODI\"\r\nT178,16,0,3,1,1,N,\"000000\"\r\nT104,56,0,3,1,1,N,\"SAP\"\r\nT178,56,0,3,1,1,N,\"99999999\"\r\nT104,104,0,4,1,1,N,\"00AA00\"\r\nT104,176,0,2,1,1,N,\"Descripcionlinea1\"\r\nT104,200,0,2,1,1,N,\"Descripcionlinea2\"\r\nT104,224,0,2,1,1,N,\"Descripcionlinea3\"\r\nT104,308,0,4,1,1,N,\"Z000\"\r\nb208,264,DX,0,0,o0,m5,\"" + tagData + "\"\r\nT104,368,0,2,1,1,N,\"" + tagData + "\"\r\nT104,392,0,2,1,1,N,\"09/03/2022 12:16\"\r\n";
            String osname = System.getProperty("os.name").toLowerCase();
            if (!osname.contains("windows"))
                printcommandstring+="PR1,1\r\n";

            byte[] zmpclecommand = printcommandstring.getBytes("utf-8");
            usbConnection.write(zmpclecommand);//Send print data to the printer
        }
        catch (Exception e)
        {
            System.out.println("Print failed. Details:"+e.getMessage());
            return;
        }
        //endregion

        //region Wait print process finish
        //read back the status through the printer's USB port, confirm that the printer receives the data and wait for the printing to complete
        String returnmsg = usbConnection.read(512);//Read printer's data, When the print command is changed from W to PR, the printer will return data: "RP" after printing.
        if (!returnmsg.isEmpty())
            System.out.println("Data returned by the printer:" + returnmsg);
        new printutility().WaitPrintProcess(thiszmprinter, thiszmlabel);//Wait for printing to complete
        //endregion

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