import { system, world, GameMode } from "@minecraft/server";
import { NAMESPACE, ITEM_ID, UNDO_TOOL } from './main';

world.beforeEvents.itemUse.subscribe(event => {
    const player = event.source;
    const item = event.itemStack.typeId;

    if (item === UNDO_TOOL) {
        const buildVariant = world.getDynamicProperty("buildVariant");
        const placeRotation = world.getDynamicProperty("placeRotation");
        const placeX = world.getDynamicProperty("placeX");
        const placeY = world.getDynamicProperty("placeY");
        const placeZ = world.getDynamicProperty("placeZ");
        const undo = world.getDynamicProperty("undo");
        const undoCooldown = player.getItemCooldown("undo_tool");

        if (!undoCooldown) {
            if (undo === false) {
                player.runCommandAsync(`playsound random.pop @s ~ ~ ~ 0.75 1 0.75`);
                player.sendMessage({ translate: `${NAMESPACE}.builds.no_undo` });
            } else {
                world.setDynamicProperty("undo", false);
                
                system.run(() => {
                    player.runCommand(`event entity @e[tag=new] minecraft:instant_despawn`);
                    player.runCommand(`execute as @e[type=!player,family=!furniture,tag=new] at @s run teleport @s ~ -100 ~`);

                    player.runCommand(`structure load "builds/clear/build_${buildVariant}" ${placeX} ${placeY} ${placeZ} ${placeRotation}_degrees`);
                    player.runCommand(`structure load "DodoStudios:build_undo" ${placeX} ${placeY} ${placeZ}`);
                    player.runCommand(`structure delete "DodoStudios:build_undo"`);
                    player.runCommand(`event entity @e[type=dodo:tick] minecraft:instant_despawn`);

                    player.runCommand(`execute as @a[r=48] at @s unless block ~ ~ ~ air [] unless block ~ ~ ~ water [] unless block ~ ~ ~ short_grass [] unless block ~ ~ ~ tall_grass [] unless block ~ ~ ~ snow_layer [] unless block ~ ~ ~ grass_path [] run effect @s slow_falling 5 0 true`);
                    player.runCommand(`execute as @a[r=48] at @s unless block ~ ~ ~ air [] unless block ~ ~ ~ water [] unless block ~ ~ ~ short_grass [] unless block ~ ~ ~ tall_grass [] unless block ~ ~ ~ snow_layer [] unless block ~ ~ ~ grass_path [] run teleport @s ~ ~16 ~`);
                    player.runCommand(`execute as @a[r=48] at @s unless block ~ ~ ~ air [] unless block ~ ~ ~ water [] unless block ~ ~ ~ short_grass [] unless block ~ ~ ~ tall_grass [] unless block ~ ~ ~ snow_layer [] unless block ~ ~ ~ grass_path [] run effect @s slow_falling 8 0 true`);
                    player.runCommand(`execute as @a[r=48] at @s unless block ~ ~ ~ air [] unless block ~ ~ ~ water [] unless block ~ ~ ~ short_grass [] unless block ~ ~ ~ tall_grass [] unless block ~ ~ ~ snow_layer [] unless block ~ ~ ~ grass_path [] run teleport @s ~ ~16 ~`);
                    player.runCommand(`execute as @a[r=48] at @s unless block ~ ~ ~ air [] unless block ~ ~ ~ water [] unless block ~ ~ ~ short_grass [] unless block ~ ~ ~ tall_grass [] unless block ~ ~ ~ snow_layer [] unless block ~ ~ ~ grass_path [] run teleport @s ~ ~16 ~`);
                    player.runCommand(`execute as @a[r=48] at @s unless block ~ ~ ~ air [] unless block ~ ~ ~ water [] unless block ~ ~ ~ short_grass [] unless block ~ ~ ~ tall_grass [] unless block ~ ~ ~ snow_layer [] unless block ~ ~ ~ grass_path [] run teleport @s ~ ~16 ~`);
                });
                
                player.runCommandAsync(`clear @s ${UNDO_TOOL}`);

                if (player.getGameMode() === GameMode.creative) {
                    player.runCommandAsync(`execute unless entity @s[hasitem={item=${ITEM_ID}_${buildVariant}}] run give @s ${ITEM_ID}_${buildVariant}`);
                } else {
                    player.runCommandAsync(`give @s ${ITEM_ID}_${buildVariant}`);
                }
            }
        }
    }
});