importPackage(java.awt);
importPackage(java.awt.geom);

include("draw_scroll.js");
// include("mtr_util.js");

var meetNTEVersionRequirement = Resources.getNTEVersionInt() >= 402;
var isMTR4 = Resources.getMTRVersion().includes("4.0");

const SCROLL_SPEED = CONFIG.scrollTextSpeed || 80;

function create(ctx, state, train) {
    state.scrollScreenFirst = null;  // 第一节车厢（车头）专用的滚动屏幕实例
    state.scrollScreenMiddle = null; // 中间车厢共用的滚动屏幕实例
    state.scrollScreenLast = null;   // 最后一节车厢（车尾）专用的滚动屏幕实例
    state.lastScrollText = "";
}

function dispose(ctx, state, train) {
    if (state.scrollScreenFirst) {
        state.scrollScreenFirst.close();
    }
    if (state.scrollScreenMiddle) {
        state.scrollScreenMiddle.close();
    }
    if (state.scrollScreenLast) {
        state.scrollScreenLast.close();
    }
}

function render(ctx, state, train) {
    if (!meetNTEVersionRequirement || isMTR4) {
        return;
    }

    let trainStatus = getTrainStatus(train);
    
    let platformInfo;
    switch (trainStatus) {
        case STATUS_NO_ROUTE:
            platformInfo = null;
            break;
        case STATUS_RETURNING_TO_DEPOT:
            platformInfo = train.getAllPlatforms().get(train.getAllPlatforms().size() - 1);
            break;
        case STATUS_WAITING_FOR_DEPARTURE:
            platformInfo = train.getAllPlatforms().get(0);
            break;
        case STATUS_CHANGING_ROUTE:
            platformInfo = train.getAllPlatforms().get(train.getAllPlatformsNextIndex());
            break;
        default:
            platformInfo = train.getThisRoutePlatforms().get(train.getThisRoutePlatformsNextIndex());
            break;
    }

    const routeInfo = getRouteInfo(train, trainStatus, platformInfo);
    
    if (routeInfo == null) {
        return;
    }

    if (!checkJsonProperty(state, "routeInfo", routeInfo)) {
        state.routeInfo = routeInfo;
        state.lastScrollText = "";
    }

    let isArrived = trainStatus == STATUS_ARRIVED;
    let nextStationIndex = trainStatus == STATUS_RETURNING_TO_DEPOT ? 1 : train.getThisRoutePlatformsNextIndex();
    
    let scrollText = buildScrollTextContent(routeInfo, nextStationIndex, trainStatus, isArrived);
    
    if (state.lastScrollText != scrollText) {
        state.lastScrollText = scrollText;
        
        if (state.scrollScreenFirst) {
            state.scrollScreenFirst.close();
        }
        if (state.scrollScreenMiddle) {
            state.scrollScreenMiddle.close();
        }
        if (state.scrollScreenLast) {
            state.scrollScreenLast.close();
        }
        
        state.scrollScreenFirst = createScrollsScreen(0.8, 1, scrollText, SCROLL_SPEED);
        state.scrollScreenMiddle = createScrollsScreen(0.8, 1, scrollText, SCROLL_SPEED);
        state.scrollScreenLast = createScrollsScreen(0.8, 1, scrollText, SCROLL_SPEED);
    }
    
    let lastCarIndex = train.trainCars() - 1;
    
    // 第一节车厢（车头）- 使用专用实例
    if (state.scrollScreenFirst) {
        state.scrollScreenFirst.update();
        
        let matricesFirstNormal = new Matrices();
        matricesFirstNormal.translate(0, 2.155, -9.38);
        
        let matricesFirstFlipped = new Matrices();
        matricesFirstFlipped.translate(0, 2.155, 7.18);
        matricesFirstFlipped.rotateY(Math.PI);
        
        ctx.drawCarModel(state.scrollScreenFirst.dynamicModelHolder, 0, matricesFirstNormal);
        ctx.drawCarModel(state.scrollScreenFirst.dynamicModelHolder, 0, matricesFirstFlipped);
    }
    
    // 中间车厢（第2节到倒数第2节）- 使用中间共用实例
    if (state.scrollScreenMiddle && lastCarIndex > 1) {
        state.scrollScreenMiddle.update();
        
        let matricesMiddleNormal = new Matrices();
        matricesMiddleNormal.translate(0, 2.155, -9.38);
        
        let matricesMiddleFlipped = new Matrices();
        matricesMiddleFlipped.translate(0, 2.155, 9.38);
        matricesMiddleFlipped.rotateY(Math.PI);
        
        for (let i = 1; i < lastCarIndex; i++) {
            ctx.drawCarModel(state.scrollScreenMiddle.dynamicModelHolder, i, matricesMiddleNormal);
            ctx.drawCarModel(state.scrollScreenMiddle.dynamicModelHolder, i, matricesMiddleFlipped);
        }
    }
    
    // 最后一节车厢（车尾）- 使用专用实例
    if (state.scrollScreenLast && lastCarIndex > 0) {
        state.scrollScreenLast.update();
        
        let matricesLastNormal = new Matrices();
        matricesLastNormal.translate(0, 2.155, -7.17);
        
        let matricesLastFlipped = new Matrices();
        matricesLastFlipped.translate(0, 2.155, 9.385);
        matricesLastFlipped.rotateY(Math.PI);
        
        ctx.drawCarModel(state.scrollScreenLast.dynamicModelHolder, lastCarIndex, matricesLastNormal);
        ctx.drawCarModel(state.scrollScreenLast.dynamicModelHolder, lastCarIndex, matricesLastFlipped);
    }
}
