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

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

const LEDColors = {
    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(LEDColors.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(LEDColors.yellow)
            .pos(0, i*16.75+15.5)
            .scale(1.25)
            .draw(ctx);

            if (TextUtil.cycleString(arrival.carCount()) >= 8) {
                Text.create("Arrival length")
                .text(TextUtil.cycleString(arrival.carCount()))
                .color(LEDColors.green)
                .pos(27, i*16.75+15.5)
                .scale(1.25)
                .draw(ctx);
            } else {
                Text.create("Arrival length")
                .text(TextUtil.cycleString(arrival.carCount()))
                .color(LEDColors.yellow)
                .pos(27, i*16.75+15.5)
                .scale(1.25)
                .draw(ctx);
            }

            Text.create("Arrival destination")
            .text(TextUtil.cycleString(arrival.destination()))
            .color(LEDColors.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(LEDColors.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(LEDColors.yellow)
                .pos(135, i*16.75+15.5)
                .rightAlign()
                .scale(1.25)
                .draw(ctx);
            }
        }
    }
}

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