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

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

let localization = {
    "e": {
        this: "THIS",
        next: "NEXT"
    },
    "k": {
        this: "이것",
        next: "다음"
    },
    "b": {
        this: "THIS|이것",
        next: "NEXT|다음"
    }
};

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

    if (raw == "") {
        if (language == "e") {
            return "0 min.";
        }
        if (language == "k") {
            return "0 분";
        }
        if (language == "b") {
            return "0 min.|0 분";
        }
    }

    const num = parseInt(raw); // gets X
    const isMinute = raw.includes("min");

    if (language === "e") {
        return num + (isMinute ? " min." : " sec.");
    }

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

    if (language === "b") {
        return num + (isMinute ? " min.|" + num + " 분" : " sec.|" + num + " 초");
    }

    return raw; // fallback
}

function render(ctx, state, pids) {
    Texture.create("background")
    .texture("jsblock:textures/gtx_a_bg.png")
    .size(136, 76)
    .draw(ctx);


    let language;
    let languageInput = pids.getCustomMessage(0);
    if (languageInput == "k") {
        language = "k";
    } else if (languageInput == "b") {
        language = "b";
    } else {
        language = "e";
    }

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

        if (arrival != null) {
            Text.create("Arrival destination")
            .text(TextUtil.cycleString(arrival.destination()))
            .color(0xFFFFFF)
            .pos(2, i * 20 + 20)
            .scale(1.25)
            .stretchXY()
            .size(70, 100)
            .draw(ctx);

            Text.create("Arrival time")
            .text(TextUtil.cycleString(formatETA(TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())), language)))
            .color(0xFFFF00)
            .pos(133, i * 20 + 20)
            .rightAlign()
            .scale(1.25)
            .draw(ctx);
        }
    }

    let dateObj = new Date();
    let hours = dateObj.getHours().toString().padStart(2, "0");
    let minutes = dateObj.getMinutes().toString().padStart(2, "0");

    let clockString = `${hours}:${minutes}`;

    Text.create("Clock")
    .text(clockString)
    .color(0xFFFFFF)
    .pos(135, 1.5)
    .scale(0.9)
    .rightAlign()
    .draw(ctx);

    let firstArrival = pids.arrivals().get(0);
    let lineColor;
    if (firstArrival != null) {
        lineColor = firstArrival.routeColor();

        let previousColor = lineColor;

        for (let i = 0; i < 10; i++) {
            let checkArrival = pids.arrivals().get(i);
            if (checkArrival == null) {
                break;
            }

            if (previousColor != checkArrival.routeColor()) {
                lineColor = 0x888888;
                break;
            }
        }

        Texture.create("route line")
        .texture("jsblock:textures/rectangle.png")
        .color(lineColor)
        .size(136, 3)
        .pos(0, 60)
        .draw(ctx);

        Texture.create("route station circle")
        .texture("jsblock:textures/gtx_stop_circle.png")
        .size(7, 7)
        .pos(114, 58)
        .draw(ctx);

        Text.create("Station name")
        .text(TextUtil.cycleString(pids.station().getName()))
        .color(0xFFFFFF)
        .pos(125, 66)
        .scale(0.75)
        .rightAlign()
        .draw(ctx);
    }

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

        let arrivalTimeString = TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime()));
        let arrivalTimestamp = arrival.arrivalTime();

        let arrivalSeconds;
        if (arrivalTimeString == "") {
            arrivalSeconds = 0;
        } else if (arrivalTimeString.includes("sec")) {
            arrivalSeconds = (arrivalTimestamp - Date.now()) / 1000;
        } else {
            break;
        }

        Texture.create("train")
        .texture("jsblock:textures/gtx_train.png")
        .size(12, 6)
        .pos(111.5 - ((arrivalSeconds / 60) * 110), 54)
        .draw(ctx);
    }
}

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

// At station: 112.5
// At start: 2.5