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

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

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

    let arrival = pids.arrivals().get(0);
    if (arrival != null) {
        let routeNumberSpace = "";
        if (arrival.routeNumber()) {
            routeNumberSpace = " ";
        }

        Text.create("Arrival route + destination")
        .text(TextUtil.cycleString(arrival.routeNumber()) + routeNumberSpace + TextUtil.cycleString(arrival.destination()))
        .color(0xFFFFFF)
        .pos(0, 16)
        .scale(2)
        .stretchXY()
        .size(60, 100)
        .draw(ctx);

        if (TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())).replace(" sec"," SEC").replace(" mins"," MIN").replace(" min","") == "") {
            Text.create("Arrival time")
            .text("0 MIN")
            .color(0xFFFFFF)
            .pos(185, 16)
            .rightAlign()
            .scale(2)
            .draw(ctx);
        } else {
            Text.create("Arrival time")
            .text(TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())).replace(" sec"," SEC").replace(" mins"," MIN").replace(" min"," MIN"))
            .color(0xFFFFFF)
            .pos(185, 16)
            .rightAlign()
            .scale(2)
            .draw(ctx);
        }
    }

    const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    let dateObj = new Date();
    let month = months[dateObj.getMonth()];
    let dayOfMonth = dateObj.getDate();
    let year = dateObj.getFullYear();
    let hours = dateObj.getHours();
    let ampm = "am";
    if (hours > 12) {
        hours -= 12;
        ampm = "pm";
    }
    if (hours == 0) {
        hours = 12;
    }
    let minutes = dateObj.getMinutes().toString().padStart(2, "0");

    let dateString = `${month} ${dayOfMonth}, ${year}    ${hours}:${minutes}${ampm}`;

    Text.create("clock")
    .text(dateString)
    .color(0xFFFFFF)
    .pos(93, 42)
    .centerAlign()
    .scale(1.85)
    .draw(ctx);
}

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