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

const HEADER_HEIGHT = 13;


function create(ctx, state, pids) {
}



function render(ctx, state, pids) {


    Texture.create("Background")
        .texture("jsblock:textures/block/pids/pids_ccl_new_bg.png")
        .size(pids.width, pids.height)
        .draw(ctx);



    // ─── Top Bar ───

    let arrivalsWrapper = pids.arrivals();

    let hasPlatform =
        arrivalsWrapper != null &&
        arrivalsWrapper.platforms().size() > 0;



    if (!hasPlatform) {

        Text.create("PlatformClosed")
            .text("Platform Closed")
            .color(0xFFFF00)
            .pos(pids.width - 5, 2)
            .scale(0.9)
            .rightAlign()
            .draw(ctx);

        return;
    }




    let firstArrival = arrivalsWrapper.get(0);

    let seconds = 999;


    if(firstArrival != null){

        seconds = Math.floor(
            (firstArrival.arrivalTime() - Date.now()) / 1000
        );

    }






// ─── Platform / Arriving In / Clock ───
if (seconds > 10) {

    // Platform (top-left)
    Text.create("Platform Label")
        .text("Platform " + arrivalsWrapper.get(0).platformName())
        .pos(6, 2)
        .scale(0.9)
        .color(0xFFFFFF)
        .draw(ctx);

    // Arriving In (top-right)
    Text.create("Arriving In")
        .text("Arriving in")
        .pos(pids.width - 6, 2)
        .rightAlign()
        .scale(0.8)
        .color(0xFFFFFF)
        .draw(ctx);

Text.create("Clock")
    .text(PIDSUtil.formatTime(MinecraftClient.worldDayTime(), true))
    .pos(6, pids.height - 4)
    .scale(0.7) // slightly smaller
    .color(0xFFFFFF)
    .draw(ctx);
}

    // =====================================================
    // APPROACHING PAGE
    // >10 SEC
    // =====================================================

    if(seconds > 10){

Texture.create("Top Divider")
    .texture("minecraft:textures/block/white_concrete.png")
    .pos(4, 12)
    .size(pids.width - 8, 0.5)
    .draw(ctx);

let platformName = arrivalsWrapper.platforms().get(0).getName();

    for(let i = 0; i < 2; i++){

        let arrival = arrivalsWrapper.get(i);

        if(arrival == null || pids.isRowHidden(i))
            continue;

        let rowY = 26 + (i * 25);



// Determine header and subtitle
let destinationText = arrival.destination();
let subtitleText = "ends at";

if (destinationText.indexOf("Anticlockwise") >= 0) {
    subtitleText = "via";
    destinationText = "Anticlockwise";
} else if (destinationText.indexOf("Clockwise") >= 0) {
    subtitleText = "via";
    destinationText = "Clockwise";
}

// First line (large heading)
Text.create("Approaching Header")
    .text(destinationText)
    .pos(8, rowY)
    .size(pids.width - 42, 8)
    .scaleXY()
    .scale(1.15)
    .color(0xFFFFFF)
    .draw(ctx);

// "via" / "ends at" (fixed width so it never overlaps)
Text.create("Approaching Subtitle")
    .text(subtitleText)
    .pos(8, rowY + 11)
    .size(14, 7)
    .scaleXY()
    .color(0xFFFFFF)
    .draw(ctx);

// MRT Caplet Fill
Texture.create("Approaching Caplet Fill")
    .texture("jsblock:textures/block/pids/ccl_new_misc/mrt_caplet.png")
    .pos(28, rowY + 10)
    .size(20, 10)
    .color(arrival.routeColor())
    .draw(ctx);

// MRT Caplet Outline
Texture.create("Approaching Caplet Outline")
    .texture("jsblock:textures/block/pids/ccl_new_misc/mrt_caplet_outline.png")
    .pos(28, rowY + 10)
    .size(20, 10)
    .draw(ctx);

// Platform Number
Text.create("Approaching Platform")
    .text(arrival.platformName())
    .pos(38, rowY + 11)
    .size(9, 9)
    .scaleXY()
    .centerAlign()
    .color(0xFFFFFF)
    .draw(ctx);

// Destination
Text.create("Approaching Destination")
    .text(TextUtil.cycleString(arrival.destination()))
    .pos(52, rowY + 11)
    .size(42, 7)
    .scaleXY()
    .color(0xFFFFFF)
    .draw(ctx);


        let eta = Math.ceil(
            (arrival.arrivalTime() - Date.now()) / 60000
        );



        Text.create("Approaching ETA")
            .text(
                eta <= 0 ? "ARR" : String(eta)
            )
            .pos(pids.width - 8,rowY)
            .rightAlign()
            .scale(1.15)
            .color(0xFFFFFF)
            .draw(ctx);


        if(eta > 1){

            Text.create("Approaching ETA Unit")
                .text("mins")
                .pos(pids.width - 8,rowY + 10)
                .rightAlign()
                .scale(0.85)
                .color(0xFFFFFF)
                .draw(ctx);


        } else if(eta == 1){

            Text.create("Approaching ETA Unit")
                .text("min")
                .pos(pids.width - 8,rowY + 11)
                .rightAlign()
                .scale(0.85)
                .color(0xFFFFFF)
                .draw(ctx);

        }




        if(i == 0){

            Texture.create("Divider Line")
                .texture("minecraft:textures/block/white_concrete.png")
                .pos(4,rowY + 21)
                .size(pids.width - 8,0.5)
                .draw(ctx);

        }

    }

    return;

}






    // =====================================================
    // ORIGINAL PAGE
    // <=10 SEC
    // =====================================================

let platformName =
    arrivalsWrapper.platforms().get(0).getName();

// Determine direction text and subtitle
let directionText = "Clockwise";
let viaText = "ends at";

if (firstArrival != null) {
    let destination = firstArrival.destination();

    if (destination.indexOf("Anticlockwise") >= 0) {
        directionText = "Anticlockwise";
        viaText = "via";
    } else if (destination.indexOf("Clockwise") >= 0) {
        directionText = "Clockwise";
        viaText = "via";
    } else {
        directionText = destination;
        viaText = "ends at";
    }
}

// Clockwise (flash yellow)
Text.create("Direction")
    .text(directionText)
    .pos(pids.width / 2, 8)
    .centerAlign()
    .scale(0.6)
    .color((Math.floor(Date.now() / 500) % 2 === 0) ? 0xFFFF00 : 0xFFFFFF)
    .draw(ctx);

// "via" / "ends at"
Text.create("Via Ends At")
    .text(viaText)
    .pos(pids.width / 2, 18)
    .centerAlign()
    .scale(1.15)
    .color(0xFFFFFF)
    .draw(ctx);

// MRT Caplet Fill
Texture.create("Header Caplet Fill")
    .texture("jsblock:textures/block/pids/ccl_new_misc/mrt_caplet.png")
    .pos(pids.width / 2 - 15, 28)
    .size(30, 14)
    .color(arrivalsWrapper.get(0).routeColor())
    .draw(ctx);

// MRT Caplet Outline
Texture.create("Header Caplet Outline")
    .texture("jsblock:textures/block/pids/ccl_new_misc/mrt_caplet_outline.png")
    .pos(pids.width / 2 - 15, 28)
    .size(30, 14)
    .draw(ctx);

// Platform Number
Text.create("Header Platform")
    .text(platformName)
    .pos(pids.width / 2, 31)
    .size(9, 9)
    .scaleXY()
    .centerAlign()
    .color(0xFFFFFF)
    .draw(ctx);


    let START_ROW = 3;


	for (let i = 0; i < 1; i++) {


       let rowY = 50 + (i * 16);


        let customMsg = pids.getCustomMessage(i);

        let arrival = arrivalsWrapper.get(i);



        if((arrival == null || pids.isRowHidden(i))
            && customMsg === "")
            continue;






        if(customMsg != "") {



            Text.create(TextUtil.cycleString("Custom Text"))
                .text(customMsg)
                .scale(1.25)
                .size(pids.width - 10,9)
                .scaleXY()
                .pos(20,rowY)
                .scale(i===0 ? 1.25 : 1.1)
                .color(i===0 ? 0xFFFF00 : 0xFFFFFF)
                .draw(ctx);




        } else {




         let destX = 5;

// ETA (fixed at the right)
Text.create("ETA Text")
    .text(
        TextUtil.getNonCjkParts(
            PIDSUtil.getETAText(arrival.arrivalTime())
        )
    )
    .scale(1.25)
    .size(20, 8)
    .scaleXY()
    .rightAlign()
    .pos(pids.width - 6, rowY)
    .color(0xFFFFFF)
    .draw(ctx);

// Destination (centered within the space before the ETA)
Text.create("Arrival Destination")
    .text(TextUtil.cycleString(arrival.destination()))
    .pos((pids.width - 28) / 2, rowY)
    .size(pids.width - 70, 8)
    .scaleXY()
    .centerAlign()
    .scale(1.45)
    .color(0xFFFFFF)
    .draw(ctx);

        }

    }

}




function dispose(ctx, state, pids) {
}