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

const LEDColors = {
    red: 0xFF0000,
    yellow: 0xFFFF00,
    green: 0x00FF00
};

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

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
}

let localization = {
    "e": {
        departures: "DEPARTURES",
        currentTime: "Current Time",
        time: "Time",
        train: "Train",
        trainNum: "Train No.",
        to: "To",
        track: "Track",
        in: "In",
        boarding: "Boarding"
    },
    "k": {
        departures: "출발",
        currentTime: "현재 시간",
        time: "시간",
        train: "기차",
        trainNum: "열차 번호",
        to: "쪽으로",
        track: "길",
        in: "안에",
        boarding: "탑승"
    },
    "b": {
        departures: "DEPARTURES|출발",
        currentTime: "Current Time|현재 시간",
        time: "Time|시간",
        train: "Train|기차",
        trainNum: "Train No.|열차 번호",
        to: "To|쪽으로",
        track: "Track|길",
        in: "In|안에",
        boarding: "Boarding|탑승"
    }
};

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

    Text.create("Title")
    .text(TextUtil.cycleString(localization[language].departures))
    .color(LEDColors.yellow)
    .pos(93, 1.5)
    .centerAlign()
    .scale(1)
    .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(`${TextUtil.cycleString(localization[language].currentTime)}  ${clockString}`)
    .color(LEDColors.yellow)
    .pos(5, 2.5)
    .scale(0.6)
    .draw(ctx);

    Text.create("header bar boarding")
    .text(TextUtil.cycleString(localization[language].boarding))
    .color(LEDColors.yellow)
    .pos(1, 11)
    .scale(0.3)
    .draw(ctx);

    Text.create("header bar time")
    .text(TextUtil.cycleString(localization[language].time))
    .color(LEDColors.yellow)
    .pos(17, 11)
    .scale(0.3)
    .draw(ctx);

    Text.create("header bar train")
    .text(TextUtil.cycleString(localization[language].train))
    .color(LEDColors.yellow)
    .pos(33, 11)
    .scale(0.3)
    .draw(ctx);

    Text.create("header bar train number")
    .text(TextUtil.cycleString(localization[language].trainNum))
    .color(LEDColors.yellow)
    .pos(72, 11)
    .scale(0.3)
    .draw(ctx);

    Text.create("header bar to")
    .text(TextUtil.cycleString(localization[language].to))
    .color(LEDColors.yellow)
    .pos(90, 11)
    .scale(0.3)
    .draw(ctx);

    Text.create("header bar track")
    .text(TextUtil.cycleString(localization[language].track))
    .color(LEDColors.yellow)
    .pos(156, 11)
    .scale(0.3)
    .draw(ctx);

    Text.create("header bar in")
    .text(TextUtil.cycleString(localization[language].in))
    .color(LEDColors.yellow)
    .pos(170, 11)
    .scale(0.3)
    .draw(ctx);

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

        if (arrival != null) {
            let arrivalDate = new Date(arrival.arrivalTime());
            let arrivalHours = arrivalDate.getHours().toString().padStart(2, "0");
            let arrivalMinutes = arrivalDate.getMinutes().toString().padStart(2, "0");
            let arrivalTime = `${arrivalHours}:${arrivalMinutes}`;

            let arrivalColor;
            if (PIDSUtil.getETAText(arrival.arrivalTime()) == "") {
                arrivalColor = LEDColors.green;
                Texture.create("circle")
                .texture("jsblock:textures/circle.png")
                .color(0xFF0000)
                .size(6, 6)
                .pos(5, i * 7.5 + 16.25)
                .draw(ctx);
            } else {
                arrivalColor = LEDColors.yellow;
            }

            Text.create("Arrival time")
            .text(arrivalTime)
            .color(arrivalColor)
            .pos(17, i * 7.5 + 16.5)
            .scale(0.55)
            .draw(ctx);

            Text.create("Arrival train")
            .text(TextUtil.getNonExtraParts(arrival.routeName()))
            .color(arrivalColor)
            .pos(33, i * 7.5 + 16.5)
            .scale(0.55)
            .draw(ctx);

            Text.create("Arrival train number")
            .text(arrival.routeNumber())
            .color(arrivalColor)
            .pos(72, i * 7.5 + 16.5)
            .scale(0.55)
            .draw(ctx);

            Text.create("Arrival destination")
            .text(arrival.destination())
            .color(arrivalColor)
            .pos(90, i * 7.5 + 16.5)
            .scale(0.55)
            .draw(ctx);

            Text.create("Arrival track number")
            .text(arrival.platformName())
            .color(arrivalColor)
            .pos(156, i * 7.5 + 16.5)
            .scale(0.55)
            .draw(ctx);

            Text.create("Arrival in")
            .text(formatETA(TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())), language))
            .color(arrivalColor)
            .pos(170, i * 7.5 + 16.5)
            .scale(0.55)
            .draw(ctx);
        }
    }
}

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