/**
 * Example: Programmatic Label Printing (no LSF file required)
 *
 * This demonstrates creating a label entirely in code — defining the layout,
 * adding text, barcode, line, rectangle, image, and RFID objects,
 * then printing directly.
 *
 * Build (MSVC): cl /utf-8 example_programmatic.c /Fe:example_programmatic.exe /I. x64\LP_LabelPrinter.lib
 */

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

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

    // -----------------------------------------------------------------------
    // 1. Create and configure the printer
    // -----------------------------------------------------------------------
    LP_PRINTER printer = LpPrinter_Create();

    // Choose one interface type:
    LpPrinter_SetInterface(printer, LP_RFID_USB);        // USB RFID (driverless)
    // LpPrinter_SetInterface(printer, LP_RFID_NET);     // Network RFID
    // LpPrinter_SetNetAddress(printer, "192.168.1.100"); // IP for NET
    LpPrinter_SetDpi(printer, 300);
    LpPrinter_SetSpeed(printer, 4);      // inches/sec
    LpPrinter_SetDarkness(printer, 10);  // 0-20

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

    // -----------------------------------------------------------------------
    // 2. Create label layout
    // -----------------------------------------------------------------------
    LP_LABEL label = LpLabel_Create();
    LpLabel_SetWidth(label, 70);    // mm
    LpLabel_SetHeight(label, 50);   // mm

    // -----------------------------------------------------------------------
    // 3. Add objects to the label
    // -----------------------------------------------------------------------

    // --- Paragraph text (auto word-wrap, shrink, compress) ---
    int idx = LpLabel_AddText(label, "text-1",
        "Company Name Ltd.", 41.81f, 3.01f,
        12, "Microsoft YaHei", 1, 53.5f);
    LpLabel_SetTextAlign(label, idx, 1);    // center-aligned
    LpLabel_SetFontStyle(label, idx, 1);    // bold

    // --- Single-line text ---
    LpLabel_AddText(label, "text-2", "Model:",  11.38f, 14.50f, 8, "Microsoft YaHei", 0, 0);
    LpLabel_AddText(label, "text-3", "Name:",   11.38f, 21.98f, 8, "Microsoft YaHei", 0, 0);
    LpLabel_AddText(label, "text-4", "Asset:",  11.38f, 29.94f, 8, "Microsoft YaHei", 0, 0);
    LpLabel_AddText(label, "text-5", "Date:",   11.38f, 37.27f, 8, "Microsoft YaHei", 0, 0);

    LpLabel_AddText(label, "text-6", "Laptop", 33.39f, 21.78f, 9, "Microsoft YaHei", 0, 0);
    LpLabel_AddText(label, "text-7", "Sales",  36.96f, 29.74f, 8, "Microsoft YaHei", 0, 0);
    LpLabel_AddText(label, "text-8", "2024-8-20", 33.52f, 37.07f, 9, "Microsoft YaHei", 0, 0);

    // --- Barcode (1D, with text below) ---
    idx = LpLabel_AddBarcode(label, "barcode-1", "ZZ123456",
        30.12f, 13.19f, "Code 128 Auto", 3.0f, 3.0f, 0);
    LpLabel_SetBarcodeText(label, idx, "Arial", 7.0f, 0.2f, 2); // font, size, gap, align

    // --- Barcode (2D, no text) ---
    LpLabel_AddBarcode(label, "barcode-2", "ZZ123456",
        60.08f, 35.09f, "QR Code(2D)", 4.0f, 11.79f, 2);

    // --- Rectangle (rounded) ---
    idx = LpLabel_AddRect(label, "rectangle-1", 1.19f, 2.56f, 68.39f, 47.10f, 0.6f);
    LpLabel_SetRectShape(label, idx, 1);   // 0=normal, 1=rounded, 2=ellipse, 3=triangle
    LpLabel_SetRectRadius(label, idx, 4.0f);

    // --- Lines ---
    idx = LpLabel_AddLine(label, "line-1", 27.43f, 19.64f, 58.42f, 19.64f, 0.6f);
    LpLabel_SetLineDash(label, idx, 2);    // dash-dot style
    LpLabel_AddLine(label, "line-2", 27.43f, 34.89f, 58.42f, 34.89f, 0.6f);
    LpLabel_AddLine(label, "line-3", 27.43f, 27.0f,  58.42f, 27.0f,  0.6f);
    LpLabel_AddLine(label, "line-4", 27.43f, 42.29f, 58.42f, 42.29f, 0.6f);

    // --- Image (base64-encoded PNG/JPEG/BMP) ---
    // LpLabel_AddImage(label, "image-1", 2.87f, 4.20f, 1.0f, base64String);

    // --- RFID object (UHF, hex data, EPC area) ---
    LpLabel_AddRfid(label, "rfiduhf-1", "20260603AAAA0001",
        0,  // encoderType: 0=UHF, 1=HF15693, 2=HF14443A, 3=NFC
        1,  // dataType: 0=text, 1=hex, 2=URL(NDEF), 3=plain text(NDEF)
        0); // dataBlock: 0=EPC, 1=USER

    // -----------------------------------------------------------------------
    // 4. Optional: save preview image before printing
    // -----------------------------------------------------------------------
    // LpLabel_SavePreview(label, printer, "d:/preview.bmp");

    // -----------------------------------------------------------------------
    // 5. Print
    // -----------------------------------------------------------------------
    int result = LpLabel_Print(label, printer);
    printf("Print result: %d %s\n", result,
           result == 0 ? "(success)" : "(error)");

    // -----------------------------------------------------------------------
    // 6. Cleanup
    // -----------------------------------------------------------------------
    LpLabel_Destroy(label);
    LpPrinter_Destroy(printer);

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