include(Resources.id("jsblock:scripts/pids_util.js"));
var eta;

function create(ctx, state, pids) {
    print("Hello World ^^");
}

const oldLEDColors = {
    red: 0xFF0000,
    yellow: 0xFCA103,
    green: 0xA9D130
};

function render(ctx, state, pids) {
    Texture.create("background")
    .texture("jsblock:textures/led_bg.png")
    .size(pids.width, pids.height)
    .draw(ctx);

    Text.create()
    .text("LN  CAR       DEST       MIN")
    .color(oldLEDColors.red)
    .pos(0, 3)
    .scale(1.25)
    .draw(ctx);

    for (let i = 0; i < pids.rows; i++) {
        let arrival = pids.arrivals().get(i);
        if (arrival != null) {
            Text.create("Arrival route")
            .text(TextUtil.cycleString(arrival.routeNumber()))
            .color(oldLEDColors.yellow)
            .pos(0, i*16.75+15.5)
            .scale(1.25)
            .draw(ctx);

            Text.create("Arrival length")
            .text(TextUtil.cycleString(arrival.carCount()))
            .color(oldLEDColors.yellow)
            .pos(27, i*16.75+15.5)
            .scale(1.25)
            .draw(ctx);

            Text.create("Arrival destination")
            .text(TextUtil.cycleString(arrival.destination()))
            .color(oldLEDColors.yellow)
            .pos(48, i*16.75+15.5)
            .scale(1.25)
            .stretchXY()
            .size(45, 100)
            .draw(ctx);

            if (TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())).replace(" sec","s").replace(" mins","").replace(" min","") == "") {
                Text.create("Arrival time")
                .text("BRD")
                .color(oldLEDColors.yellow)
                .pos(135, i*16.75+15.5)
                .rightAlign()
                .scale(1.25)
                .draw(ctx);
            } else {
                Text.create("Arrival time")
                .text(TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())).replace(" sec","s").replace(" mins","").replace(" min",""))
                .color(oldLEDColors.yellow)
                .pos(135, i*16.75+15.5)
                .rightAlign()
                .scale(1.25)
                .draw(ctx);
            }
        }
    }
}

function dispose(ctx, state, pids) {
    print("Goodbye World ^^;");
}