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

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

function render(ctx, state, pids) {
    for (let i = 0; i < 2; i++) {
        let arrival = pids.arrivals().get(i);
        if (arrival != null) {
            let destinationColor = 0xFFFFFF;
            if (TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())).replace(" mins", " Min").replace(" min", " Min").replace(" sec", " Sec") == "") {
                destinationColor = 0xFFFF00;
            }

            Text.create("Arrival number")
            .text(`${i + 1}.`)
            .color(0xFFFFFF)
            .pos(6, i * 24 + 13.25)
            .scale(1.25)
            .centerAlign()
            .draw(ctx);

            Texture.create("route color")
            .texture("jsblock:textures/circle.png")
            .size(18, 18)
            .pos(11.75, i * 24 + 8)
            .color(pids.arrivals().get(i).routeColor())
            .draw(ctx);

            Text.create("Arrival route")
            .text(TextUtil.cycleString(arrival.routeNumber()))
            .color(0xFFFFFF)
            .pos(20.75, i * 24 + 13.25)
            .scale(1.25)
            .centerAlign()
            .draw(ctx);

            Text.create("Arrival destination")
            .text(TextUtil.cycleString(arrival.destination()).toUpperCase())
            .color(0xFFFFFF)
            .pos(33, i*24+12)
            .scale(1.75)
            .stretchXY()
            .size(55, 100)
            .draw(ctx);

            if (TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())).replace(" mins", " Min").replace(" min", " Min").replace(" sec", " Sec") == "") {
                Text.create("Arrival time")
                .text("0 Min")
                .color(destinationColor)
                .pos(182, i*24+12)
                .rightAlign()
                .scale(1.75)
                .draw(ctx);
            } else {
                Text.create("Arrival time")
                .text(TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())).replace(" mins", " Min").replace(" min", " Min").replace(" sec", " Sec"))
                .color(destinationColor)
                .pos(182, i*24+12)
                .rightAlign()
                .scale(1.75)
                .draw(ctx);
            }
        }
    }

    Texture.create("line")
    .texture("jsblock:textures/rectangle.png")
    .size(pids.width, 0.75)
    .pos(0, 29)
    .color(0xFFFFFF)
    .draw(ctx);

    const weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    let dateObj = new Date();
    let weekday = weekdays[dateObj.getDay()];
    let month = months[dateObj.getMonth()];
    let dayOfMonth = dateObj.getDate();
    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 = `${weekday}, ${month} ${dayOfMonth}, ${hours}:${minutes} ${ampm}`;

    Text.create("Clock")
    .text(dateString)
    .color(0xFFFF00)
    .pos(182, 2.5)
    .rightAlign()
    .scale(0.8)
    .draw(ctx);
}

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