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) {
    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 index")
            .text(TextUtil.cycleString(i == 0 ? localization[language].this : localization[language].next))
            .color(0xFFFFFF)
            .pos(1, i * 20 + 17.5)
            .scale(1)
            .draw(ctx);

            Text.create("Arrival destination")
            .text(TextUtil.cycleString(arrival.destination()))
            .color(0xFFFF00)
            .pos(24, i * 20 + 18)
            .scale(1.25)
            .stretchXY()
            .size(55, 100)
            .draw(ctx);

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

    let dateObj = new Date();
    let month = (dateObj.getMonth() + 1).toString().padStart(2, "0");
    let dayOfMonth = dateObj.getDate().toString().padStart(2, "0");
    let hours = dateObj.getHours().toString().padStart(2, "0");
    let minutes = dateObj.getMinutes().toString().padStart(2, "0");
    let year = dateObj.getFullYear();

    let clockString = `${hours}:${minutes}`;
    let dateString;
    if (language == "e") {
        dateString = `${year}/${month}/${dayOfMonth}`;
    } else if (language == "k") {
        dateString = `${year}년 ${month}월 ${dayOfMonth}일`;
    } else if (language == "b") {
        dateString = `${year}/${month}/${dayOfMonth}|${year}년 ${month}월 ${dayOfMonth}일`;
    }

    Text.create("Clock")
    .text(clockString)
    .color(0xFFFFFF)
    .pos(68, 1.5)
    .scale(1)
    .centerAlign()
    .draw(ctx);

    Text.create("Date")
    .text(TextUtil.cycleString(dateString))
    .color(0xFFFFFF)
    .pos(135, 1.5)
    .scale(0.75)
    .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 1")
        .texture("jsblock:textures/circle.png")
        .color(lineColor)
        .size(5, 5)
        .pos(115, 59)
        .draw(ctx);

        Texture.create("route station circle 2")
        .texture("jsblock:textures/circle.png")
        .color(0xFFFFFF)
        .size(3, 3)
        .pos(116, 60)
        .draw(ctx);

        Text.create("Station name")
        .text(TextUtil.cycleString(pids.station().getName()))
        .color(0xFFFF00)
        .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 arrivalColor = arrival.routeColor();
        let arrivalTimeString = TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime()));
        let arrivalTimestamp = arrival.arrivalTime();

        let trainIcon;
        let arrivalSeconds;
        if (arrivalTimeString == "") {
            trainIcon = "jsblock:textures/sm_train_1.png";
            arrivalSeconds = 0;
        } else if (arrivalTimeString.includes("sec")) {
            trainIcon = "jsblock:textures/sm_train_2.png";
            arrivalSeconds = (arrivalTimestamp - Date.now()) / 1000;
        } else {
            break;
        }

        Texture.create("train color")
        .texture("jsblock:textures/sm_train_color.png")
        .color(arrivalColor)
        .size(12, 6)
        .pos(112.5 - ((arrivalSeconds / 60) * 110), 54)
        .draw(ctx);

        Texture.create("train main")
        .texture(trainIcon)
        .size(12, 6)
        .pos(112.5 - ((arrivalSeconds / 60) * 110), 54)
        .draw(ctx);
    }
}

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

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