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/rectangle.png")
    .size(pids.width, pids.height)
    .pos(0, 0)
    .color(0x1E2224)
    .draw(ctx);

    Texture.create("header")
    .texture("jsblock:textures/rectangle.png")
    .size(pids.width, 10)
    .pos(0, 0)
    .color(0x363D40)
    .draw(ctx);

    Text.create("station name")
    .text(pids.station().getName() + " Station")
    .color(0xFFFFFF)
    .pos(2.5, 2)
    .bold()
    .scale(0.8)
    .stretchXY()
    .size(100, 100)
    .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 dateString = `${hours}:${minutes} ${ampm}`;

    Text.create("Clock")
    .text(dateString)
    .color(0xFFFFFF)
    .pos(133.5, 2)
    .rightAlign()
    .bold()
    .scale(0.8)
    .draw(ctx);

    for (let i = 0; i < 4; i++) {
        let arrival = pids.arrivals().get(i);
        if (arrival != null) {
            Texture.create("route color")
            .texture("jsblock:textures/circle.png")
            .size(13, 13)
            .pos(3, i * 16.5 + 11.5)
            .color(pids.arrivals().get(i).routeColor())
            .draw(ctx);

            Text.create("Arrival route")
            .text(TextUtil.cycleString(arrival.routeNumber()))
            .color(0xFFFFFF)
            .pos(9.25, i * 16.5 + 15.25)
            .bold()
            .scale(0.8)
            .centerAlign()
            .draw(ctx);

            Text.create("Arrival destination")
            .text(TextUtil.cycleString(arrival.destination()))
            .color(0xFFFFFF)
            .pos(19, i*16.5+15)
            .bold()
            .scale(1.1)
            .stretchXY()
            .size(60, 100)
            .draw(ctx);

            if (TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())).replace(" mins", " min").replace(" min", " min").replace(" sec", " sec") == "") {
                Text.create("Arrival time")
                .text("Now")
                .color(0xFFFFFF)
                .pos(132.5, i*16.5+15)
                .rightAlign()
                .bold()
                .scale(1.1)
                .draw(ctx);
            } else {
                Text.create("Arrival time")
                .text(TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())).replace(" mins", " min").replace(" min", " min").replace(" sec", " sec"))
                .color(0xFFFFFF)
                .pos(132.5, i*16.5+15)
                .rightAlign()
                .bold()
                .scale(1.1)
                .draw(ctx);
            }
        }
    }
}

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