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

// assyukuDraw修正版（小さいバッファ対応）
function assyukuDraw(g, text, textSize, drawPos, targetWidth, font, textColor, offsetX) {
    let fm = g.getFontMetrics(font.deriveFont(Font.PLAIN, textSize));

    if (fm.stringWidth(text) > targetWidth) {
        let nextStaScale = targetWidth / fm.stringWidth(text);
        // ★スケール適用フォント
        g.setFont(font.deriveFont(AffineTransform.getScaleInstance(nextStaScale, 1.0)).deriveFont(Font.PLAIN, textSize));
        // ★スケール後の実際の幅で中央揃え！
        let scaledWidth = fm.stringWidth(text) * nextStaScale;
        let startX = offsetX + ((targetWidth - scaledWidth) / 2);
        g.setColor(textColor);
        g.drawString(text, startX, drawPos);
    } else {
        // ★スケール不要時も中央揃え
        g.setFont(font.deriveFont(Font.PLAIN, textSize));
        g.setColor(textColor);
        let currentWidth = fm.stringWidth(text);
        let startX = offsetX + ((targetWidth - currentWidth) / 2);
        g.drawString(text, startX, drawPos);
    }
}

function ikisakiMain(g, train, state, cars) {
    let text_g = state.text_g;

    // ★変数（3倍小さいサイズ）
    let small_width = 128, small_height = 32;
    let display_width = 384, display_height = 96;
    let font = Resources.readFont(Resources.id("mtr:font/notosansjp.ttf"));
    let font_bold = Resources.readFont(Resources.id("mtr:font/notosansjp_bold.ttf"));
    let next = train.getThisRoutePlatformsNextIndex();
    let platformList = train.getThisRoutePlatforms();
    let nextSta = (next + 1 < platformList.size()) ? platformList[next + 1].station.name : "";
    let nextStaJpn = TextUtil.getCjkParts(nextSta);
    let nextStaEng = TextUtil.getNonCjkParts(nextSta);
    let finalSta = platformList[next].destinationName;
    let finalStaJpn = TextUtil.getCjkParts(finalSta);
    let finalStaEng = TextUtil.getNonCjkParts(finalSta);
    let routeType = platformList[next].route.lightRailRouteNumber;
    let routeTypeJpn = TextUtil.getCjkParts(routeType);
    let routeColor = new Color(platformList[next].route.color);
    let routeTypeColor;
    let backGround = new Color(0.2, 0.2, 0.2);

    // ★1. 小さいバッファクリア（120x40）
    text_g.setColor(backGround);
    text_g.fillRect(0, 0, small_width, small_height);

    if (train.isOnRoute()) {
        // ★種別（座標・サイズを1/4に自動調整）
        if (routeTypeJpn == "快速") {
            routeTypeColor = Color.red;
            text_g.setColor(routeTypeColor);
            text_g.fillRect(0, 0, small_width / 8 * 3, small_height);
            assyukuDraw(text_g, "快 速", 20, 20, small_width / 8 * 3, font, Color.white, 0);
            assyukuDraw(text_g, "Rapid", 12, 31, small_width / 8 * 3, font, Color.white, 0);
        } else if (routeTypeJpn == "区間快速") {
            routeTypeColor = Color.blue;
            text_g.setColor(routeTypeColor);
            text_g.fillRect(0, 0, small_width / 8 * 3, small_height);
            text_g.setColor(Color.white);
            text_g.setFont(font.deriveFont(Font.PLAIN, 12));
            text_g.drawString("区 快", 1, 12);
            text_g.setFont(font.deriveFont(Font.PLAIN, 6));
            text_g.drawString("Semi", 1, 18);
            text_g.drawString("Rapid", 21, 24);
        } else if (routeTypeJpn == "普通" || routeTypeJpn == "各停" || routeTypeJpn == "各駅停車") {
            routeTypeColor = Color.lightGray;
            text_g.setColor(routeTypeColor);
            text_g.fillRect(0, 0, small_width / 8 * 3, small_height);
            assyukuDraw(text_g, "普通", 20, 20, small_width / 8 * 3, font, Color.white, 0);
            assyukuDraw(text_g, "Local", 12, 31, small_width / 8 * 3, font, Color.white, 0);
        } else if (routeTypeJpn == "通勤快速") {
            routeTypeColor = Color.orange;
            text_g.setColor(routeTypeColor);
            text_g.fillRect(0, 0, small_width / 8 * 3, small_height);
            text_g.setColor(Color.white);
            text_g.setFont(font.deriveFont(Font.PLAIN, 12));
            text_g.drawString("通快", 1, 12);
            text_g.setFont(font.deriveFont(Font.PLAIN, 6));
            text_g.drawString("Com.", 1, 18);
            text_g.drawString("Rapid", 21, 24);
        } else {
            routeTypeColor = routeColor;
            text_g.setColor(routeTypeColor);
            text_g.fillRect(0, 0, small_width / 8 * 3, small_height);
        }

        if ((train.speed() < 0.27777) && (next + 1 != platformList.size())) {
            if (Date.now() % 9000 < 3000) {
                assyukuDraw(text_g, finalStaJpn + " 行", 20, 20, small_width / 8 * 5, font, Color.white, small_width / 8 * 3);  // 85→22
                assyukuDraw(text_g, "次は " + nextStaJpn, 12, 31, small_width / 8 * 5, font, Color.yellow, small_width / 8 * 3); // 50→12
            } else if (Date.now() % 9000 < 6000 && Date.now() % 9000 >= 3000) {
                assyukuDraw(text_g, "For " + finalStaEng, 20, 20, small_width / 8 * 5, font, Color.white, small_width / 8 * 3);  // 65→16
                assyukuDraw(text_g, "Next " + nextStaEng, 12, 31, small_width / 8 * 5, font, Color.yellow, small_width / 8 * 3); // 40→10
            } else {
                text_g.setColor(backGround);
                text_g.fillRect(0, 0, small_width, small_height);
                text_g.setColor(Color.white);
                text_g.setFont(font.deriveFont(Font.PLAIN, 32));  // 100→25
                let carText = cars + "号車";
                text_g.drawString(carText, (small_width - text_g.getFontMetrics(font.deriveFont(Font.PLAIN, 32)).stringWidth(carText)) / 2, 31);
            }
        } else {
            assyukuDraw(text_g, finalStaJpn, 20, 20, small_width / 8 * 5, font, Color.white, small_width / 8 * 3);  // 90→22
            assyukuDraw(text_g, finalStaEng, 12, 31, small_width / 8 * 5, font, Color.white, small_width / 8 * 3); // 45→11
        }
    } else {
        let font_nis = Resources.readFont(Resources.id("mtr:font/notosansjp.ttf"));
        let nisW = text_g.getFontMetrics(font_nis.deriveFont(Font.PLAIN, 20)).stringWidth("回　 送");  // 100→25
        let nisEngW = text_g.getFontMetrics(font_nis.deriveFont(Font.PLAIN, 12)).stringWidth("Not In Service");  // 50→12
        text_g.setColor(Color.white);
        text_g.setFont(font_nis.deriveFont(Font.PLAIN, 20));
        text_g.drawString("回　 送", (small_width / 2) - (nisW / 2), 20);  // 100→25
        text_g.setColor(Color.yellow);
        text_g.setFont(font_nis.deriveFont(Font.PLAIN, 12));
        text_g.drawString("Not In Service", (small_width / 2) - (nisEngW / 2), 31);  // 160→40
    }
    // ★4倍拡大＋網掛け（最初のコードと完全同一！）
    let ledImage = Resources.readBufferedImage(Resources.id("mtr:new_straight_lcd/images/led.png"));
    g.drawImage(state.text_image, 0, 0, display_width, display_height, 0, 0, small_width, small_height, null);
    g.drawImage(ledImage, 0, 0, display_width + 1, display_height + 1, null);
}

function ikisakiFront(g, train, state) {
    let text_g = state.text_front_g;

    // ★前面用も同じ処理（変数省略版）
    let small_width = 128, small_height = 32;
    let display_width = 384, display_height = 96;
    let font = Resources.readFont(Resources.id("mtr:font/notosansjp.ttf"));
    let font_bold = Resources.readFont(Resources.id("mtr:font/notosansjp_bold.ttf"));
    let next = train.getThisRoutePlatformsNextIndex();
    let platformList = train.getThisRoutePlatforms();
    let finalSta = platformList[next].destinationName;
    let finalStaJpn = TextUtil.getCjkParts(finalSta);
    let finalStaEng = TextUtil.getNonCjkParts(finalSta);
    let routeType = platformList[next].route.lightRailRouteNumber;
    let routeTypeJpn = TextUtil.getCjkParts(routeType);
    let routeColor = new Color(platformList[next].route.color);
    let routeTypeColor;
    let backGround = new Color(0.2, 0.2, 0.2);

    text_g.setColor(backGround);
    text_g.fillRect(0, 0, small_width, small_height);

    if (train.isOnRoute()) {
        // ★種別（ikisakiMainと同じ）
        if (routeTypeJpn == "快速") {
            routeTypeColor = Color.red;
            text_g.setColor(routeTypeColor);
            text_g.fillRect(0, 0, small_width / 8 * 3, small_height);
            assyukuDraw(text_g, "快 速", 20, 20, small_width / 8 * 3, font, Color.white, 0);
            assyukuDraw(text_g, "Rapid", 12, 31, small_width / 8 * 3, font, Color.white, 0);
        } else if (routeTypeJpn == "区間快速") {
            routeTypeColor = Color.blue;
            text_g.setColor(routeTypeColor);
            text_g.fillRect(0, 0, small_width / 8 * 3, small_height);
            text_g.setColor(Color.white);
            text_g.setFont(font.deriveFont(Font.PLAIN, 12));
            text_g.drawString("区 快", 1, 12);
            text_g.setFont(font.deriveFont(Font.PLAIN, 6));
            text_g.drawString("Semi", 1, 18);
            text_g.drawString("Rapid", 21, 24);
        } else if (routeTypeJpn == "普通" || routeTypeJpn == "各停" || routeTypeJpn == "各駅停車") {
            routeTypeColor = Color.lightGray;
            text_g.setColor(routeTypeColor);
            text_g.fillRect(0, 0, small_width / 8 * 3, small_height);
            assyukuDraw(text_g, "普通", 20, 20, small_width / 8 * 3, font, Color.white, 0);
            assyukuDraw(text_g, "Local", 12, 31, small_width / 8 * 3, font, Color.white, 0);
        } else if (routeTypeJpn == "通勤快速") {
            routeTypeColor = Color.orange;
            text_g.setColor(routeTypeColor);
            text_g.fillRect(0, 0, small_width / 8 * 3, small_height);
            text_g.setColor(Color.white);
            text_g.setFont(font.deriveFont(Font.PLAIN, 12));
            text_g.drawString("通快", 1, 12);
            text_g.setFont(font.deriveFont(Font.PLAIN, 6));
            text_g.drawString("Com.", 1, 18);
            text_g.drawString("Rapid", 21, 24);
        } else {
            routeTypeColor = routeColor;
            text_g.setColor(routeTypeColor);
            text_g.fillRect(0, 0, small_width / 8 * 3, small_height);
        }
        assyukuDraw(text_g, finalStaJpn, 20, 20, small_width / 8 * 5, font, Color.white, small_width / 8 * 3);
        assyukuDraw(text_g, finalStaEng, 12, 31, small_width / 8 * 5, font, Color.white, small_width / 8 * 3);
    } else {
        // 回送表示（ikisakiMainと同じ）
        let font_nis = Resources.readFont(Resources.id("mtr:font/notosansjp.ttf"));
        let nisW = text_g.getFontMetrics(font_nis.deriveFont(Font.PLAIN, 20)).stringWidth("回　 送");
        let nisEngW = text_g.getFontMetrics(font_nis.deriveFont(Font.PLAIN, 12)).stringWidth("Not In Service");
        text_g.setColor(Color.white);
        text_g.setFont(font_nis.deriveFont(Font.PLAIN, 20));
        text_g.drawString("回　 送", (small_width / 2) - (nisW / 2), 20);
        text_g.setColor(Color.yellow);
        text_g.setFont(font_nis.deriveFont(Font.PLAIN, 12));
        text_g.drawString("Not In Service", (small_width / 2) - (nisEngW / 2), 31);
    }

    let ledImageFront = Resources.readBufferedImage(Resources.id("mtr:new_straight_lcd/images/led.png"));
    g.drawImage(state.text_front_image, 0, 0, display_width, display_height, 0, 0, small_width, small_height, null);
    g.drawImage(ledImageFront, 0, 0, display_width + 1, display_height + 1, null);
}
