import { world, system } from "@minecraft/server";
import { ActionFormData, ModalFormData } from "@minecraft/server-ui";

function hasTankEquipmentInInventory(player) {
    
    const invComp = player.getComponent("minecraft:inventory");
    if (!invComp) {
        
        return false;
    }

    const container = invComp.container;
    
    for (let i = 0; i < container.size; i++) {
        const slotItem = container.getItem(i);
        if (slotItem && slotItem.typeId === "ttav:tank_equipment") {
            
            return true;
        }
    }
    
    return false;
}

function showTankSettingsUI(player) {
    
    const tankTag = Array.from(player.getTags()).find((tag) =>
        tag.startsWith("tank_")
    );
    if (!tankTag) {
        
        return;
    }
    

    const tankId = tankTag.split("_")[1];
    
    let tank = player.dimension.getEntities().find((e) => e.id === tankId);
    if (!tank) {
        
        return;
    }
    

    const form = new ActionFormData()
        .title("军事载具设置")
        .button("§6修理", "textures/ui/ttav_buttons/normal_button")
        .button("§6命名", "textures/ui/ttav_buttons/normal_button")
        .button("§6停车模式", "textures/ui/ttav_buttons/normal_button")
        .button("返回", "textures/ui/ttav_buttons/back_button");

    form.show(player).then((response) => {
        
        if (response.canceled || response.selection === 3) {
            
            return;
        }

        switch (response.selection) {
            case 0: 
                
                if (hasTankEquipmentInInventory(player)) {
                    
                    tank.addEffect("instant_health", 2, {
                        amplifier: 20,
                        showParticles: true,
                    });
                    player.onScreenDisplay.setActionBar("§a油箱维修成功!");
                } else {
                    
                    player.onScreenDisplay.setActionBar(
                        "§c你需要修理该设备!"
                    );
                }
                break;

            case 1: 
                
                const renameForm = new ModalFormData()
                    .title("重命名")
                    .textField("输入新名字:", "在此输入新名字...");

                renameForm.show(player).then((renameResponse) => {
                    
                    if (!renameResponse.canceled && renameResponse.formValues[0]) {
                        
                        tank.nameTag = renameResponse.formValues[0];
                        player.onScreenDisplay.setActionBar("§a重命名成功!");
                    }
                    showTankSettingsUI(player);
                });
                break;

            case 2: 
                
                const isParkModeOn = tank.getDynamicProperty("park_mode") || false;
                
                if (!isParkModeOn) {
                    tank.triggerEvent("turn_on_park_mode");
                    tank.setDynamicProperty("park_mode", true);
                    player.onScreenDisplay.setActionBar("§a车灯打开!");
                } else {
                    tank.triggerEvent("turn_off_park_mode");
                    tank.setDynamicProperty("park_mode", false);
                    player.onScreenDisplay.setActionBar("§a车灯关闭!");
                }
                break;
            
        }
    });
}

export { showTankSettingsUI };
