import { world, system } from "@minecraft/server";
import { ActionFormData } from "@minecraft/server-ui";
import { startCameraSystem, stopCameraSystem } from "./CameraSystem.js";
import { getMainAmmo, getSubAmmo, getFlareAmmo } from "./AmmoSystem.js";
import { showAPVRocketRadarUI } from "./TTAVRadar/APVRocketRadar.js";
import { showIFVRocketRadarUI } from "./TTAVRadar/IFVRocketRadar.js";
import { showAPVRocketMainGunUI, showIFVRocketMainGunUI } from "./APVRocketMainGunUI.js";
import { VEHICLES } from "./VehiclesRoot.js";
import { showTankSettingsUI } from "./TankSettingsUI.js";
const dimensions = [
    world.getDimension("minecraft:overworld"),
    world.getDimension("minecraft:nether"),
    world.getDimension("minecraft:the_end"),
];

export function showAPVRocketControlUI(player) {
    const tankTag = Array.from(player.getTags()).find(tag => tag.startsWith('tank_'));
    if (!tankTag) return;

    const tankId = tankTag.split('_')[1];
    let tank = null;

    for (const dimension of dimensions) {
        const vehicles = dimension.getEntities();
        for (const vehicle of vehicles) {
            if (vehicle.id === tankId) {
                tank = vehicle;
                break;
            }
        }
        if (tank) break;
    }
    if (!tank) return;

    const mainAmmo = getMainAmmo(tank);
    const subAmmo = getSubAmmo(tank);
    const flareAmmo = getFlareAmmo(tank);
    const fuel = tank.getDynamicProperty("fuel_value") || 0;
    const engineStatus = tank.getDynamicProperty("ttav:engine") ? "§aON" : "§cOFF";
    const lightStatus = tank.getDynamicProperty("ttav:vehicle_light") ? "§aON" : "§cOFF";
    const location = tank.location;

    const statusText = 
        `§6§l位置§r\n` +
        `§e${Math.floor(location.x)}, ${Math.floor(location.y)}, ${Math.floor(location.z)}\n\n` +
        `§6§l火炮状态§r\n` +
        `§6引擎: \n${engineStatus}\n` +
        `§6灯: \n${lightStatus}\n` +
        `§6燃料: §e${Math.floor((fuel / 10000) * 100)}%\n` +
        `§6§l固定: §r\n${tank.hasTag("stationary_mode") ? "§aON" : "§cOFF"}\n` +
        `\n§6§l弹药§r\n` +
        `§6主炮: \n§e${mainAmmo}\n` +
        `§6副炮: \n§e${subAmmo}\n` +
        `§6信号弹: \n§e${flareAmmo}`;

    const form = new ActionFormData()
        .title("APV火箭控制")
        .body(statusText)
        .button(
            tank.getDynamicProperty("ttav:engine") ?
            "引擎: ON\n§7点击关闭" :
            "引擎: OFF\n§7点击打开",
            tank.getDynamicProperty("ttav:engine") ?
            "textures/ui/ttav_buttons/main_power_button_on" :
            "textures/ui/ttav_buttons/main_power_button_off"
        )
        .button(
            tank.getDynamicProperty("ttav:vehicle_light") ?
            "引擎: ON\n§7点击关闭" :
            "引擎: OFF\n§7点击打开",
            tank.getDynamicProperty("ttav:vehicle_light") ?
            "textures/ui/ttav_buttons/toggle_on_button" :
            "textures/ui/ttav_buttons/toggle_off_button"
        )
        .button("喇叭", "textures/ui/ttav_buttons/normal_button")
        .button("设置", "textures/ui/ttav_buttons/normal_button")
        .button("返回", "textures/ui/ttav_buttons/back_button")
        .button("主炮模式", "textures/ui/ttav_buttons/main_gun_button")
        .button("副炮模式", "textures/ui/ttav_buttons/sub_gun_button")
        .button("信号弹模式", "textures/ui/ttav_buttons/flare_gun_button")
        .button(
            tank.hasTag("stationary_mode") ? 
            "固定\n§a激活" : 
            "固定", 
            tank.hasTag("stationary_mode") ? 
            "textures/ui/ttav_buttons/toggle_on_button" :
            "textures/ui/ttav_buttons/toggle_off_button"
        )
        .button("雷达", "textures/ui/ttav_buttons/normal_button");

    form.show(player).then(response => {
        if (response.canceled) return;

        if (response.selection === 3) {
            showTankSettingsUI(player);
            return;
        }
        if (response.selection === 4) return;

        if (response.selection === 0) {
            const currentState = tank.getDynamicProperty("ttav:engine");
            if (!currentState) {
                const currentFuel = tank.getDynamicProperty("fuel_value") || 0;
                if (currentFuel > 0) {
                    system.runTimeout(() => {
                        tank.triggerEvent("engine_on");
                        tank.setDynamicProperty("ttav:engine", true);
                    }, 28);
                    startCameraSystem();
                    startEngineCameraHud(player);
                    player.playSound("ttavBeepButton", { pitch: 1.0, volume: 1.0 });
                    player.playSound("ttavTankEngineStart", { pitch: 1.0, volume: 1.0 });
                } else {
                    player.sendMessage("§c没有燃料！无法启动引擎.");
                }
            } else {
                tank.triggerEvent("engine_off");
                tank.setDynamicProperty("ttav:engine", false);
                tank.removeTag("stationary_mode");
                stopCameraSystem();
                player.runCommand(`camera @s clear`);
                player.onScreenDisplay.setTitle("tank_clear_cam");
                player.playSound("ttavBeepButton", { pitch: 1.0, volume: 1.0 });
                player.playSound("ttavTankEngineStop", { pitch: 1.0, volume: 1.0 });
                const vehicleConfig = Object.values(VEHICLES).find(v => v.identifier === tank.typeId);
                if (vehicleConfig) {
                    player.runCommand(`clear @s ${vehicleConfig.mainGunItem}`);
                    player.runCommand(`clear @s ${vehicleConfig.subGunItem}`);
                    player.runCommand(`clear @s ${vehicleConfig.flareGunItem}`);
                }    
            }
        }
        if (response.selection === 1) {
            const lightState = tank.getDynamicProperty("ttav:vehicle_light");
            if (!lightState) {
                tank.triggerEvent("vehicle_light_on");
                tank.setDynamicProperty("ttav:vehicle_light", true);
                player.playSound("ttavButton1", { pitch: 1.0, volume: 1.0 });
            } else {
                tank.triggerEvent("vehicle_light_off");
                tank.setDynamicProperty("ttav:vehicle_light", false);
                player.playSound("ttavButton1", { pitch: 1.0, volume: 1.0 });
            }
        }
        if (response.selection === 2) {
            player.runCommand("playsound pnt.v_horn @a[r=30]");
            showAPVRocketControlUI(player);
        }
        if (response.selection === 5) {
            const engineState = tank.getDynamicProperty("ttav:engine");
            const vehicleConfig = Object.values(VEHICLES).find(v => v.identifier === tank.typeId);
            if (engineState) {
                showAPVRocketMainGunUI(player);
                tank.addTag("stationary_mode");
                tank.triggerEvent("start_stationary");
                player.playSound("ttavButton2", { pitch: 1.0, volume: 1.0 });
                player.playSound("ttavStationary", { pitch: 1.0, volume: 1.0 });
                player.runCommand(`clear @s ${vehicleConfig.subGunItem}`);
                player.runCommand(`clear @s ${vehicleConfig.flareGunItem}`);
            } else {
                player.onScreenDisplay.setActionBar("§c发动机必须运转才能切换武器!");
                player.playSound("note.bass", { pitch: 1.0, volume: 1.0 });
            }
        }
        
        if (response.selection === 6) {
            const engineState = tank.getDynamicProperty("ttav:engine");
            if (engineState) {
                const vehicleConfig = Object.values(VEHICLES).find(v => v.identifier === tank.typeId);
                if (vehicleConfig) {
                    tank.triggerEvent("stop_stationary");
                    tank.removeTag("stationary_mode");
                    player.playSound("ttavButton2", { pitch: 1.0, volume: 1.0 });
                    player.runCommand(`replaceitem entity @s slot.hotbar 1 ${vehicleConfig.subGunItem} 1 0 {"item_lock":{"mode":"lock_in_slot"},"keep_on_death":{}}`);
                }
            } else {
                player.onScreenDisplay.setActionBar("§c发动机必须运转才能切换武器!");
                player.playSound("note.bass", { pitch: 1.0, volume: 1.0 });
            }
        }
        
        if (response.selection === 7) {
            const engineState = tank.getDynamicProperty("ttav:engine");
            if (engineState) {
                const vehicleConfig = Object.values(VEHICLES).find(v => v.identifier === tank.typeId);
                if (vehicleConfig) {
                    tank.triggerEvent("stop_stationary");
                    tank.removeTag("stationary_mode");
                    player.playSound("ttavButton2", { pitch: 1.0, volume: 1.0 });
                    player.runCommand(`replaceitem entity @s slot.hotbar 1 ${vehicleConfig.flareGunItem} 1 0 {"item_lock":{"mode":"lock_in_slot"},"keep_on_death":{}}`);
                }
            } else {
                player.onScreenDisplay.setActionBar("§c发动机必须运转才能切换武器!");
                player.playSound("note.bass", { pitch: 1.0, volume: 1.0 });
            }
        }
        
        if (response.selection === 8) {
            const engineState = tank.getDynamicProperty("ttav:engine");
            if (engineState) {
                if (tank.hasTag("stationary_mode")) {
                    tank.triggerEvent("stop_stationary");
                    tank.removeTag("stationary_mode");
                    player.playSound("ttavButton1", { pitch: 1.0, volume: 1.0 });
                    player.playSound("ttavStationary", { pitch: 1.0, volume: 1.0 });
                } else {
                    tank.triggerEvent("start_stationary");
                    tank.addTag("stationary_mode");
                    player.playSound("ttavButton1", { pitch: 1.0, volume: 1.0 });
                    player.playSound("ttavStationary", { pitch: 1.0, volume: 1.0 });
                }
                showAPVRocketControlUI(player);
            } else {
                player.onScreenDisplay.setActionBar("§c发动机必须运转才能切换固定模式!");
                player.playSound("note.bass", { pitch: 1.0, volume: 1.0 });
                showAPVRocketControlUI(player);
            }
            return;
        }
        
        if (response.selection === 9) {
            showAPVRocketRadarUI(player, tank);
            player.playSound("ttavButton1", { pitch: 1.0, volume: 1.0 });
            return;
        }
    });
}
export function showMLRSControlUI(player) {
    const tankTag = Array.from(player.getTags()).find(tag => tag.startsWith('tank_'));
    if (!tankTag) return;

    const tankId = tankTag.split('_')[1];
    let tank = null;

    for (const dimension of dimensions) {
        const vehicles = dimension.getEntities();
        for (const vehicle of vehicles) {
            if (vehicle.id === tankId) {
                tank = vehicle;
                break;
            }
        }
        if (tank) break;
    }
    if (!tank) return;

    const mainAmmo = getMainAmmo(tank);
    const subAmmo = getSubAmmo(tank);
    const flareAmmo = getFlareAmmo(tank);
    const fuel = tank.getDynamicProperty("fuel_value") || 0;
    const engineStatus = tank.getDynamicProperty("ttav:engine") ? "§aON" : "§cOFF";
    const lightStatus = tank.getDynamicProperty("ttav:vehicle_light") ? "§aON" : "§cOFF";
    const location = tank.location;

    const statusText = 
        `§6§l位置§r\n` +
        `§e${Math.floor(location.x)}, ${Math.floor(location.y)}, ${Math.floor(location.z)}\n\n` +
        `§6§l火炮状态§r\n` +
        `§6引擎: \n${engineStatus}\n` +
        `§6灯: \n${lightStatus}\n` +
        `§6燃料: §e${Math.floor((fuel / 10000) * 100)}%\n` +
        `§6§l固定: §r\n${tank.hasTag("stationary_mode") ? "§aON" : "§cOFF"}\n` +
        `\n§6§l弹药§r\n` +
        `§6主炮: \n§e${mainAmmo}\n` +
        `§6副炮: \n§e${subAmmo}\n` +
        `§6信号弹: \n§e${flareAmmo}`;

    const form = new ActionFormData()
        .title("APV火箭控制")
        .body(statusText)
        .button(
            tank.getDynamicProperty("ttav:engine") ?
            "引擎: ON\n§7点击关闭" :
            "引擎: OFF\n§7点击打开",
            tank.getDynamicProperty("ttav:engine") ?
            "textures/ui/ttav_buttons/main_power_button_on" :
            "textures/ui/ttav_buttons/main_power_button_off"
        )
        .button(
            tank.getDynamicProperty("ttav:vehicle_light") ?
            "引擎: ON\n§7点击关闭" :
            "引擎: OFF\n§7点击打开",
            tank.getDynamicProperty("ttav:vehicle_light") ?
            "textures/ui/ttav_buttons/toggle_on_button" :
            "textures/ui/ttav_buttons/toggle_off_button"
        )
        .button("喇叭", "textures/ui/ttav_buttons/normal_button")
        .button("设置", "textures/ui/ttav_buttons/normal_button")
        .button("返回", "textures/ui/ttav_buttons/back_button")
        .button("主炮模式", "textures/ui/ttav_buttons/main_gun_button")
        .button("副炮模式", "textures/ui/ttav_buttons/sub_gun_button")
        .button("信号弹模式", "textures/ui/ttav_buttons/flare_gun_button")
        .button(
            tank.hasTag("stationary_mode") ? 
            "固定\n§a激活" : 
            "固定", 
            tank.hasTag("stationary_mode") ? 
            "textures/ui/ttav_buttons/toggle_on_button" :
            "textures/ui/ttav_buttons/toggle_off_button"
        )
        .button("雷达", "textures/ui/ttav_buttons/normal_button");

    form.show(player).then(response => {
        if (response.canceled) return;

        if (response.selection === 3) {
            showTankSettingsUI(player);
            player.playSound("ttavButton1", { pitch: 1.0, volume: 1.0 });
            return;
        }
        if (response.selection === 4) return;

        if (response.selection === 0) {
            const currentState = tank.getDynamicProperty("ttav:engine");
            if (!currentState) {
                const currentFuel = tank.getDynamicProperty("fuel_value") || 0;
                if (currentFuel > 0) {
                    system.runTimeout(() => {
                        tank.triggerEvent("engine_on");
                        tank.setDynamicProperty("ttav:engine", true);
                    }, 28);
                    startCameraSystem();
                    startEngineCameraHud(player);
                    player.playSound("ttavBeepButton", { pitch: 1.0, volume: 1.0 });
                    player.playSound("ttavTankEngineStart", { pitch: 1.0, volume: 1.0 });
                } else {
                    player.sendMessage("§c没有燃料！无法启动引擎.");
                }
            } else {
                tank.triggerEvent("engine_off");
                tank.setDynamicProperty("ttav:engine", false);
                tank.removeTag("stationary_mode");
                stopCameraSystem();
                player.runCommand(`camera @s clear`);
                player.onScreenDisplay.setTitle("tank_clear_cam");
                player.playSound("ttavBeepButton", { pitch: 1.0, volume: 1.0 });
                player.playSound("ttavTankEngineStop", { pitch: 1.0, volume: 1.0 });
                const vehicleConfig = Object.values(VEHICLES).find(v => v.identifier === tank.typeId);
                if (vehicleConfig) {
                    player.runCommand(`clear @s ${vehicleConfig.mainGunItem}`);
                    player.runCommand(`clear @s ${vehicleConfig.subGunItem}`);
                    player.runCommand(`clear @s ${vehicleConfig.flareGunItem}`);
                }    
            }
        }
        if (response.selection === 1) {
            const lightState = tank.getDynamicProperty("ttav:vehicle_light");
            if (!lightState) {
                tank.triggerEvent("vehicle_light_on");
                tank.setDynamicProperty("ttav:vehicle_light", true);
                player.playSound("ttavButton1", { pitch: 1.0, volume: 1.0 });
            } else {
                tank.triggerEvent("vehicle_light_off");
                tank.setDynamicProperty("ttav:vehicle_light", false);
                player.playSound("ttavButton1", { pitch: 1.0, volume: 1.0 });
            }
        }
        if (response.selection === 2) {
            player.runCommand("playsound pnt.v_horn @a[r=30]");
            showMLRSControlUI(player);
        }
        if (response.selection === 5) {
            const engineState = tank.getDynamicProperty("ttav:engine");
            const vehicleConfig = Object.values(VEHICLES).find(v => v.identifier === tank.typeId);
            if (engineState) {
                showMLRSMainGunUI(player);
                tank.addTag("stationary_mode");
                tank.triggerEvent("start_stationary");
                player.playSound("ttavButton2", { pitch: 1.0, volume: 1.0 });
                player.playSound("ttavStationary", { pitch: 1.0, volume: 1.0 });
                player.runCommand(`clear @s ${vehicleConfig.subGunItem}`);
                player.runCommand(`clear @s ${vehicleConfig.flareGunItem}`);
            } else {
                player.onScreenDisplay.setActionBar("§c发动机必须运转才能切换武器!");
                player.playSound("note.bass", { pitch: 1.0, volume: 1.0 });
            }
        }
        
        if (response.selection === 6) {
            const engineState = tank.getDynamicProperty("ttav:engine");
            if (engineState) {
                const vehicleConfig = Object.values(VEHICLES).find(v => v.identifier === tank.typeId);
                if (vehicleConfig) {
                    tank.triggerEvent("stop_stationary");
                    tank.removeTag("stationary_mode");
                    player.playSound("ttavButton2", { pitch: 1.0, volume: 1.0 });
                    player.runCommand(`replaceitem entity @s slot.hotbar 1 ${vehicleConfig.subGunItem} 1 0 {"item_lock":{"mode":"lock_in_slot"},"keep_on_death":{}}`);
                }
            } else {
                player.onScreenDisplay.setActionBar("§c发动机必须运转才能切换武器!");
                player.playSound("note.bass", { pitch: 1.0, volume: 1.0 });
            }
        }
        
        if (response.selection === 7) {
            const engineState = tank.getDynamicProperty("ttav:engine");
            if (engineState) {
                const vehicleConfig = Object.values(VEHICLES).find(v => v.identifier === tank.typeId);
                if (vehicleConfig) {
                    tank.triggerEvent("stop_stationary");
                    tank.removeTag("stationary_mode");
                    player.playSound("ttavButton2", { pitch: 1.0, volume: 1.0 });
                    player.runCommand(`replaceitem entity @s slot.hotbar 1 ${vehicleConfig.flareGunItem} 1 0 {"item_lock":{"mode":"lock_in_slot"},"keep_on_death":{}}`);
                }
            } else {
                player.onScreenDisplay.setActionBar("§c发动机必须运转才能切换武器!");
                player.playSound("note.bass", { pitch: 1.0, volume: 1.0 });
            }
        }
        
        if (response.selection === 8) {
            const engineState = tank.getDynamicProperty("ttav:engine");
            if (engineState) {
                if (tank.hasTag("stationary_mode")) {
                    tank.triggerEvent("stop_stationary");
                    tank.removeTag("stationary_mode");
                    player.playSound("ttavStationary", { pitch: 1.0, volume: 1.0 });
                } else {
                    tank.triggerEvent("start_stationary");
                    tank.addTag("stationary_mode");
                    player.playSound("ttavStationary", { pitch: 1.0, volume: 1.0 });
                }
                showMLRSControlUI(player);
            } else {
                player.onScreenDisplay.setActionBar("§c发动机必须运转才能切换固定模式!");
                player.playSound("note.bass", { pitch: 1.0, volume: 1.0 });
                showMLRSControlUI(player);
            }
            return;
        }
        
        if (response.selection === 9) {
            showMLRSRadarUI(player, tank);
            player.playSound("ttavButton1", { pitch: 1.0, volume: 1.0 });
            return;
        }
    });
}
export function showIFVRocketControlUI(player) {
    const tankTag = Array.from(player.getTags()).find(tag => tag.startsWith('tank_'));
    if (!tankTag) return;

    const tankId = tankTag.split('_')[1];
    let tank = null;

    for (const dimension of dimensions) {
        const vehicles = dimension.getEntities();
        for (const vehicle of vehicles) {
            if (vehicle.id === tankId) {
                tank = vehicle;
                break;
            }
        }
        if (tank) break;
    }
    if (!tank) return;

    const mainAmmo = getMainAmmo(tank);
    const subAmmo = getSubAmmo(tank);
    const flareAmmo = getFlareAmmo(tank);
    const fuel = tank.getDynamicProperty("fuel_value") || 0;
    const engineStatus = tank.getDynamicProperty("ttav:engine") ? "§aON" : "§cOFF";
    const lightStatus = tank.getDynamicProperty("ttav:vehicle_light") ? "§aON" : "§cOFF";
    const location = tank.location;

    const statusText = 
        `§6§l位置§r\n` +
        `§e${Math.floor(location.x)}, ${Math.floor(location.y)}, ${Math.floor(location.z)}\n\n` +
        `§6§l火炮状态§r\n` +
        `§6引擎: \n${engineStatus}\n` +
        `§6灯: \n${lightStatus}\n` +
        `§6燃料: §e${Math.floor((fuel / 10000) * 100)}%\n` +
        `§6§l固定: §r\n${tank.hasTag("stationary_mode") ? "§aON" : "§cOFF"}\n` +
        `\n§6§l弹药§r\n` +
        `§6主炮: \n§e${mainAmmo}\n` +
        `§6副炮: \n§e${subAmmo}\n` +
        `§6信号弹: \n§e${flareAmmo}`;

    const form = new ActionFormData()
        .title("载具控制器")
        .body(statusText)
        .button(
            tank.getDynamicProperty("ttav:engine") ?
            "引擎: ON\n§7点击关闭" :
            "引擎: OFF\n§7点击打开",
            tank.getDynamicProperty("ttav:engine") ?
            "textures/ui/ttav_buttons/main_power_button_on" :
            "textures/ui/ttav_buttons/main_power_button_off"
        )
        .button(
            tank.getDynamicProperty("ttav:vehicle_light") ?
            "引擎: ON\n§7点击关闭" :
            "引擎: OFF\n§7点击打开",
            tank.getDynamicProperty("ttav:vehicle_light") ?
            "textures/ui/ttav_buttons/toggle_on_button" :
            "textures/ui/ttav_buttons/toggle_off_button"
        )
        .button("喇叭", "textures/ui/ttav_buttons/normal_button")
        .button("设置", "textures/ui/ttav_buttons/normal_button")
        .button("返回", "textures/ui/ttav_buttons/back_button")
        .button("主炮模式", "textures/ui/ttav_buttons/main_gun_button")
        .button("副炮模式", "textures/ui/ttav_buttons/sub_gun_button")
        .button("信号弹模式", "textures/ui/ttav_buttons/flare_gun_button")
        .button("子弹类型", "textures/ui/ttav_buttons/normal_button")
        .button("雷达", "textures/ui/ttav_buttons/normal_button");

    form.show(player).then(response => {
        if (response.canceled) return;

        if (response.selection === 3) {
            showTankSettingsUI(player);
            player.playSound("ttavButton1", { pitch: 1.0, volume: 1.0 });
            return;
        }
        if (response.selection === 4) return;

        if (response.selection === 0) {
            const currentState = tank.getDynamicProperty("ttav:engine");
            if (!currentState) {
                const currentFuel = tank.getDynamicProperty("fuel_value") || 0;
                if (currentFuel > 0) {
                    system.runTimeout(() => {
                        tank.triggerEvent("engine_on");
                        tank.setDynamicProperty("ttav:engine", true);
                    }, 28);
                    startCameraSystem();
                    startEngineCameraHud(player);
                    player.playSound("ttavBeepButton", { pitch: 1.0, volume: 1.0 });
                    player.playSound("ttavTankEngineStart", { pitch: 1.0, volume: 1.0 });
                } else {
                    player.sendMessage("§c没有燃料！无法启动引擎.");
                }
            } else {
                tank.triggerEvent("engine_off");
                tank.setDynamicProperty("ttav:engine", false);
                tank.removeTag("stationary_mode");
                stopCameraSystem();
                player.runCommand(`camera @s clear`);
                player.onScreenDisplay.setTitle("tank_clear_cam");
                player.playSound("ttavBeepButton", { pitch: 1.0, volume: 1.0 });
                player.playSound("ttavTankEngineStop", { pitch: 1.0, volume: 1.0 });
                const vehicleConfig = Object.values(VEHICLES).find(v => v.identifier === tank.typeId);
                if (vehicleConfig) {
                    player.runCommand(`clear @s ${vehicleConfig.mainGunItem}`);
                    player.runCommand(`clear @s ${vehicleConfig.subGunItem}`);
                    player.runCommand(`clear @s ${vehicleConfig.flareGunItem}`);
                }    
            }
        }
        if (response.selection === 1) {
            const lightState = tank.getDynamicProperty("ttav:vehicle_light");
            if (!lightState) {
                tank.triggerEvent("vehicle_light_on");
                tank.setDynamicProperty("ttav:vehicle_light", true);
                player.playSound("ttavButton1", { pitch: 1.0, volume: 1.0 });
            } else {
                tank.triggerEvent("vehicle_light_off");
                tank.setDynamicProperty("ttav:vehicle_light", false);
                player.playSound("ttavButton1", { pitch: 1.0, volume: 1.0 });
            }
        }
        if (response.selection === 2) {
            player.runCommand("playsound pnt.v_horn @a[r=30]");
            showIFVRocketControlUI(player);
        }
        if (response.selection === 5) {
            const engineState = tank.getDynamicProperty("ttav:engine");
            const vehicleConfig = Object.values(VEHICLES).find(v => v.identifier === tank.typeId);
            if (engineState) {
                showIFVRocketMainGunUI(player);
                player.playSound("ttavButton2", { pitch: 1.0, volume: 1.0 });
                player.runCommand(`clear @s ${vehicleConfig.subGunItem}`);
                player.runCommand(`clear @s ${vehicleConfig.flareGunItem}`);
            } else {
                player.onScreenDisplay.setActionBar("§c发动机必须运转才能切换武器!");
                player.playSound("note.bass", { pitch: 1.0, volume: 1.0 });
            }
        }
        
        if (response.selection === 6) {
            const engineState = tank.getDynamicProperty("ttav:engine");
            if (engineState) {
                const vehicleConfig = Object.values(VEHICLES).find(v => v.identifier === tank.typeId);
                if (vehicleConfig) {
                    player.playSound("ttavButton2", { pitch: 1.0, volume: 1.0 });
                    player.runCommand(`replaceitem entity @s slot.hotbar 1 ${vehicleConfig.subGunItem} 1 0 {"item_lock":{"mode":"lock_in_slot"},"keep_on_death":{}}`);
                }
            } else {
                player.onScreenDisplay.setActionBar("§c发动机必须运转才能切换武器!");
                player.playSound("note.bass", { pitch: 1.0, volume: 1.0 });
            }
        }
        
        if (response.selection === 7) {
            const engineState = tank.getDynamicProperty("ttav:engine");
            if (engineState) {
                const vehicleConfig = Object.values(VEHICLES).find(v => v.identifier === tank.typeId);
                if (vehicleConfig) {
                    player.playSound("ttavButton2", { pitch: 1.0, volume: 1.0 });
                    player.runCommand(`replaceitem entity @s slot.hotbar 1 ${vehicleConfig.flareGunItem} 1 0 {"item_lock":{"mode":"lock_in_slot"},"keep_on_death":{}}`);
                }
            } else {
                player.onScreenDisplay.setActionBar("§c发动机必须运转才能切换武器!");
                player.playSound("note.bass", { pitch: 1.0, volume: 1.0 });
            }
        }
        
        if (response.selection === 8) {
            IFVSRockethellSelectionUI(player);
            player.playSound("ttavButton1", { pitch: 1.0, volume: 1.0 });
            return;
        }
        
        if (response.selection === 9) {
            showIFVRocketRadarUI(player, tank);
            player.playSound("ttavButton1", { pitch: 1.0, volume: 1.0 });
            return;
        }
    });
}
export function IFVSRockethellSelectionUI(player) {
    const form = new ActionFormData()
        .title("子弹选择")
        .button("§6温压弹\n§eAOE伤害", "textures/ui/ttav_buttons/toggle_off_button")
        .button("§6尾翼稳定脱壳穿甲弹\n§e单个目标", "textures/ui/ttav_buttons/toggle_off_button")
        .button("§6磷弹\n§eAOE伤害", "textures/ui/ttav_buttons/toggle_off_button")
        .button("返回", "textures/ui/ttav_buttons/back_button");

    form.show(player).then(response => {
        if (response.canceled || response.selection === 3) {
            showIFVRocketControlUI(player);
            player.playSound("ttavCameraSwitch", { pitch: 1.0, volume: 1.0 });
            return;
        }

        
        player.removeTag("heat_shell");
        player.removeTag("apfsds_shell");
        player.removeTag("phosphorus_shell");

        
        switch (response.selection) {
            case 0:
                player.addTag("heat_shell");
                player.onScreenDisplay.setActionBar("§e[ §6选择了§e温压弹 §e]");
                break;
            case 1:
                player.addTag("apfsds_shell");
                player.onScreenDisplay.setActionBar("§e[ §6选择了§eAPFSDS §e]");
                break;
            case 2:
                player.addTag("phosphorus_shell");
                player.onScreenDisplay.setActionBar("§e[ §6选择了§e磷弹 §e]");
                break;
        }
        player.playSound("ttavInteract", { pitch: 1.0, volume: 1.0 });
    });
}
export function startEngineCameraHud(player) {
    
    player.onScreenDisplay.setTitle("tank_main_gun_cam_connecting1");
    system.runTimeout(() => {
        player.onScreenDisplay.setTitle("tank_main_gun_cam_connecting2");
        system.runTimeout(() => {
            player.onScreenDisplay.setTitle("tank_main_gun_cam_connecting3");
            system.runTimeout(() => {
                player.onScreenDisplay.setTitle("tank_main_gun_cam_connecting4");
            }, 8);
        }, 8);
    }, 8);
}
