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

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

function render(ctx, state, pids) {
    try {
    Texture.create("header")
    .texture("jsblock:textures/rectangle.png")
    .size(pids.width, 19)
    .pos(0, 0)
    .color(0xC9C9C9)
    .draw(ctx);

    Text.create("header left")
    .text("Dest")
    .color(0x000000)
    .pos(2.5, 4)
    .bold()
    .scale(1.7)
    .draw(ctx);

    Text.create("Header right")
    .text("Min")
    .color(0x000000)
    .pos(133.5, 4)
    .rightAlign()
    .bold()
    .scale(1.7)
    .draw(ctx);

    let entries = [];

    for (let j = 0; j < 10; j++) {
        let arrival = pids.arrivals().get(j);
        if (arrival != null) {
            let routeName = arrival.routeName();
            let routeColor = arrival.routeColor();
            let routeNumber = arrival.routeNumber();

            let e0 = entries[0];
            let e1 = entries[1];
            let e2 = entries[2];

            if (
                e0 != null &&
                e0.routeName == routeName &&
                e0.routeColor == routeColor &&
                e0.routeNumber == routeNumber
            ) {
                if (e0.times.length < 2) {
                    e0.times.push(arrival.arrivalTime());
                }
            } else if (
                e1 != null &&
                e1.routeName == routeName &&
                e1.routeColor == routeColor &&
                e1.routeNumber == routeNumber
            ) {
                if (e1.times.length < 2) {
                    e1.times.push(arrival.arrivalTime());
                }
            } else if (
                e2 != null &&
                e2.routeName == routeName &&
                e2.routeColor == routeColor &&
                e2.routeNumber == routeNumber
            ) {
                if (e2.times.length < 2) {
                    e2.times.push(arrival.arrivalTime());
                }
            } else {
                entries.push({
                    routeName: routeName,
                    routeNumber: routeNumber,
                    routeColor: routeColor,
                    destination: arrival.destination(),
                    times: [arrival.arrivalTime()]
                });
            }
        } else {
            break;
        }
    }

    for (let i = 0; i < 3; i++) {
        let entry = entries[i];
        if (entry != null) {
            Texture.create("route color")
            .texture("jsblock:textures/circle.png")
            .size(16, 16)
            .pos(1.5, i * 18.5 + 21)
            .color(entry.routeColor)
            .draw(ctx);

            Text.create("Arrival route")
            .text(entry.routeNumber)
            .color(0xFFFFFF)
            .pos(9.25, i * 18.5 + 25.5)
            .bold()
            .scale(1)
            .centerAlign()
            .draw(ctx);

            Text.create("Arrival destination")
            .text(TextUtil.cycleString(entry.destination))
            .color(0xFFFFFF)
            .pos(20, i*18.5+25.5)
            .bold()
            .scale(1.3)
            .stretchXY()
            .size(50, 100)
            .draw(ctx);

            let arrivalTimes = [];
            for (let k = 0; k < entry.times.length; k++) {
                if (PIDSUtil.getETAText(entry.times[k]) == "") {
                    arrivalTimes.push("0");
                } else {
                    arrivalTimes.push(TextUtil.getNonCjkParts(PIDSUtil.getETAText(entry.times[k])).replace(" mins", "").replace(" min", "").replace(" sec", "s"));
                }
            }
            let arrivalTimesString = arrivalTimes.join(", ");

            Text.create("Arrival time")
            .text(arrivalTimesString)
            .color(0xFFFFFF)
            .pos(132.5, i*18.5+25.5)
            .rightAlign()
            .bold()
            .scale(1.3)
            .draw(ctx);
        }
    }
} catch(err) {
    Text.create("Error")
            .text(err)
            .color(0xFF0000)
            .pos(2, 2)
            .scale(1)
            .wrapText()
            .size(132, 72)
            .draw(ctx);
}
}

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