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_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(0xFFFF00)
            .pos(pids.width - 5, 2)
            .scale(0.9)
            .rightAlign()
            .draw(ctx);

        return;
    }



    let firstArrival = arrivalsWrapper.get(0);

    let firstArrivalSeconds = 999;

    if(firstArrival != null){

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

    }



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

    if(firstArrivalSeconds > 10){


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



        // Headers

        Text.create("Destination Header")
            .text("Destination")
            .pos(5,8)
            .scale(0.65)
            .color(0xFFFFFF)
            .draw(ctx);



        Text.create("Platform Header")
            .text("Platform " + platformName)
            .pos(pids.width / 2,8)
            .scale(0.7)
            .centerAlign()
            .color(0xFFFF00)
            .draw(ctx);



        Text.create("Arriving Header")
            .text("Arriving in")
            .pos(pids.width - 5,8)
            .scale(0.65)
            .rightAlign()
            .color(0xFFFFFF)
            .draw(ctx);



        // Yellow divider under header

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





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


            let arrival = arrivalsWrapper.get(i);

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



            let rowY = 28 + (i * 22);




            Text.create("Approaching Destination")
                .text(TextUtil.cycleString(arrival.destination()))
                .pos(5,rowY)
                .size(70,8)
                .scaleXY()
                .scale(0.9)
                .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 - 30,rowY)
                .rightAlign()
                .scale(0.9)
                .color(0xFFFFFF)
                .draw(ctx);



            if(eta > 0){

                Text.create("ETA Unit")
                    .text(eta > 1 ? "mins" : "min")
                    .pos(pids.width - 5,rowY)
                    .rightAlign()
                    .scale(0.75)
                    .color(0xFFFFFF)
                    .draw(ctx);

            }

        }


        return;

    }





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


    let START_ROW = 2;
    let MAX_ROWS = pids.rows - 1;


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


        if (i >= 2) continue;


        let rowY = HEADER_HEIGHT + ((i + START_ROW) * 16.75);

        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;



            if(!pids.isPlatformNumberHidden()){


                if(i === 0){


                    const centerX = pids.width / 2;

                    // enlarged circle
                    const circleSize = 16;
                    const numberHeight = 11;

                    const topY = rowY - 18;




                    Texture.create("Platform Circle Top Fill")
                        .texture("jsblock:textures/block/pids/ccl_misc/mrt_circle.png")
                        .pos(centerX-circleSize/2,topY)
                        .size(circleSize,circleSize)
                        .color(arrival.routeColor())
                        .draw(ctx);



                    Texture.create("Platform Circle Top Outline")
                        .texture("jsblock:textures/block/pids/ccl_misc/mrt_circle_outline.png")
                        .pos(centerX-circleSize/2,topY)
                        .size(circleSize,circleSize)
                        .draw(ctx);




                    Text.create("Platform Number Top")
                        .text(arrival.platformName())
                        .pos(centerX,topY+circleSize/2-numberHeight/2)
                        .size(numberHeight,numberHeight)
                        .scaleXY()
                        .centerAlign()
                        .color(0xFFFFFF)
                        .draw(ctx);


                    destX = 20;



                } else {



                    Texture.create("Platform Circle Fill")
                        .texture("jsblock:textures/block/pids/ccl_misc/mrt_circle.png")
                        .pos(5,rowY-1)
                        .size(10.5,10.5)
                        .color(arrival.routeColor())
                        .draw(ctx);



                    Texture.create("Platform Circle Outline")
                        .texture("jsblock:textures/block/pids/ccl_misc/mrt_circle_outline.png")
                        .pos(5,rowY-1)
                        .size(10.5,10.5)
                        .draw(ctx);



                    Text.create("Platform Circle Text")
                        .text(arrival.platformName())
                        .pos(10,rowY+1)
                        .size(9,9)
                        .scaleXY()
                        .scale(0.9)
                        .centerAlign()
                        .color(0xFFFFFF)
                        .draw(ctx);


                    destX = 20;

                }

            }





            let destinationColor =
                (i === 0)
                    ? (
                        firstArrivalSeconds <= 10
                            ? (
                                Math.floor(Date.now()/500)%2===0
                                    ? 0xFFFF00
                                    : 0xFFFFFF
                              )
                            : 0xFFFFFF
                      )
                    : 0xFFFFFF;





            Text.create("Arrival Destination")
                .text(TextUtil.cycleString(arrival.destination()))
                .pos(destX,rowY)
                .size(i===0?60:50,i===0?9:8)
                .scaleXY()
                .scale(i===0?1.25:1.1)
                .color(destinationColor)
                .draw(ctx);





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

        }
    }
}



function dispose(ctx,state,pids){
}