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(0x292929)
    .draw(ctx);

    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 = 0x000000;

                Texture.create("top arrival box")
                .texture("jsblock:textures/rectangle.png")
                .size(174, 29.5)
                .pos(12, i * 30)
                .color(0xFFFFFF)
                .draw(ctx);
            } else {
                Texture.create("top arrival box")
                .texture("jsblock:textures/rectangle.png")
                .size(174, 29.5)
                .pos(12, i * 30.5)
                .color(0x5C5C5C)
                .draw(ctx);
            }

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

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

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

            Text.create("Arrival destination")
            .text(TextUtil.cycleString(arrival.destination()))
            .color(destinationColor)
            .pos(36, i*30.5+9)
            .scale(1.75)
            .stretchXY()
            .size(65, 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")
                .color(destinationColor)
                .pos(173, i*30+5)
                .centerAlign()
                .bold()
                .scale(2)
                .draw(ctx);

                Text.create("Arrival time label")
                .text("MIN")
                .color(destinationColor)
                .pos(173, i*30+20)
                .centerAlign()
                .scale(0.75)
                .draw(ctx);
            } else {
                Text.create("Arrival time")
                .text(TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())).replace(" mins", "").replace(" min", "").replace(" sec", ""))
                .color(destinationColor)
                .pos(173, i*30+5)
                .centerAlign()
                .bold()
                .scale(2)
                .draw(ctx);

                if (TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())).includes("sec")) {
                    Text.create("Arrival time label")
                    .text("SEC")
                    .color(destinationColor)
                    .pos(173, i*30+20)
                    .centerAlign()
                    .scale(0.75)
                    .draw(ctx);
                } else {
                    Text.create("Arrival time label")
                    .text("MIN")
                    .color(destinationColor)
                    .pos(173, i*30+20)
                    .centerAlign()
                    .scale(0.75)
                    .draw(ctx);
                }
            }
        }
    }
}

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