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/amtrak_lcd_pids.png")
    .size(pids.width, pids.height)
    .draw(ctx);

    let dateObj = new Date();
    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 clockString = `${hours}:${minutes} ${ampm}`;

    Text.create("Clock")
    .text(clockString)
    .color(0x003E7E)
    .pos(131, 0.75)
    .scale(0.7)
    .rightAlign()
    .draw(ctx);


    for (let i = 0; i < 10; i++) {
        let arrival = pids.arrivals().get(i);
        if (arrival != null) {
            let arrivalDate = new Date(arrival.arrivalTime());
            let arrivalHours = arrivalDate.getHours();
            let arrivalampm = "a";
            if (arrivalHours > 12) {
                arrivalHours -= 12;
                let arrivalampm = "p";
            }
            if (arrivalHours == 0) {
                arrivalHours = 12;
            }
            let arrivalMinutes = arrivalDate.getMinutes().toString().padStart(2, "0");
            let arrivalTime = `${arrivalHours}:${arrivalMinutes}${arrivalampm}`;

            let arrivalStatus = "On Time";
            if (TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())) == "") {
                arrivalStatus = "Boarding";
            }

            Text.create("Arrival time")
            .text(arrivalTime)
            .color(0xFFFFFF)
            .pos(7.5, i*3.75+11.25)
            .scale(0.4)
            .centerAlign()
            .draw(ctx);

            Text.create("Arrival route number")
            .text(arrival.routeNumber())
            .color(0xFFFFFF)
            .pos(20.25, i*3.75+11.25)
            .scale(0.4)
            .centerAlign()
            .draw(ctx);

            Text.create("Arrival route name")
            .text(TextUtil.getNonExtraParts(arrival.routeName()))
            .color(0xFFFFFF)
            .pos(30, i*3.75+11.25)
            .scale(0.4)
            .stretchXY()
            .size(75, 100)
            .draw(ctx);

            Text.create("Arrival destination")
            .text(TextUtil.cycleString(arrival.destination()))
            .color(0xFFFFFF)
            .pos(64.25, i*3.75+11.25)
            .scale(0.4)
            .stretchXY()
            .size(100, 100)
            .draw(ctx);

            if (arrivalStatus == "Boarding") {
                Texture.create("boarding rectangle")
                .texture("jsblock:textures/rectangle.png")
                .color(0xFFFF00)
                .size(17, 3.7)
                .pos(109, i * 3.75 + 11.07)
                .draw(ctx);
            }

            Text.create("Arrival status")
            .text(arrivalStatus)
            .color((arrivalStatus == "Boarding") ? 0x000000 : 0xFFFFFF)
            .pos(109.5, i*3.75+11.25)
            .scale(0.4)
            .draw(ctx);

            Text.create("Arrival platform number")
            .text(arrival.platformName())
            .color(0xFFFFFF)
            .pos(130.75, i*3.75+11.25)
            .scale(0.4)
            .centerAlign()
            .draw(ctx);
        }
    }

    Text.create("Custom message")
    .text(pids.getCustomMessage(0))
    .color(0xFFFFFF)
    .pos(2, 54)
    .scale(0.4)
    .wrapText()
    .size(400, 70)
    .draw(ctx);
}

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