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

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

function render(ctx, state, pids) {
    Texture.create("background")
    .texture("jsblock:textures/rectangle.png")
    .size(pids.width, pids.height)
    .pos(0, 0)
    .color(0xFFFFFF)
    .draw(ctx);

    Texture.create("line")
    .texture("jsblock:textures/rectangle.png")
    .size(pids.width, 0.75)
    .pos(0, 24)
    .color(0xAAAAAA)
    .draw(ctx);

    Texture.create("bottom bar left")
    .texture("jsblock:textures/rectangle.png")
    .size(45, 11)
    .pos(0, 49)
    .color(0xAAAAAA)
    .draw(ctx);

    Texture.create("bottom bar right")
    .texture("jsblock:textures/rectangle.png")
    .size(140.25, 11)
    .pos(45.75, 49)
    .color(0x000000)
    .draw(ctx);

    for (let i = 0; i < 2; i++) {
        let arrival = pids.arrivals().get(i);
        if (arrival != null) {
            let destinationColor = 0x000000;
            if (TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())).replace(" mins", "min").replace(" min", "min").replace(" sec", "sec") == "") {
                destinationColor = 0xFFFF00;

                Texture.create("arrival box")
                .texture("jsblock:textures/rectangle.png")
                .size(pids.width, 24)
                .pos(0, i * 24.75)
                .color(0x000000)
                .draw(ctx);
            }

            Text.create("Arrival number")
            .text(`${i + 1}.`)
            .color(destinationColor)
            .pos(6, i * 24 + 8.25)
            .scale(1.25)
            .centerAlign()
            .draw(ctx);

            Texture.create("route color")
            .texture("jsblock:textures/circle.png")
            .size(18, 18)
            .pos(11.75, i * 24 + 3)
            .color(pids.arrivals().get(i).routeColor())
            .draw(ctx);

            Text.create("Arrival route")
            .text(TextUtil.cycleString(arrival.routeNumber()))
            .color(0xFFFFFF)
            .pos(20.75, i * 24 + 8.25)
            .scale(1.25)
            .centerAlign()
            .draw(ctx);

            Text.create("Arrival destination")
            .text(TextUtil.cycleString(arrival.destination()))
            .color(destinationColor)
            .pos(35, i*24+7)
            .scale(1.75)
            .stretchXY()
            .size(55, 100)
            .draw(ctx);

            if (TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())).replace(" mins", "min").replace(" min", "min").replace(" sec", "sec") == "") {
                Text.create("Arrival time")
                .text("0min")
                .color(destinationColor)
                .pos(182, i*24+7)
                .rightAlign()
                .scale(1.75)
                .draw(ctx);
            } else {
                Text.create("Arrival time")
                .text(TextUtil.getNonCjkParts(PIDSUtil.getETAText(arrival.arrivalTime())).replace(" mins", "min").replace(" min", "min").replace(" sec", "sec"))
                .color(destinationColor)
                .pos(182, i*24+7)
                .rightAlign()
                .scale(1.75)
                .draw(ctx);
            }
        }
    }

    let dateObj = new Date();
    let hours = dateObj.getHours();
    let ampm = "AM";
    if (hours > 12) {
        hours -= 12;
        ampm = "PM";
    }
    if (hours == 0) {
        hours = 12;
    }
    let minutes = dateObj.getMinutes().toString().padStart(2, "0");

    let dateString = `${hours}:${minutes} ${ampm}`;

    Text.create("Clock")
    .text(dateString)
    .color(0xFFFFFF)
    .pos(1.5, 52)
    .scale(0.8)
    .draw(ctx);

    let weatherImg;
    if (MinecraftClient.worldIsThundering()) {
        weatherImg = "jsblock:textures/nyc_weather_thunder.png";
    } else if(MinecraftClient.worldIsRaining()) {
        weatherImg = "jsblock:textures/nyc_weather_rainy.png";
    } else {
        weatherImg = "jsblock:textures/nyc_weather_sunny.png";
    }

    Texture.create("weather icon")
    .texture(weatherImg)
    .size(8, 8)
    .pos(35, 50.5)
    .draw(ctx);
}

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