/**
 * Example: Print Label from LSF File
 *
 * This demonstrates loading a label from an .lsf file, reading RFID tag data,
 * updating object variables, and printing in a loop.
 *
 * Build (MSVC): cl /utf-8 example_lsf.c /Fe:example_lsf.exe /I. x64\LP_LabelPrinter.lib
 */

#include <stdio.h>
#include "lp_api.h"

int main() {
    printf("=== LSF Label Example ===\n\n");

    // -----------------------------------------------------------------------
    // 1. Load label file
    // -----------------------------------------------------------------------
    LP_LABELDOC doc = LpLabelDoc_Load("D:/Test/70x50_asset_label.lsf");
    if (!doc) {
        printf("Failed to load LSF file. Error: %s\n", LpLabelDoc_GetLastError());
        return -1;
    }
    printf("LSF file loaded.\n");
    printf("  Objects: %d\n", LpLabelDoc_GetObjectCount(doc));
    printf("  Stored DPI: %d\n", LpLabelDoc_GetPrinterDpi(doc));

    // -----------------------------------------------------------------------
    // 2. Create and configure printer
    // -----------------------------------------------------------------------
    LP_PRINTER printer = LpPrinter_Create();
    LpPrinter_SetInterface(printer, LP_RFID_USB);
    LpPrinter_SetDpi(printer, 300);
    LpPrinter_SetSpeed(printer, 4);
    LpPrinter_SetDarkness(printer, 12);

    if (LpPrinter_Connect(printer) != 0) {
        printf("Failed to connect. Error: %s\n", LpGetLastErrorMessage());
        LpLabelDoc_Destroy(doc);
        LpPrinter_Destroy(printer);
        return -2;
    }
    printf("Printer connected.\n\n");

    // -----------------------------------------------------------------------
    // 3. Print loop
    // -----------------------------------------------------------------------
    int totalCopies = 10;
    for (int i = 0; i < totalCopies; i++) {

        // Check printer status
        int status = LpPrinter_GetStatusCode(printer);
        if (status != 0) {
            char info[256];
            LpPrinter_GetStatusInfo(status, info, sizeof(info));
            printf("Printer error (code %d): %s\n", status, info);
            break;
        }

        // --- Optional: read RFID tag and use it to update label data ---
        // char rfidBuf[128];
        // int rfidLen = LpPrinter_ReadUHFTag(printer, 0, 3, 5000, rfidBuf, sizeof(rfidBuf));
        // if (rfidLen > 0) {
        //     printf("Read tag: %s\n", rfidBuf);
        //     LpObject_SetVariableAll(doc, "uid", rfidBuf);
        // }

        // --- Update variable with incrementing value ---
        char epc[32];
        snprintf(epc, sizeof(epc), "99998888AAAA%04d", i + 1);
        LpObject_SetVariableAll(doc, "epc", epc);

        // --- Optional: save preview ---
        // LpLabelDoc_SavePreview(doc, printer, "d:/preview.bmp");

        // --- Print ---
        int result = LpLabelDoc_Print(doc, printer);
        if (result != 0) {
            printf("Print failed at %d/%d (result=%d)\n", i + 1, totalCopies, result);
            break;
        }
        printf("Printed %d/%d\n", i + 1, totalCopies);
    }

    // -----------------------------------------------------------------------
    // 4. Cleanup
    // -----------------------------------------------------------------------
    LpLabelDoc_Destroy(doc);
    LpPrinter_Destroy(printer);

    printf("\nDone.\n");
    return 0;
}
