
import {
    Label,
    Grid,
    Text,
    Line,
    Box,
    Circle,
    Barcode,
    FontFamily,
    PrintDensity,
    Spacing,
    SizeType
} from "jszpl";

import {
    Printer as PrinterTable
} from "../sequelize/printer.model.mjs";

import {
    Template as TemplateTable
} from "../sequelize/template.model.mjs";

import {
    Medium as MediaTable
} from "../sequelize/media.model.mjs";

import {
    Queue as QueueTable
} from "../sequelize/queue.model.mjs";

import {
    Job as JobTable
} from "../sequelize/job.model.mjs";




export async function GenerateZPL(queueId, limit = undefined)
{
    let queue = await QueueTable.findByPk(queueId);
    let printer = await queue.getPrinter();
    let medium = await printer.getMedium();

    let jobs = await queue.getJobs({
        limit
    });

    var Result = '';

    while (jobs.length > 0) {
        // get jobs for row
        let nextJobs = jobs.splice(0, medium.columns);

        // generate new Label
        const label = new Label();
        label.printDensity = new PrintDensity(printer.density);
        label.width = 100;
        label.height = 50;
        label.padding = new Spacing(10);

        // generate Grid
        const grid = new Grid();
        label.content.push(grid);
        grid.border = 1;
        grid.columnSpacing = 2;
        grid.rowSpacing = 0;

        for (let cIndex = 0; cIndex < medium.columns; cIndex++) {
            grid.columns.push(new Size(1, SizeType.Relative));
        }

        grid.rows.push(new Size(1, SizeType.Relative));

        nextJobs.forEach((job, columnNumber) => {
            
            let template = job.getTemplate();
            const text = new Text();
            label.content.push(text);
            text.fontFamily = new FontFamily("0");
            text.text = 'Hello World!';
            
        });
        
        Result += label.generateZPL();
    }

    return Result;
}
