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_nel_bg.png")
        .size(pids.width, pids.height)
        .draw(ctx);




    let arrivalsWrapper = pids.arrivals();

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



    if(!hasPlatform){

        Text.create("PlatformClosed")
            .text("Platform Closed")
            .color(0xFF0000)
            .pos(5,2)
            .scale(0.9)
            .draw(ctx);

        return;

    }





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



    let firstArrival = arrivalsWrapper.get(0);


    let seconds = 999;


    if(firstArrival != null){

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

    }







    // =====================================================
    // TRAIN APPROACHING (15 - 10 SEC)
    // =====================================================


    if(seconds <= 15 && seconds > 10){


        Texture.create("Train Approaching")
            .texture("jsblock:textures/block/pids/nel_misc/train_approaching.png")
            .pos(0,0)
            .size(pids.width, pids.height)
            .draw(ctx);


        return;

    }







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


    if(seconds > 10){



        // Advertisement (3 rows)

        Texture.create("NEL Advertisement")
            .texture("jsblock:textures/block/pids/nel_misc/ad_nel.png")
            .pos(5,5)
            .size(pids.width - 10,48)
            .draw(ctx);





        let arrival = arrivalsWrapper.get(0);



        if(arrival != null){



            let rowY = pids.height - 18;



            Text.create("Next Train")
                .text("Next Train")
                .pos(5,rowY)
                .scale(0.8)
                .color(0xFFFFFF)
                .draw(ctx);




            Text.create("ETA")
                .text(
                    TextUtil.getNonCjkParts(
                        PIDSUtil.getETAText(arrival.arrivalTime())
                    )
                )
                .pos(pids.width/2,rowY)
                .scale(0.9)
                .centerAlign()
                .color(0xFFFFFF)
                .draw(ctx);





            Text.create("Destination")
                .text(TextUtil.cycleString(arrival.destination()))
                .pos(pids.width-5,rowY)
                .scale(0.7)
                .rightAlign()
                .color(0xFFFFFF)
                .draw(ctx);



        }



        return;

    }







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



    let centerX = pids.width / 2;



// Clock

Text.create("Clock")
    .text(PIDSUtil.formatTime(MinecraftClient.worldDayTime(), true))
    .pos(centerX - 38,5)
    .scale(0.75)
    .rightAlign()
    .color(0xFFFFFF)
    .draw(ctx);




// Vertical purple line

Texture.create("NEL Vertical Line")
    .texture("minecraft:textures/block/pink_concrete.png")
    .pos(centerX - 25,3)
    .size(1,12)
    .draw(ctx);





Text.create("Platform")
    .text("Platform " + platformName)
    .pos(centerX - 18,5)
    .size(60,8)
    .scaleXY()
    .scale(0.65)
    .leftAlign()
    .color(0xFFFFFF)
    .draw(ctx);






    // Divider line

    Texture.create("NEL Divider")
        .texture("minecraft:textures/block/pink_concrete.png")
        .pos(5,18)
        .size(pids.width-10,1)
        .draw(ctx);







    if(firstArrival == null)
        return;





    // Next Train

    Text.create("Next Train")
        .text("Next Train")
        .pos(centerX,35)
        .scale(1)
        .centerAlign()
        .color(0xFFFFFF)
        .draw(ctx);





    Text.create("ETA")
        .text(
            TextUtil.getNonCjkParts(
                PIDSUtil.getETAText(firstArrival.arrivalTime())
            )
        )
        .pos(centerX,48)
        .scale(1.1)
        .centerAlign()
        .color(0xFFFFFF)
        .draw(ctx);







    Text.create("Destination")
        .text(TextUtil.cycleString(firstArrival.destination()))
        .pos(centerX,62)
        .scale(1)
        .centerAlign()
        .color(0xFFFFFF)
        .draw(ctx);



}






function dispose(ctx,state,pids){

}