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

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

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";
    }

    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();
    let minutes = dateObj.getMinutes().toString().padStart(2, "0");

    let isPM = false;
    if (hours > 12) {
        hours -= 12;
        isPM = true;
    }
    if (hours == 0) {
        hours = 12;
    }
    hours = hours.toString().padStart(2, "0");

    let clockString;
    if (language == "e") {
        clockString = `${hours}:${minutes} ${isPM ? "PM" : "AM"}`;
    } else if (language == "k") {
        clockString = `${isPM ? "오후" : "오전"} ${hours}:${minutes}`;
    } else if (language == "b") {
        clockString = `${hours}:${minutes} ${isPM ? "PM" : "AM"}|${isPM ? "오후" : "오전"} ${hours}:${minutes}`;
    }

    let dateString;
    if (language == "e") {
        dateString = `${month}/${dayOfMonth}`;
    } else if (language == "k") {
        dateString = `${month}월 ${dayOfMonth}일`;
    } else if (language == "b") {
        dateString = `${month}/${dayOfMonth}|${month}월 ${dayOfMonth}일`;
    }
    
    Text.create("Date")
    .text(TextUtil.cycleString(dateString))
    .color(0xFFFFFF)
    .pos(3, 3)
    .scale(1)
    .draw(ctx);

    Text.create("Clock")
    .text(TextUtil.cycleString(clockString))
    .color(0xFFFFFF)
    .pos(3, 12)
    .scale(1)
    .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(0xFFFFFF)
        .size(5, 5)
        .pos(115, 59)
        .draw(ctx);

        Texture.create("route station circle 2")
        .texture("jsblock:textures/circle.png")
        .color(lineColor)
        .size(3, 3)
        .pos(116, 60)
        .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 arrivalColor = arrival.routeColor();
        let arrivalTimeString = TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime()));
        let arrivalTimestamp = arrival.arrivalTime();

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

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

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

        let destinationString;
        if (language == "e") {
            destinationString = `for ${TextUtil.getNonCjkParts(arrival.destination())}`;
        } else if (language == "k") {
            destinationString = `${TextUtil.getCjkParts(arrival.destination())}행`;
        } else if (language == "b") {
            destinationString = `for ${TextUtil.getNonCjkParts(arrival.destination())}|${TextUtil.getCjkParts(arrival.destination())}행`;
        }

        /*Text.create("Train destination")
        .text(destinationString)
        .color(0xFFFFFF)
        .pos(117 - ((arrivalSeconds / 60) * 110), 48)
        .scale(0.5)
        .centerAlign()
        .draw(ctx);*/
    }
}

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