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

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

function formatETA(arrivalTime, language) {
    const raw = arrivalTime; // e.g. "5 mins", "30 sec"

    const num = parseInt(raw); // gets X

    if (!num) {
        return localization[language].now;
    }

    const isMinute = raw.includes("min");

    if (language === "e") {
        return num + (isMinute ? "m" : "s");
    }

    if (language === "k") {
        return num + (isMinute ? "분" : "초");
    }

    if (language === "b") {
        return num + (isMinute ? "m|" + num + "분" : "s|" + num + "초");
    }

    return raw; // fallback
}

let localization = {
    "e": {
        now: "Now"
    },
    "k": {
        now: "지금"
    },
    "b": {
        now: "Now|지금"
    }
};

function render(ctx, state, pids) {
    let language;
    let languageInput = pids.getCustomMessage(0);
    if (languageInput == "k") {
        language = "k";
    } else if (languageInput == "b") {
        language = "b";
    } else {
        language = "e";
    }


    Texture.create("background")
    .texture("jsblock:textures/bus_pids_large.png")
    .size(186, 60)
    .draw(ctx);

    let arrivingBuses = [];

    for (let i = 0; i < 8; i++) {
        let arrival = pids.arrivals().get(i);

        if (arrival == null) {
            break;
        }

        let arrivalTime = formatETA(TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())), language);
        arrivingBuses.push([arrival.routeNumber(), arrivalTime, arrival.routeColor()]);
    }

    for (let i = 0; i < 4; i++) {
        if (arrivingBuses[i] != null) {
            Texture.create("background")
            .texture("jsblock:textures/bus_icon.png")
            .color(arrivingBuses[i][2])
            .size(11, 11)
            .pos(0.5, i * 15 + 2)
            .draw(ctx);

            Text.create("Arrival route")
            .text(arrivingBuses[i][0])
            .color(0xFFFFFF)
            .pos(12, i * 15 + 4)
            .scale(1.2)
            .draw(ctx);

            Text.create("Arrival time")
            .text(TextUtil.cycleString(arrivingBuses[i][1]))
            .color(0x00FFFF)
            .pos(90, i * 15 + 4)
            .rightAlign()
            .scale(1.2)
            .draw(ctx);
        }
    }

    for (let i = 0; i < 4; i++) {
        if (arrivingBuses[i + 4] != null) {
            Texture.create("background")
            .texture("jsblock:textures/bus_icon.png")
            .color(arrivingBuses[i + 4][2])
            .size(11, 11)
            .pos(94.5, i * 15 + 2)
            .draw(ctx);

            Text.create("Arrival route")
            .text(arrivingBuses[i + 4][0])
            .color(0xFFFFFF)
            .pos(106, i * 15 + 4)
            .scale(1.2)
            .draw(ctx);

            Text.create("Arrival time")
            .text(TextUtil.cycleString(arrivingBuses[i + 3][1]))
            .color(0x00FFFF)
            .pos(184, i * 15 + 4)
            .rightAlign()
            .scale(1.2)
            .draw(ctx);
        }
    }
}

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