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

// Внешние дисплеи (из 423.js)
let slotHeadDestCfg = {								// ЭМУ ОСНОВНОЙ ГОЛОВНОЙ (БЕЛЫЕ ОГНИ) ВАГОН
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-0.941092, 2.68763, 8.07985],		// Левый Верхний Угол XYZ
                    [-0.941092, 2.38219, 8.07985],	// Левый Нижний Угол XYZ
                    [0.941091, 2.38219, 8.07985],		// Правый Нижний Угол XYZ
                    [0.941091, 2.68763, 8.07985],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
    ],
};

let slotHeadSideDestCfg = {								// ЭМУ ОСНОВНОЙ ГОЛОВНОЙ (БЕЛЫЕ ОГНИ) ВАГОН
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-right-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-1.43556, 2.04992, -3.59267],		// Левый Верхний Угол XYZ
                    [-1.43556, 1.79565, -3.59267],	// Левый Нижний Угол XYZ
                    [-1.43556, 1.79565, -2.51614],		// Правый Нижний Угол XYZ
                    [-1.43556, 2.04992, -2.51614],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
        {
            name: 'head-left-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [1.43512, 2.05035, 0.78823],		// Левый Верхний Угол XYZ
                    [1.43512, 1.79522, 0.78823],	// Левый Нижний Угол XYZ
                    [1.43512, 1.79522, -0.288302],		// Правый Нижний Угол XYZ
                    [1.43512, 2.05035, -0.288302],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
    ],
};

let slotHeadSideDest2Cfg = {								// ЭМУ ОСНОВНОЙ ГОЛОВНОЙ (БЕЛЫЕ ОГНИ) ВАГОН
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-right-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-1.43528, 2.04991, -2.91948],		// Левый Верхний Угол XYZ
                    [-1.43528, 1.79565, -2.91948],	// Левый Нижний Угол XYZ
                    [-1.43528, 1.79565, -1.79565],		// Правый Нижний Угол XYZ
                    [-1.43528, 2.04991, -1.79565],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
        {
            name: 'head-left-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [1.43602, 2.05035, 1.46446],		// Левый Верхний Угол XYZ
                    [1.43602, 1.79522, 1.46446],	// Левый Нижний Угол XYZ
                    [1.43602, 1.79522, 0.387925],		// Правый Нижний Угол XYZ
                    [1.43602, 2.05035, 0.387925],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
    ],
};

let slotHeadSideDest3Cfg = {								// ЭМУ ОСНОВНОЙ ГОЛОВНОЙ (БЕЛЫЕ ОГНИ) ВАГОН
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-right-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-1.43597, 2.04991, -2.27374],		// Левый Верхний Угол XYZ
                    [-1.43597, 1.79565, -2.27374],	// Левый Нижний Угол XYZ
                    [-1.43597, 1.79565, -1.1972],		// Правый Нижний Угол XYZ
                    [-1.43597, 2.04991, -1.1972],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
        {
            name: 'head-left-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [1.43602, 2.05035, 2.1102],		// Левый Верхний Угол XYZ
                    [1.43602, 1.79522, 2.1102],	// Левый Нижний Угол XYZ
                    [1.43602, 1.79522, 1.03366],		// Правый Нижний Угол XYZ
                    [1.43602, 2.05035, 1.03366],		// Правый Верхний Угол 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.43533, 2.05035, -1.59734],		// Левый Верхний Угол XYZ
                    [-1.43533, 1.79522, -1.59734],	// Левый Нижний Угол XYZ
                    [-1.43533, 1.79522, -0.520808],		// Правый Нижний Угол XYZ
                    [-1.43533, 2.05035, -0.520808],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
        {
            name: 'mid-tail-left-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [1.43619, 2.04992, 2.78356],		// Левый Верхний Угол XYZ
                    [1.43619, 1.79565, 2.78356],	// Левый Нижний Угол XYZ
                    [1.43619, 1.79565, 1.70703],		// Правый Нижний Угол XYZ
                    [1.43619, 2.04992, 1.70703],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
        {
            name: 'mid-tail-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [0.941089, 2.68763, -8.88965],		// Левый Верхний Угол XYZ
                    [0.941089, 2.38219, -8.88965],	// Левый Нижний Угол XYZ
                    [-0.941094, 2.38219, -8.88965],		// Правый Нижний Угол XYZ
                    [-0.941094, 2.68763, -8.88965],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
    ],
};

let slotMidTailSideDest2Cfg = {								// ЭМУ ОСНОВНОЙ ГОЛОВНОЙ (БЕЛЫЕ ОГНИ) ВАГОН
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'mid-tail-right-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-1.43612, 2.04992, -6.4227],		// Левый Верхний Угол XYZ
                    [-1.43612, 1.79565, -6.4227],	// Левый Нижний Угол XYZ
                    [-1.43612, 1.79565, -5.34617],		// Правый Нижний Угол XYZ
                    [-1.43612, 2.04992, -5.34617],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
        {
            name: 'mid-tail-left-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [1.43481, 2.05035, -2.0418],		// Левый Верхний Угол XYZ
                    [1.43481, 1.79521, -2.0418],	// Левый Нижний Угол XYZ
                    [1.43619, 1.79521, -3.11833],		// Правый Нижний Угол XYZ
                    [1.43619, 2.05035, -3.11833],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
        {
            name: 'mid-tail-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-0.941092, 2.68763, 5.25472],		// Левый Верхний Угол XYZ
                    [-0.941092, 2.38219, 5.25472],	// Левый Нижний Угол XYZ
                    [0.941091, 2.38219, 5.25472],		// Правый Нижний Угол XYZ
                    [0.941091, 2.68763, 5.25472],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
    ],
};

let slotMidTailSideDest3Cfg = {								// ЭМУ ОСНОВНОЙ ГОЛОВНОЙ (БЕЛЫЕ ОГНИ) ВАГОН
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'mid-tail-right-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-1.43554, 2.04991, -5.7418],		// Левый Верхний Угол XYZ
                    [-1.43554, 1.79565, -5.7418],	// Левый Нижний Угол XYZ
                    [-1.43554, 1.79565, -4.66527],		// Правый Нижний Угол XYZ
                    [-1.43554, 2.04991, -4.66527],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
        {
            name: 'mid-tail-left-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [1.43516, 2.05035, -1.35787],		// Левый Верхний Угол XYZ
                    [1.43516, 1.79521, -1.35787],	// Левый Нижний Угол XYZ
                    [1.43516, 1.79521, -2.4344],		// Правый Нижний Угол XYZ
                    [1.43516, 2.05035, -2.4344],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
    ],
};

let slotMidTailSideDest4Cfg = {								// ЭМУ ОСНОВНОЙ ГОЛОВНОЙ (БЕЛЫЕ ОГНИ) ВАГОН
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'mid-tail-right-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-1.43536, 2.04991, -5.10377],		// Левый Верхний Угол XYZ
                    [-1.43536, 1.79565, -5.10377],	// Левый Нижний Угол XYZ
                    [-1.43536, 1.79565, -4.02723],		// Правый Нижний Угол XYZ
                    [-1.43536, 2.04991, -4.02723],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
        {
            name: 'mid-tail-left-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [1.43519, 2.05035, -0.719831],		// Левый Верхний Угол XYZ
                    [1.43519, 1.79521, -0.719831],	// Левый Нижний Угол XYZ
                    [1.43519, 1.79521, -1.79636],		// Правый Нижний Угол XYZ
                    [1.43519, 2.05035, -1.79636],		// Правый Верхний Угол 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.941088, 2.68763, -11.7184],     // Левый Верхний Угол XYZ
                    [0.941088, 2.38219, -11.7184], // Левый Нижний Угол XYZ
                    [-0.901004, 2.38219, -11.7184],      // Правый Нижний Угол XYZ
                    [-0.901004, 2.68763, -11.7184],      // Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
    ],
};

let slotTailSideDestCfg = {								// ЭМУ ОСНОВНОЙ ГОЛОВНОЙ (БЕЛЫЕ ОГНИ) ВАГОН
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'tail-right-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-1.43534, 2.05035, -4.42729],		// Левый Верхний Угол XYZ
                    [-1.43534, 1.79522, -4.42729],	// Левый Нижний Угол XYZ
                    [-1.43534, 1.79522, -3.35076],		// Правый Нижний Угол XYZ
                    [-1.43534, 2.05035, -3.35076],		// Правый Верхний Угол XYZ
                ],
            ],
            offsets: [[0, 0, 0]],
        },
        {
            name: 'tail-left-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [1.43525, 2.04992, -0.04639],		// Левый Верхний Угол XYZ
                    [1.43525, 1.79565, -0.04639],	// Левый Нижний Угол XYZ
                    [1.43525, 1.79565, -1.12292],		// Правый Нижний Угол XYZ
                    [1.43525, 2.04992, -1.12292],		// Правый Верхний Угол 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]],
        },
    ],
};

// Внутренние дисплеи (из 423_inside.js)
let slotHeadInsideDestCfg = {
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-another-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-0.528613, 2.71228, -8.6444],
                    [-0.528613, 2.54395, -8.6444],
                    [0.528614, 2.54395, -8.6444],
                    [0.528614, 2.71228, -8.6444]
                ]
            ],
            offsets: [[0, 0, 0]]
        }
    ]
};

let slotMidInsideDestCfg = {
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [0.528614, 2.71229, 6.53924],
                    [0.528614, 2.54395, 6.53924],
                    [-0.528614, 2.54395, 6.53924],
                    [-0.528614, 2.71229, 6.53924]
                ]
            ],
            offsets: [[0, 0, 0]]
        },
        {
            name: 'head-another-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-0.528613, 2.71228, -8.00059],
                    [-0.528613, 2.54395, -8.00059],
                    [0.528614, 2.54395, -8.00059],
                    [0.528614, 2.71228, -8.00059]
                ]
            ],
            offsets: [[0, 0, 0]]
        }
    ]
};

let slotMidInsideDest2Cfg = {
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [0.528614, 2.71229, 7.18479],
                    [0.528614, 2.54395, 7.18479],
                    [-0.528614, 2.54395, 7.18479],
                    [-0.528614, 2.71229, 7.18479]
                ]
            ],
            offsets: [[0, 0, 0]]
        },
        {
            name: 'head-another-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-0.528613, 2.71228, -7.35451],
                    [-0.528613, 2.54395, -7.35451],
                    [0.528614, 2.54395, -7.35451],
                    [0.528614, 2.71228, -7.35451]
                ]
            ],
            offsets: [[0, 0, 0]]
        }
    ]
};

let slotMidTailInsideDestCfg = {
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [0.528616, 2.71228, 7.83504],
                    [0.528616, 2.54395, 7.83504],
                    [-0.528611, 2.54395, 7.83504],
                    [-0.528611, 2.71228, 7.83504]
                ]
            ],
            offsets: [[0, 0, 0]]
        }
    ]
};

let slotMidTailInsideDest2Cfg = {
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-0.528613, 2.71228, -11.4735],
                    [-0.528613, 2.54395, -11.4735],
                    [0.528614, 2.54395, -11.4735],
                    [0.528614, 2.71228, -11.4735]
                ]
            ],
            offsets: [[0, 0, 0]]
        }
    ]
};

let slotMidInsideDest3Cfg = {
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [0.528614, 2.71229, 3.71696],
                    [0.528614, 2.54395, 3.71696],
                    [-0.528614, 2.54395, 3.71696],
                    [-0.528614, 2.71229, 3.71696]
                ]
            ],
            offsets: [[0, 0, 0]]
        },
        {
            name: 'head-another-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-0.528613, 2.71228, -10.823],
                    [-0.528613, 2.54395, -10.823],
                    [0.528614, 2.54395, -10.823],
                    [0.528614, 2.71228, -10.823]
                ]
            ],
            offsets: [[0, 0, 0]]
        }
    ]
};

let slotMidInsideDest4Cfg = {
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [0.528614, 2.71229, 4.35459],
                    [0.528614, 2.54395, 4.35459],
                    [-0.528614, 2.54395, 4.35459],
                    [-0.528614, 2.71229, 4.35459]
                ]
            ],
            offsets: [[0, 0, 0]]
        },
        {
            name: 'head-another-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [-0.528613, 2.71228, -10.1843],
                    [-0.528613, 2.54395, -10.1843],
                    [0.528614, 2.54395, -10.1843],
                    [0.528614, 2.71228, -10.1843]
                ]
            ],
            offsets: [[0, 0, 0]]
        }
    ]
};

let slotMidTailInsideDest3Cfg = {
    version: 1,
    texSize: [1024, 128],
    slots: [
        {
            name: 'head-inside-dest',
            texArea: [0, 0, 1024, 128],
            pos: [
                [
                    [0.528616, 2.71228, 5.00428],
                    [0.528616, 2.54395, 5.00428],
                    [-0.528611, 2.54395, 5.00428],
                    [-0.528611, 2.71228, 5.00428]
                ]
            ],
            offsets: [[0, 0, 0]]
        }
    ]
};

// Создание помощников отображения для внешних дисплеев
let dhHeadBaseDest = new DisplayHelper(slotHeadDestCfg);
let dhHeadSideBaseDest = new DisplayHelper(slotHeadSideDestCfg);
let dhHeadSideBaseDest2 = new DisplayHelper(slotHeadSideDest2Cfg);
let dhHeadSideBaseDest3 = new DisplayHelper(slotHeadSideDest3Cfg);
let dhMidTailSideDest = new DisplayHelper(slotMidTailSideDestCfg);
let dhMidTailSideDest2 = new DisplayHelper(slotMidTailSideDest2Cfg);
let dhMidTailSideDest3 = new DisplayHelper(slotMidTailSideDest3Cfg);
let dhMidTailSideDest4 = new DisplayHelper(slotMidTailSideDest4Cfg);
let dhHeadBaseRoute = new DisplayHelper(slotHeadRouteCfg);
let dhTailBaseDest = new DisplayHelper(slotTailDestCfg);
let dhTailSideBaseDest = new DisplayHelper(slotTailSideDestCfg);
let dhTailBaseRoute = new DisplayHelper(slotTailRouteCfg);

// Создание помощников отображения для внутренних дисплеев
let dhHeadInsideBaseDest = new DisplayHelper(slotHeadInsideDestCfg);
let dhMidInsideBaseDest = new DisplayHelper(slotMidInsideDestCfg);
let dhMidInsideBaseDest2 = new DisplayHelper(slotMidInsideDest2Cfg);
let dhMidTailInsideDest = new DisplayHelper(slotMidTailInsideDestCfg);
let dhMidTailInsideDest2 = new DisplayHelper(slotMidTailInsideDest2Cfg);
let dhMidInsideBaseDest3 = new DisplayHelper(slotMidInsideDest3Cfg);
let dhMidInsideBaseDest4 = new DisplayHelper(slotMidInsideDest4Cfg);
let dhMidTailInsideDest3 = new DisplayHelper(slotMidTailInsideDest3Cfg);

// Общий шрифт
var EMUFont = Resources.readFont(Resources.idRelative('mtr:fronts/timessquare.ttf'));

function create(ctx, state, train) {
    // Инициализация внешних дисплеев
    state.pisRateLimit = new RateLimit(0.05);
    state.dhHeadDest = dhHeadBaseDest.create();
    state.dhHeadSideDest = dhHeadSideBaseDest.create();
    state.dhHeadSideDest2 = dhHeadSideBaseDest2.create();
    state.dhHeadSideDest3 = dhHeadSideBaseDest3.create();
    state.dhMidTailSideDest = dhMidTailSideDest.create();
    state.dhMidTailSideDest2 = dhMidTailSideDest2.create();
    state.dhMidTailSideDest3 = dhMidTailSideDest3.create();
    state.dhMidTailSideDest4 = dhMidTailSideDest4.create();
    state.dhHeadRoute = dhHeadBaseRoute.create();
    state.dhTailDest = dhTailBaseDest.create();
    state.dhTailSideDest = dhTailSideBaseDest.create();
    state.dhTailRoute = dhTailBaseRoute.create();
    
    // Инициализация внутренних дисплеев
    state.pisRateLimitNextStation = new RateLimit(0.07);
    state.dhHeadInsideDest = dhHeadInsideBaseDest.create();
    state.dhMidInsideDest = dhMidInsideBaseDest.create();
    state.dhMidInsideDest2 = dhMidInsideBaseDest2.create();
    state.dhMidTailInsideDest = dhMidTailInsideDest.create();
    state.dhMidTailInsideDest2 = dhMidTailInsideDest2.create();
    state.dhMidInsideDest3 = dhMidInsideBaseDest3.create();
    state.dhMidInsideDest4 = dhMidInsideBaseDest4.create();
    state.dhMidTailInsideDest3 = dhMidTailInsideDest3.create();

    // Общие параметры
    state.routeIDmath = Math.floor(1 + (Math.random() * 59));
    state.routeID = state.routeIDmath;
    state.lastStationName = "";
    state.scrollOffset = 0;
    state.fullText = "";
    state.textWidth = 0;
    
    return state.routeID;
}

function dispose(ctx, state, train) {
    // Закрытие внешних дисплеев
    state.dhHeadDest.close();
    state.dhHeadSideDest.close();
    state.dhHeadSideDest2.close();
    state.dhHeadSideDest3.close();
    state.dhMidTailSideDest.close();
    state.dhMidTailSideDest2.close();
    state.dhMidTailSideDest3.close();
    state.dhMidTailSideDest4.close();
    state.dhHeadRoute.close();
    state.dhTailDest.close();
    state.dhTailSideDest.close();
    state.dhTailRoute.close();
    
    // Закрытие внутренних дисплеев
    state.dhHeadInsideDest.close();
    state.dhMidInsideDest.close();
    state.dhMidInsideDest2.close();
    state.dhMidTailInsideDest.close();
    state.dhMidTailInsideDest2.close();
    state.dhMidInsideDest3.close();
    state.dhMidInsideDest4.close();
    state.dhMidTailInsideDest3.close();
}

function render(ctx, state, train) {
    // Обработка внешних дисплеев
    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.dhHeadSideDest2, graphicsName: 'head-right-dest' },
            { dh: state.dhHeadSideDest2, graphicsName: 'head-left-dest' },
            { dh: state.dhHeadSideDest3, graphicsName: 'head-right-dest' },
            { dh: state.dhHeadSideDest3, 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.dhMidTailSideDest2, graphicsName: 'mid-tail-right-dest' },
            { dh: state.dhMidTailSideDest2, graphicsName: 'mid-tail-left-dest' },
            { dh: state.dhMidTailSideDest3, graphicsName: 'mid-tail-right-dest' },
            { dh: state.dhMidTailSideDest3, graphicsName: 'mid-tail-left-dest' },
            { dh: state.dhMidTailSideDest4, graphicsName: 'mid-tail-right-dest' },
            { dh: state.dhMidTailSideDest4, graphicsName: 'mid-tail-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, 100);
            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.dhHeadSideDest2.model, 1, null);
    ctx.drawCarModel(state.dhHeadSideDest3.model, 2, null);
    ctx.drawCarModel(state.dhHeadRoute.model, 0, null);
    ctx.drawCarModel(state.dhMidTailSideDest.model, 3, null);
    ctx.drawCarModel(state.dhMidTailSideDest2.model, 4, null);
    ctx.drawCarModel(state.dhMidTailSideDest3.model, 5, null);
    ctx.drawCarModel(state.dhMidTailSideDest4.model, 6, 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);

    // Обработка внутренних дисплеев
    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 nextStation = getNextStation(train);
        state.fullText = "Nächste station: " + nextStation + "          ";
        state.scrollOffset -= 0;
        
        let testG = state.dhHeadInsideDest.graphicsFor('head-another-inside-dest');
        testG.setFont(EMUFont.deriveFont(Font.PLAIN, 90));
        state.textWidth = testG.getFontMetrics().stringWidth(state.fullText);
    }

    if (state.pisRateLimitNextStation.shouldUpdate() && train.shouldRenderDetail()) {
        state.scrollOffset -= 20;
        if (state.scrollOffset < -state.textWidth) {
           state.scrollOffset += state.textWidth;
        }
        updateNextStationDisplay(state, train);
    }

    // Отрисовка моделей внутренних дисплеев
    ctx.drawCarModel(state.dhHeadInsideDest.model, 0, null);
    ctx.drawCarModel(state.dhMidInsideDest.model, 1, null);
    ctx.drawCarModel(state.dhMidInsideDest2.model, 2, null);
    ctx.drawCarModel(state.dhMidTailInsideDest.model, 3, null);
    ctx.drawCarModel(state.dhMidTailInsideDest2.model, 4, null);
    ctx.drawCarModel(state.dhMidInsideDest3.model, 5, null);
    ctx.drawCarModel(state.dhMidInsideDest4.model, 6, null);
    ctx.drawCarModel(state.dhMidTailInsideDest3.model, 7, null);
}

// Функции для внутренних дисплеев
function updateNextStationDisplay(state, train) {
    ['head-inside-dest', 'head-another-inside-dest'].forEach(slotName => {
        updateDisplaySlot(state.dhMidInsideDest, slotName, state);
        updateDisplaySlot(state.dhMidInsideDest2, slotName, state);
        updateDisplaySlot(state.dhMidInsideDest3, slotName, state);
        updateDisplaySlot(state.dhMidInsideDest4, slotName, state);
    });

    ['head-inside-dest'].forEach(slotName => {
        updateDisplaySlot(state.dhMidTailInsideDest, slotName, state);
        updateDisplaySlot(state.dhMidTailInsideDest2, slotName, state);
        updateDisplaySlot(state.dhMidTailInsideDest3, slotName, state);
    });

    ['head-another-inside-dest'].forEach(slotName => {
        updateDisplaySlot(state.dhHeadInsideDest, slotName, state);
    });
}

function updateDisplaySlot(dh, slotName, state) {
    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));
    
    g.drawString(state.fullText, state.scrollOffset, 100);
    
    if (state.scrollOffset + state.textWidth < 1024) {
        g.drawString(state.fullText, state.scrollOffset + state.textWidth, 100);
    }
    
    dh.upload();
}

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

function updateDisplays(state, text) {
    state.scrollText = text + "    ";
    state.scrollPosition = 0;
    updateDestinationDisplays(state, text);
    updateRouteDisplays(state);
}

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