package com.ZMPrinter.Demo;

import com.ZMPrinter.printerstyle;
import com.ZMPrinter.printutility;
import com.ZMPrinter.zmlabel;
import com.ZMPrinter.zmprinter;

public class NET_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_NET;//interface type
        thiszmprinter.printerdpi = 203;//Printer resolution, 203dpi, 300dpi or 600dpi
        thiszmprinter.printSpeed = 4;//print speed
        thiszmprinter.printDarkness = 10;//print darkness
        thiszmprinter.printernetip = "192.168.8.219";//The ip address of the printer, the port number is fixed at 9100.
        //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 Defines the print operation object.
        printutility zmprintoperate = new printutility();
        //endregion

        //region Check printer status.
        if (!zmprintoperate.CheckNetPrinterStatus(thiszmprinter)) {
            System.out.println("The printer status is abnormal, please check it.");
            return;
        }
        //endregion

        //region Read the UID of the tag by net
        String tagData = zmprintoperate.ReadFromPrinterHFbyNet(thiszmprinter, 0, true);//Read the UID of the high-frequency NFC tag, stop at the print position after reading.
        if (tagData.isEmpty() || tagData.equals("X0000000"))//Read error
        {
            System.out.println("Failed to read the high frequency tag UID.");
            zmprintoperate.PrintBlankLabelbyNet(thiszmprinter, thiszmlabel);//Feed out a blank label
            try {
                Thread.sleep(3000);
            } catch (Exception ex) {
            }//Wait 3 seconds for the printer to complete all operations.
            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";
            printcommandstring += "PN1,1\r\n";//It is recommended to use the "PN" command instead of the "W" command when the RFID printer prints through the network.

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

        //region Wait print process finish
        zmprintoperate.WaitPrintProcess(thiszmprinter, thiszmlabel);//Wait for printing to complete
        //endregion
    }

}
