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
    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": {
        arrivingSoon: "Soon"
    },
    "k": {
        arrivingSoon: "곧도착"
    },
    "b": {
        arrivingSoon: "Soon|곧도착"
    }
};

// TextUtil.cycleString(localization[language].value)

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.png")
    .size(186, 60)
    .draw(ctx);

    let arrivingSoon = [];
    let laterArrivals = [];

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

        if (arrival == null) {
            break;
        }

        if (PIDSUtil.getETAText(arrival.arrivalTime()) == "") {
            arrivingSoon.push(arrival.routeNumber());
        } else {
            let arrivalTime = formatETA(TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())), language);
            laterArrivals.push([arrival.routeNumber(), arrivalTime]);
        }
    }

    Text.create("Arriving soon text")
    .text(TextUtil.cycleString(localization[language].arrivingSoon) + "  " + arrivingSoon.join(", "))
    .color(0xFFFFFF)
    .pos(3, 49)
    .scale(1.2)
    .draw(ctx);

    for (let i = 0; i < 3; i++) {
        if (laterArrivals[i] != null) {
            Text.create("Arrival route")
            .text(laterArrivals[i][0])
            .color(0xFFFFFF)
            .pos(2, i * 15 + 4)
            .scale(1.2)
            .draw(ctx);

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

    for (let i = 0; i < 3; i++) {
        if (laterArrivals[i + 3] != null) {
            Text.create("Arrival route")
            .text(laterArrivals[i + 3][0])
            .color(0xFFFFFF)
            .pos(96, i * 15 + 4)
            .scale(1.2)
            .draw(ctx);

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

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