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

// Конфигурации для внутренних дисплеев (из 474_inside.js)
let slotHeadInsideDestCfg = {
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [0.494176, 2.57499, 8.71338],
                    [0.494176, 2.44942, 8.71338],
                    [-0.494176, 2.44942, 8.71338],
                    [-0.494176, 2.57499, 8.71338]
                ]
            ],
            offsets: [[0, 0, 0]]
        },
        {
            name: 'head-another-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-0.494176, 2.57499, -12.5921],
                    [-0.494176, 2.44942, -12.5921],
                    [0.494176, 2.44942, -12.5921],
                    [0.494176, 2.57499, -12.5921]
                ]
            ],
            offsets: [[0, 0, 0]]
        }
    ]
};

let slotMotorfInsideDestCfg = {
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [0.494176, 2.57499, 6.07492],
                    [0.494176, 2.44942, 6.07492],
                    [-0.494176, 2.44942, 6.07492],
                    [-0.494176, 2.57499, 6.07492]
                ]
            ],
            offsets: [[0, 0, 0]]
        },
        {
            name: 'head-another-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-0.494186, 2.57497, -6.26148],
                    [-0.494186, 2.44941, -6.26148],
                    [0.494176, 2.44941, -6.26148],
                    [0.494176, 2.57497, -6.26148]
                ]
            ],
            offsets: [[0, 0, 0]]
        }
    ]
};

let slotMRInsideDestCfg = {
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-0.494176, 2.57499, -8.91393],
                    [-0.494176, 2.44942, -8.91393],
                    [0.494176, 2.44942, -8.91393],
                    [0.494176, 2.57499, -8.91393]
                ]
            ],
            offsets: [[0, 0, 0]]
        },
        {
            name: 'head-another-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [0.494176, 2.57499, 12.3909],
                    [0.494176, 2.44942, 12.3909],
                    [-0.494176, 2.44942, 12.3909],
                    [-0.494176, 2.57499, 12.3909]
                ]
            ],
            offsets: [[0, 0, 0]]
        }
    ]
};

let slotMInsideDestCfg = {
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [0.494176, 2.57499, 9.85748],
                    [0.494176, 2.44942, 9.85748],
                    [-0.494176, 2.44942, 9.85748],
                    [-0.494176, 2.57499, 9.85748]
                ]
            ],
            offsets: [[0, 0, 0]]
        },
        {
            name: 'head-another-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-0.494176, 2.57499, -11.4472],
                    [-0.494176, 2.44942, -11.4472],
                    [0.494176, 2.44942, -11.4472],
                    [0.494176, 2.57499, -11.4472]
                ]
            ],
            offsets: [[0, 0, 0]]
        }
    ]
};

let slotMotorInsideDestCfg = {
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [0.494176, 2.57499, 7.19649],
                    [0.494176, 2.44942, 7.19649],
                    [-0.494176, 2.44942, 7.19649],
                    [-0.494176, 2.57499, 7.19649]
                ]
            ],
            offsets: [[0, 0, 0]]
        },
        {
            name: 'head-another-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-0.494186, 2.57497, -5.13825],
                    [-0.494186, 2.44941, -5.138258],
                    [0.494176, 2.44941, -5.13825],
                    [0.494176, 2.57497, -5.13825]
                ]
            ],
            offsets: [[0, 0, 0]]
        }
    ]
};

let slotRevInsideDestCfg = {
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [0.494176, 2.57499, 13.4809],
                    [0.494176, 2.44942, 13.4809],
                    [-0.494176, 2.44942, 13.4809],
                    [-0.494176, 2.57499, 13.4809]
                ]
            ],
            offsets: [[0, 0, 0]]
        },
        {
            name: 'head-another-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-0.494186, 2.57497, -7.82341],
                    [-0.494186, 2.44941, -7.82341],
                    [0.494176, 2.44941, -7.82341],
                    [0.494176, 2.57497, -7.82341]
                ]
            ],
            offsets: [[0, 0, 0]]
        }
    ]
};

// Конфигурации для внешних дисплеев (из 474_outside.js)
let slotHeadDestCfg = {								// ЭМУ ОСНОВНОЙ ГОЛОВНОЙ (БЕЛЫЕ ОГНИ) ВАГОН
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-0.809993, 2.53334, 10.3802],		// Левый Верхний Угол XYZ
                    [-0.809993, 2.26083, 10.4072],	// Левый Нижний Угол XYZ
                    [0.809993, 2.26083, 10.4066],		// Правый Нижний Угол XYZ
                    [0.809993, 2.53334, 10.4066],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
    ],
};

let slotHeadSideDestCfg = {								// ЭМУ ОСНОВНОЙ ГОЛОВНОЙ (БЕЛЫЕ ОГНИ) ВАГОН
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-right-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-1.45558, 2.14908, -12.4061],		// Левый Верхний Угол XYZ
                    [-1.45558, 1.95794, -12.4061],	// Левый Нижний Угол XYZ
                    [-1.45558, 1.95794, -11.165],		// Правый Нижний Угол XYZ
                    [-1.45558, 2.14908, -11.165],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
        {
            name: 'head-left-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [1.45476, 2.14908, -11.165],		// Левый Верхний Угол XYZ
                    [1.45476, 1.95794, -11.165],	// Левый Нижний Угол XYZ
                    [1.45476, 1.95794, -12.4061],		// Правый Нижний Угол XYZ
                    [1.45476, 2.14908, -12.4061],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
    ],
};

let slotHeadRouteCfg = {							// ЭМУ МАРШРУТ ГОЛОВНОЙ (БЕЛЫЕ ОГНИ) ВАГОН
    version: 1,
    texSize: [256, 128],
    slots: [
        {
            name: 'head-route',
            texArea: [0, 0, 256, 128],
            pos: [
                [
                    [0, 0, 0],		// Левый Верхний Угол XYZ
                    [0, 0, 0],		// Левый Нижний Угол XYZ
                    [0, 0, 0],	    // Правый Нижний Угол XYZ
                    [0, 0, 0],	    // Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
    ],
};

let slotTailDestCfg = {							    // ЭМУ ОСНОВНОЙ ХВОСТОВОЙ (КРАСНЫЕ ОГНИ) ВАГОН
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'tail-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [0.809993, 2.53334, -9.48866],     // Левый Верхний Угол XYZ
                    [0.809993, 2.26083, -9.51508], // Левый Нижний Угол XYZ
                    [-0.809993, 2.26083, -9.51508],      // Правый Нижний Угол XYZ
                    [-0.809993, 2.53334, -9.48866],      // Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
    ],
};

let slotTailSideDestCfg = {								// ЭМУ ОСНОВНОЙ ГОЛОВНОЙ (БЕЛЫЕ ОГНИ) ВАГОН
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'tail-right-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-1.45521, 2.14908, 12.0542],		// Левый Верхний Угол XYZ
                    [-1.45521, 1.95794, 12.0542],	// Левый Нижний Угол XYZ
                    [-1.45521, 1.95794, 13.2953],		// Правый Нижний Угол XYZ
                    [-1.45521, 2.14908, 13.2953],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
        {
            name: 'tail-left-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [1.45532, 2.14908, 13.2953],		// Левый Верхний Угол XYZ
                    [1.45532, 1.95794, 13.2953],	// Левый Нижний Угол XYZ
                    [1.45532, 1.95794, 12.0542],		// Правый Нижний Угол XYZ
                    [1.45532, 2.14908, 12.0542],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
    ],
};

let slotTailRouteCfg = {						    // ЭМУ МАРШРУТ ХВОСТОВОЙ (КРАСНЫЕ ОГНИ) ВАГОН
    version: 1,
    texSize: [256, 128],
    slots: [
        {
            name: 'tail-route',
            texArea: [0, 0, 256, 128],
            pos: [
                [
                    [0, 0, 0],     // Левый Верхний Угол XYZ
                    [0, 0, 0],     // Левый Нижний Угол XYZ
                    [0, 0, 0],     // Правый Нижний Угол XYZ
                    [0, 0, 0],     // Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
    ],
};

let slotMidTailSideDestCfg = {								// ЭМУ ОСНОВНОЙ ГОЛОВНОЙ (БЕЛЫЕ ОГНИ) ВАГОН
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'mid-tail-right-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-1.45558, 2.14908, -12.4061],		// Левый Верхний Угол XYZ
                    [-1.45558, 1.95794, -12.4061],	// Левый Нижний Угол XYZ
                    [-1.45558, 1.95794, -11.165],		// Правый Нижний Угол XYZ
                    [-1.45558, 2.14908, -11.165],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 1.15]],
        },
        {
            name: 'mid-tail-left-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [1.45476, 2.14908, -11.165],		// Левый Верхний Угол XYZ
                    [1.45476, 1.95794, -11.165],	// Левый Нижний Угол XYZ
                    [1.45476, 1.95794, -12.4061],		// Правый Нижний Угол XYZ
                    [1.45476, 2.14908, -12.4061],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 1.15]],
        },
        {
            name: 'mid-tail-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-0.809993, 2.53334, 11.524],		// Левый Верхний Угол XYZ
                    [-0.809993, 2.26083, 11.5504],	// Левый Нижний Угол XYZ
                    [0.809993, 2.26083, 11.5504],		// Правый Нижний Угол XYZ
                    [0.809993, 2.53334, 11.524],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
    ],
};

let slotMidHeadSideDestCfg = {								// ЭМУ ОСНОВНОЙ ГОЛОВНОЙ (БЕЛЫЕ ОГНИ) ВАГОН
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'mid-head-right-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-1.45545, 2.14908, 10.9636],		// Левый Верхний Угол XYZ
                    [-1.45545, 1.95794, 10.9636],	// Левый Нижний Угол XYZ
                    [-1.45545, 1.95794, 12.2046],		// Правый Нижний Угол XYZ
                    [-1.45545, 2.14908, 12.2046],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
        {
            name: 'mid-head-left-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [1.45545, 2.14908, 12.2046],		// Левый Верхний Угол XYZ
                    [1.45545, 1.95794, 12.2046],	// Левый Нижний Угол XYZ
                    [1.45545, 1.95794, 10.9636],		// Правый Нижний Угол XYZ
                    [1.45545, 2.14908, 10.9636],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
        {
            name: 'mid-head-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [0.80999, 2.53334, -10.5789],		// Левый Верхний Угол XYZ
                    [0.80999, 2.26083, -10.6054],	// Левый Нижний Угол XYZ
                    [-0.809996, 2.26083, -10.6054],		// Правый Нижний Угол XYZ
                    [-0.809996, 2.53334, -10.5789],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
    ],
};

// Создание помощников отображения для внутренних дисплеев
let dhHeadInsideBaseDest = new DisplayHelper(slotHeadInsideDestCfg);
let dhMotorfInsideBaseDest = new DisplayHelper(slotMotorfInsideDestCfg);
let dhMRInsideBaseDest = new DisplayHelper(slotMRInsideDestCfg);
let dhMInsideBaseDest = new DisplayHelper(slotMInsideDestCfg);
let dhMotorInsideDest = new DisplayHelper(slotMotorInsideDestCfg);
let dhRevInsideDest = new DisplayHelper(slotRevInsideDestCfg);

// Создание помощников отображения для внешних дисплеев
let dhHeadBaseDest = new DisplayHelper(slotHeadDestCfg);
let dhHeadSideBaseDest = new DisplayHelper(slotHeadSideDestCfg);
let dhMidTailSideDest = new DisplayHelper(slotMidTailSideDestCfg);
let dhHeadBaseRoute = new DisplayHelper(slotHeadRouteCfg);
let dhTailBaseDest = new DisplayHelper(slotTailDestCfg);
let dhTailSideBaseDest = new DisplayHelper(slotTailSideDestCfg);
let dhMidHeadSideDest = new DisplayHelper(slotMidHeadSideDestCfg);
let dhTailBaseRoute = new DisplayHelper(slotTailRouteCfg);

var EMUFont = Resources.readFont(Resources.idRelative('mtr:fronts/timessquare.ttf'));

// Функция create объединяет инициализацию из обоих скриптов
function create(ctx, state, train) {
    // Инициализация из 474_inside.js
    state.pisRateLimit = new RateLimit(0.05);
    state.pisRateLimitNextStation = new RateLimit(0.07);
    state.dhHeadInsideDest = dhHeadInsideBaseDest.create();
    state.dhMotorfInsideDest = dhMotorfInsideBaseDest.create();
    state.dhMRInsideDest = dhMRInsideBaseDest.create();
    state.dhMInsideDest = dhMInsideBaseDest.create();
    state.dhMotorInsideDest = dhMotorInsideDest.create();
    state.dhRevInsideDest = dhRevInsideDest.create();
    state.routeIDmath = Math.floor(1 + (Math.random() * 59));
    state.lastStationName = "";
    
    // Инициализация из 474_outside.js
    state.dhHeadDest = dhHeadBaseDest.create();
    state.dhHeadSideDest = dhHeadSideBaseDest.create();
    state.dhMidTailSideDest = dhMidTailSideDest.create();
    state.dhHeadRoute = dhHeadBaseRoute.create();
    state.dhTailDest = dhTailBaseDest.create();
    state.dhTailSideDest = dhTailSideBaseDest.create();
    state.dhMidHeadSideDest = dhMidHeadSideDest.create();
    state.dhTailRoute = dhTailBaseRoute.create();
    
    return (state.routeID = state.routeIDmath);
}

// Функция render объединяет функциональность из обоих скриптов
function render(ctx, state, train) {
    // Часть из 474_inside.js для внутренних дисплеев
    let platformList = train.getThisRoutePlatforms();
    if (platformList.size() === 0) return;

    let currentPlatform = platformList[0];
    if (!currentPlatform || !currentPlatform.station) return;

    let currentStationName = currentPlatform.station.name;

    if (currentStationName !== state.lastStationName) {
        state.lastStationName = currentStationName;

        let currentIndex = -1;
        for (let i = 0; i < state.stationList.size(); i++) {
            if (state.stationList[i].station && state.stationList[i].station.name === currentStationName) {
                currentIndex = i;
                break;
            }
        }

        if (currentIndex !== -1) {
            let nextIndex = (currentIndex + 1) % state.stationList.size();
            let nextStation = state.stationList[nextIndex];
            let stationName = nextStation ? nextStation.destinationName.split('\\|')[0] : 'NICHT EINSTEIGEN';
            updateDisplays(state, stationName);
        }
    }

    // Отрисовка внутренних моделей
    ctx.drawCarModel(state.dhHeadInsideDest.model, 0, null);
    ctx.drawCarModel(state.dhMotorfInsideDest.model, 1, null);
    ctx.drawCarModel(state.dhMRInsideDest.model, 2, null);
    ctx.drawCarModel(state.dhMInsideDest.model, 3, null);
    ctx.drawCarModel(state.dhMotorInsideDest.model, 4, null);
    ctx.drawCarModel(state.dhRevInsideDest.model, 5, null);
    
    if (state.pisRateLimitNextStation.shouldUpdate() && train.shouldRenderDetail()) {
        updateNextStationDisplay(state, train);
        ctx.drawCarModel(state.dhHeadInsideDest.model, 0, null);
        ctx.drawCarModel(state.dhMotorfInsideDest.model, 1, null);
        ctx.drawCarModel(state.dhMRInsideDest.model, 2, null);
        ctx.drawCarModel(state.dhMInsideDest.model, 3, null);
        ctx.drawCarModel(state.dhMotorInsideDest.model, 4, null);
        ctx.drawCarModel(state.dhRevInsideDest.model, 5, null);
    }

    // Часть из 474_outside.js для внешних дисплеев
    if (state.pisRateLimit.shouldUpdate()) {
        let g;
        let EMUText = getDestinationStation(train);

        // Обновление внешних дисплеев с названием станции
        [
            { dh: state.dhHeadDest, graphicsName: 'head-dest' },
            { dh: state.dhHeadSideDest, graphicsName: 'head-right-dest' },
            { dh: state.dhHeadSideDest, graphicsName: 'head-left-dest' },
            { dh: state.dhTailDest, graphicsName: 'tail-dest' },
            { dh: state.dhTailSideDest, graphicsName: 'tail-right-dest' },
            { dh: state.dhTailSideDest, graphicsName: 'tail-left-dest' },
            { dh: state.dhMidTailSideDest, graphicsName: 'mid-tail-right-dest' },
            { dh: state.dhMidTailSideDest, graphicsName: 'mid-tail-left-dest' },
            { dh: state.dhMidHeadSideDest, graphicsName: 'mid-head-right-dest' },
            { dh: state.dhMidHeadSideDest, graphicsName: 'mid-head-left-dest' }
        ].forEach((element) => {
            let dh = element.dh;
            g = dh.graphicsFor(element.graphicsName);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, 1024, 128);
            g.setColor(Color.ORANGE);
            g.setFont(EMUFont.deriveFont(Font.PLAIN, 140));
            let fontMetrics = g.getFontMetrics();
            let textWidth = fontMetrics.stringWidth(EMUText);
            g.drawString(EMUText, (1024 - textWidth) / 2, 115);
            dh.upload();
        });

        // Обновление внешних дисплеев с номером маршрута
        [
            { dh: state.dhHeadRoute, graphicsName: 'head-route' },
            { dh: state.dhTailRoute, graphicsName: 'tail-route' }
        ].forEach((element) => {
            let dh = element.dh;
            g = dh.graphicsFor(element.graphicsName);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, 256, 128);
            g.setColor(Color.GREEN);
            g.setFont(EMUFont.deriveFont(Font.PLAIN, 110));
            g.drawString(state.routeID, 45, 115);
            dh.upload();
        });
    }

    // Отрисовка внешних моделей
    ctx.drawCarModel(state.dhHeadDest.model, 0, null);
    ctx.drawCarModel(state.dhHeadSideDest.model, 0, null);
    ctx.drawCarModel(state.dhHeadRoute.model, 0, null);
    ctx.drawCarModel(state.dhMidTailSideDest.model, 3, null);
    ctx.drawCarModel(state.dhMidHeadSideDest.model, 2, null);
    ctx.drawCarModel(state.dhTailDest.model, train.trainCars() - 1, null);
    ctx.drawCarModel(state.dhTailSideDest.model, train.trainCars() - 1, null);
    ctx.drawCarModel(state.dhTailRoute.model, train.trainCars() - 1, null);
}

// Функции из 474_inside.js
function updateNextStationDisplay(state, train) {
    let stationName = train.getThisRoutePlatforms().size() != 0 ? getNextStation(train) : "NICHT EINSTEIGEN";

    ['head-inside-dest', 'head-another-inside-dest'].forEach(slotName => {
        updateDisplaySlot(state.dhHeadInsideDest, slotName, stationName);
        updateDisplaySlot(state.dhMotorfInsideDest, slotName, stationName);
        updateDisplaySlot(state.dhMRInsideDest, slotName, stationName);
        updateDisplaySlot(state.dhMInsideDest, slotName, stationName);
        updateDisplaySlot(state.dhMotorInsideDest, slotName, stationName);
        updateDisplaySlot(state.dhRevInsideDest, slotName, stationName);
    });
}

function updateDisplaySlot(dh, slotName, text) {
    let g = dh.graphicsFor(slotName);
    g.setComposite(AlphaComposite.Clear);
    g.fillRect(0, 0, 1024, 128);
    g.setComposite(AlphaComposite.SrcOver);
    g.setColor(Color.ORANGE);
    g.setFont(EMUFont.deriveFont(Font.PLAIN, 90));
    
    let fontMetrics = g.getFontMetrics();
    let textWidth = fontMetrics.stringWidth(text);
    g.drawString(text, (1024 - textWidth) / 2, 100);
    dh.upload();
}

function getNextStation(train) {
    let stationList = train.getThisRoutePlatforms();
    let nextstationindex = train.getThisRoutePlatformsNextIndex();
    return stationList[nextstationindex].station.name;
}

function updateDisplays(state, text) {
    updateDestinationDisplays(state, text);
    updateRouteDisplays(state);
}

// Функция dispose объединяет освобождение ресурсов из обоих скриптов
function dispose(ctx, state, train) {
    // Освобождение ресурсов из 474_inside.js
    state.dhHeadInsideDest.close();
    state.dhMotorfInsideDest.close();
    state.dhMRInsideDest.close();
    state.dhMInsideDest.close();
    state.dhMotorInsideDest.close();
    state.dhRevInsideDest.close();

    // Освобождение ресурсов из 474_outside.js
    state.dhHeadDest.close();
    state.dhHeadSideDest.close();
    state.dhMidTailSideDest.close();
    state.dhHeadRoute.close();
    state.dhTailDest.close();
    state.dhTailSideDest.close();
    state.dhTailRoute.close();
}

// Общая функция из обоих скриптов
function getDestinationStation(train) {
    let stationList = train.getThisRoutePlatforms();
    let lastPlatform = stationList[stationList.size() - 1];
    return lastPlatform === undefined ? 'NICHT EINSTEIGEN' : lastPlatform.destinationName.split('\\|')[0];
}