importPackage(java.awt);

include("font_util.js");
include("js_util.js");
include("scrolls_screen.js");


function buildScrollTextContent(routeInfo, nextStationIndex, trainStatus, isArrived) {
    let scrollText = "";
    
    if (isArrived) {
        let currentStation = routeInfo.stationInfoList[nextStationIndex];
        let stationCjk = getMatching(currentStation.stationName, true);
        let stationNonCjk = getMatching(currentStation.stationName, false);
        let isTerminus = nextStationIndex >= routeInfo.stationInfoList.length - 1;
        
        scrollText = "本站：" + stationCjk;
        
        if (currentStation.interchangeInfo != null && currentStation.interchangeInfo.length > 0) {
            let transferCjk = "";
            for (let i = 0; i < currentStation.interchangeInfo.length; i++) {
                let interchangeRoute = currentStation.interchangeInfo[i];
                if (interchangeRoute != null) {
                    let routeNumber = TextUtil.getExtraParts(interchangeRoute.name).replaceAll(".*\\{([^}]*)}.*", "$1");
                    let routeName = (routeNumber == "" || routeNumber == TextUtil.getExtraParts(interchangeRoute.name)) ? getMatching(interchangeRoute.name, true) : routeNumber;
                    transferCjk += (i > 0 ? "、" : "") + routeName;
                }
            }
            if (transferCjk.length > 0) {
                scrollText += "，可换乘" + transferCjk;
            }
        }
        
        scrollText += " " + stationNonCjk + " station";
        
        if (currentStation.interchangeInfo != null && currentStation.interchangeInfo.length > 0) {
            let transferEn = "";
            for (let i = 0; i < currentStation.interchangeInfo.length; i++) {
                let interchangeRoute = currentStation.interchangeInfo[i];
                if (interchangeRoute != null) {
                    let routeNumber = TextUtil.getExtraParts(interchangeRoute.name).replaceAll(".*\\{([^}]*)}.*", "$1");
                    let routeNameEn = (routeNumber == "" || routeNumber == TextUtil.getExtraParts(interchangeRoute.name)) ? getMatching(interchangeRoute.name, false) : "Line " + routeNumber;
                    transferEn += (i > 0 ? ", " : "") + routeNameEn;
                }
            }
            if (transferEn.length > 0) {
                scrollText += ", Transfer to " + transferEn;
            }
        }
        
        scrollText += "。下车时请注意脚下空隙。";
        
        if (isTerminus) {
            scrollText += "欢迎再次乘坐新塘铁路列车！";
        }
    } else {
        let nextStation = routeInfo.stationInfoList[nextStationIndex];
        let terminalStation = routeInfo.destination;
        let nextStationCjk = getMatching(nextStation.stationName, true);
        let nextStationNonCjk = getMatching(nextStation.stationName, false);
        let terminalCjk = getMatching(terminalStation, true);
        let terminalNonCjk = getMatching(terminalStation, false);
        
        if (routeInfo.circularState == "CLOCKWISE") {
            terminalCjk = "内环";
            terminalNonCjk = "Inner Loop";
        } else if (routeInfo.circularState == "ANTICLOCKWISE") {
            terminalCjk = "外环";
            terminalNonCjk = "Outer Loop";
        }
        
        scrollText = "本次列车终点站：" + terminalCjk + "。下一站：" + nextStationCjk;
        
        if (nextStation.interchangeInfo != null && nextStation.interchangeInfo.length > 0) {
            let transferCjk = "";
            for (let i = 0; i < nextStation.interchangeInfo.length; i++) {
                let interchangeRoute = nextStation.interchangeInfo[i];
                if (interchangeRoute != null) {
                    let routeNumber = TextUtil.getExtraParts(interchangeRoute.name).replaceAll(".*\\{([^}]*)}.*", "$1");
                    let routeName = (routeNumber == "" || routeNumber == TextUtil.getExtraParts(interchangeRoute.name)) ? getMatching(interchangeRoute.name, true) : routeNumber;
                    transferCjk += (i > 0 ? "、" : "") + routeName;
                }
            }
            if (transferCjk.length > 0) {
                scrollText += "，可换乘" + transferCjk;
            }
        }
        
        scrollText += " This train is bound for " + terminalNonCjk + ". Next station: " + nextStationNonCjk;
        
        if (nextStation.interchangeInfo != null && nextStation.interchangeInfo.length > 0) {
            let transferEn = "";
            for (let i = 0; i < nextStation.interchangeInfo.length; i++) {
                let interchangeRoute = nextStation.interchangeInfo[i];
                if (interchangeRoute != null) {
                    let routeNumber = TextUtil.getExtraParts(interchangeRoute.name).replaceAll(".*\\{([^}]*)}.*", "$1");
                    let routeNameEn = (routeNumber == "" || routeNumber == TextUtil.getExtraParts(interchangeRoute.name)) ? getMatching(interchangeRoute.name, false) : "Line " + routeNumber;
                    transferEn += (i > 0 ? ", " : "") + routeNameEn;
                }
            }
            if (transferEn.length > 0) {
                scrollText += ", Transfer to " + transferEn;
            }
        }
        
        scrollText += "  轨道交通运营范围内严禁乞讨、卖艺、兜售、散发小广告等违规行为，请主动让座位给有需要人士。";
    }
    
    return scrollText;
}

function createScrollsScreen(width, height, scrollText, speed) {
    let fontSize = 15;
    let font = SOURCE_HAN_SANS_CN_BOLD.deriveFont(fontSize);
    let tempG = new GraphicsTexture(1, 1).graphics;
    let fm = tempG.getFontMetrics(font);
    
    let textWidth = fm.stringWidth(scrollText);
    let displayWidth = 90;
    let texHeight = 20;
    
    let texWidth = displayWidth + textWidth + 100;
    
    let info = {
        modelInfo: {
            size: { x: width, y: 0.18 },
            renderType: "light",
            uv: { x: displayWidth, y: texHeight }
        },
        graphicsInfo: { x: texWidth, y: texHeight },
        screenInfo: {
            speed: { x: speed, y: 0 },
            running: true,
            display: true
        }
    };
    
    let screen = new ScrollsScreen(info);
    let g = screen.graphics();
    
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, texWidth, texHeight);
    
    g.setFont(font);
    g.setColor(Color.RED);
    let textY = (texHeight + fm.getAscent() - fm.getDescent()) / 2;
    g.drawString(scrollText, displayWidth, textY);
    
    screen.upload();
    
    return screen;
}
