importPackage(java.awt);
importPackage(java.awt.geom);
include(Resources.id("mtrsteamloco:scripts/display_helper.js"));

let posCfg={
    "version": 1,
    "texSize": [128, 64],
    "slots": [
        {
            "name": "left",
            "texArea": [0, 0, 128, 64],
            "pos": [[[1.155, 2.3, 0.4],[1.177, 2.1, 0.4],[1.177, 2.1, -0.4],[1.155, 2.3, -0.4]]],
            "offsets": [[0, 0, 6],[0, 0, -6]]
        },
        {
            "name": "right",
            "texArea": [0, 0, 128, 64],
            "pos": [[[-1.155, 2.3, -0.4],[-1.177, 2.1, -0.4],[-1.177, 2.1, 0.4],[-1.155, 2.3, 0.4]]],
            "offsets": [[0, 0, 6],[0, 0, -6]]
        }
    ]
};
var dhBase=new DisplayHelper(posCfg);
function create(ctx, state, train){
    state.dh=dhBase.create();
    state.pisRL=new RateLimit(0.01);
    state.stateDepot=new CycleTracker(["name", 2.5, "niu", 2.5]);
    state.stateRoute=new CycleTracker(["des", 2.5, "rn", 2.5, "ns", 2.5]);
    state.scroll=new CycleTracker(["scroll", 0.55, "wait", 1.95]);
}
function dispose(ctx, state, train){
    state.dh.close();
}
function render(ctx, state, train){
    let g=state.dh.graphicsFor("left");
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, 128, 64);
    let font=Resources.getSystemFont("Noto Sans");
    if(train.shouldRenderDetail()){
        state.scroll.tick();
        if(train.isOnRoute()&&train.getThisRoutePlatformsNextIndex()<train.getThisRouteStops().size()-1){
            state.stateRoute.tick();
            let Stas_index=train.getAllPlatformsNextIndex(),Stas=train.getStops(),Stas_TR=train.getThisRouteStops();
            let nsName,destination,routeCjk="",routeEng="";
            g.setColor(Color.WHITE);
            if(Stas_index>=0&&Stas_index<Stas.size()-1){
                destination=Stas_TR.get(Stas_TR.size()-1).station.name;
                routeCjk=TextUtil.getCjkParts(Stas_TR.get(Stas_TR.size()-1).route.name);
                routeEng=TextUtil.getNonCjkParts(Stas_TR.get(Stas_TR.size()-1).route.name);
                if(train.doorValue()==0){
                    nsName=Stas.get(Stas_index).station.name;
                }
                else if(train.doorValue()>0){
                    nsName=Stas.get(Stas_index+1).station.name;
                }
            }
            else if(Stas_index+1==Stas.size()){
                nsName="不 載 客|Not in service";
                destination="終 站|Last station";
            }
            let fontsize;
            if(TextUtil.getCjkParts(nsName).length()<=4) fontsize=18;
            else if(TextUtil.getCjkParts(nsName).length()>4&&TextUtil.getCjkParts(nsName).length()<9) fontsize=14;
            else if(TextUtil.getCjkParts(nsName).length()>=9) fontsize=10;
            if(state.stateRoute.stateNow()=="des"){
                let scroll1;
                if(state.scroll.stateNow()=="scroll"){
                    scroll1=64-Math.sin(state.scroll.stateNowDuration()*2.75)*64;
                }
                else{
                    scroll1=0;
                }
                //Last Part
                g.setColor(Color.YELLOW);
                g.setFont(font.deriveFont(fontsize));
                g.drawString("下站："+TextUtil.getCjkParts(nsName), 64-(g.getFontMetrics(font.deriveFont(fontsize)).stringWidth("下站："+TextUtil.getCjkParts(nsName))/2), scroll1-34);
                g.setFont(font.deriveFont(7));
                g.drawString("Next Sta:"+TextUtil.getNonCjkParts(nsName), 64-(g.getFontMetrics(font.deriveFont(7)).stringWidth("Next Sta:"+TextUtil.getNonCjkParts(nsName))/2), scroll1-20);
                //Now Part
                g.setColor(Color.WHITE);
                g.setFont(font.deriveFont(20));
                g.drawString(TextUtil.getCjkParts(destination), 64-(g.getFontMetrics(font.deriveFont(20)).stringWidth(TextUtil.getCjkParts(destination))/2), scroll1+30);
                g.setFont(font.deriveFont(7));
                g.drawString(TextUtil.getNonCjkParts(destination), 64-(g.getFontMetrics(font.deriveFont(7)).stringWidth(TextUtil.getNonCjkParts(destination))/2), scroll1+44);
            }
            else if(state.stateRoute.stateNow()=="rn"){
                let scroll2;
                if(state.scroll.stateNow()=="scroll"){
                    scroll2=128-Math.sin(state.scroll.stateNowDuration()*2.75)*128;
                }
                else{
                    scroll2=0;
                }
                //Last Part
                g.setFont(font.deriveFont(20));
                g.drawString(TextUtil.getCjkParts(destination), scroll2-64-(g.getFontMetrics(font.deriveFont(20)).stringWidth(TextUtil.getCjkParts(destination))/2), 30);
                g.setFont(font.deriveFont(7));
                g.drawString(TextUtil.getNonCjkParts(destination), scroll2-64-(g.getFontMetrics(font.deriveFont(7)).stringWidth(TextUtil.getNonCjkParts(destination))/2), 44);
                //Now Part
                g.setFont(font.deriveFont(20));
                g.drawString(routeCjk, scroll2-(g.getFontMetrics(font.deriveFont(20)).stringWidth(routeCjk)/2)+64, 30);
                g.setFont(font.deriveFont(15));
                g.drawString(routeEng, scroll2-(g.getFontMetrics(font.deriveFont(15)).stringWidth(routeEng)/2)+64, 44);
            }
            else if(state.stateRoute.stateNow()=="ns"){
                let scroll3;
                if(state.scroll.stateNow()=="scroll"){
                    scroll3=64-Math.sin(state.scroll.stateNowDuration()*2.75)*64;
                }
                else{
                    scroll3=0;
                }
                //Last Part
                g.setFont(font.deriveFont(20));
                g.drawString(routeCjk, 64-(g.getFontMetrics(font.deriveFont(20)).stringWidth(routeCjk)/2), scroll3-34);
                g.setFont(font.deriveFont(15));
                g.drawString(routeEng, 64-(g.getFontMetrics(font.deriveFont(15)).stringWidth(routeEng)/2), scroll3-20);
                //Now Part
                g.setColor(Color.YELLOW);
                g.setFont(font.deriveFont(fontsize));
                g.drawString("下站："+TextUtil.getCjkParts(nsName), 64-(g.getFontMetrics(font.deriveFont(fontsize)).stringWidth("下站："+TextUtil.getCjkParts(nsName))/2), scroll3+30);
                g.setFont(font.deriveFont(7));
                g.drawString("Next Sta:"+TextUtil.getNonCjkParts(nsName), 64-(g.getFontMetrics(font.deriveFont(7)).stringWidth("Next Sta:"+TextUtil.getNonCjkParts(nsName))/2), scroll3+44);
            }
        }
        else{
            state.stateDepot.tick();
            let scroll;
            if(state.stateDepot.stateNow()=="name"){
                if(state.scroll.stateNow()=="scroll"){
                    scroll=64-Math.sin(state.scroll.stateNowDuration()*2.75)*64;
                }
                else{
                    scroll=0;
                }
                g.setColor(Color.WHITE);
                g.setFont(font.deriveFont(20));
                g.drawString("不 載 客", 64-(g.getFontMetrics(font.deriveFont(20)).stringWidth("不 載 客")/2), scroll-28);
                g.drawString("多謝乘搭九鐵", 64-(g.getFontMetrics(font.deriveFont(20)).stringWidth("多謝乘搭九鐡")/2), scroll+36);
            }
            else if(state.stateDepot.stateNow()=="niu"){
                if(state.scroll.stateNow()=="scroll"){
                    scroll=64-Math.sin(state.scroll.stateNowDuration()*2.75)*64;
                }
                else{
                    scroll=0;
                }
                g.setColor(Color.WHITE);
                g.setFont(font.deriveFont(20));
                g.drawString("不 載 客", 64-(g.getFontMetrics(font.deriveFont(20)).stringWidth("不 載 客")/2), scroll+36);
                g.drawString("多謝乘搭九鐡", 64-(g.getFontMetrics(font.deriveFont(20)).stringWidth("多謝乘搭九鐡")/2), scroll-28);
            }
        }
    }
    state.dh.upload();
    for(let i=0;i<train.trainCars();i++){
        ctx.drawCarModel(state.dh.model, i, null);
    }
}