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

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

const oldLEDColors = {
    red: 0xFF0000,
    yellow: 0xFCA103,
    green: 0xA9D130
};

function mapLEDColor(color) {
    const r = (color >> 16) & 0xFF;
    const g = (color >> 8) & 0xFF;
    const b = color & 0xFF;

    const dominanceThreshold = 1.4; // higher = more yellow range

    if (r > g * dominanceThreshold && r > b * dominanceThreshold) {
        return oldLEDColors.red;
    } else if (g > r * dominanceThreshold && g > b * dominanceThreshold) {
        return oldLEDColors.green;
    } else {
        return oldLEDColors.yellow;
    }
}

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

    for (let i = 0; i < 2; i++) {
        let arrival = pids.arrivals().get(i);
        if (arrival != null) {
            let destinationColor = oldLEDColors.green;
            if (TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())).replace(" mins", "min").replace(" min", "min").replace(" sec", "sec") == "") {
                destinationColor = oldLEDColors.yellow;
            }

            Text.create("Arrival number")
            .text(`${i + 1}.`)
            .color(destinationColor)
            .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(mapLEDColor(pids.arrivals().get(i).routeColor()))
            .draw(ctx);

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

            Text.create("Arrival destination")
            .text(TextUtil.cycleString(arrival.destination()))
            .color(destinationColor)
            .pos(35, 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("0min")
                .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);
            }
        }
    }
}

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