import { system, world } from "@minecraft/server";
import { UNDO_TOOL } from './main';
import { positions } from './utils';

const { offsetX, offsetY, offsetZ, sizeX, sizeY, sizeZ } = positions;

export function buildPlace(player, placeRotation, adjustedBlockLocX, adjustedBlockLocY, adjustedBlockLocZ, buildVariant) {
    let placeX = adjustedBlockLocX;
    let placeY = adjustedBlockLocY + offsetY[buildVariant];
    let placeZ = adjustedBlockLocZ;

    if (placeRotation === 0) {
        placeX += offsetX[buildVariant];
        placeZ += offsetZ[buildVariant];
    } else if (placeRotation === 90) {
        placeX -= (sizeZ[buildVariant] + offsetZ[buildVariant]);
        placeZ += offsetX[buildVariant];
    } else if (placeRotation === 180) {
        placeX -= (sizeX[buildVariant] + offsetX[buildVariant]);
        placeZ -= (sizeZ[buildVariant] + offsetZ[buildVariant]);
    } else if (placeRotation === 270) {
        placeX += offsetZ[buildVariant];
        placeZ -= (sizeX[buildVariant] + offsetX[buildVariant]);
    }

    let toX = placeX + sizeX[buildVariant];
    let toY = placeY + sizeY[buildVariant];
    let toZ = placeZ + sizeZ[buildVariant];

    world.setDynamicProperty("buildVariant", buildVariant);
    world.setDynamicProperty("placeRotation", placeRotation);
    world.setDynamicProperty("placeX", placeX);
    world.setDynamicProperty("placeY", placeY);
    world.setDynamicProperty("placeZ", placeZ);

    player.runCommand(`structure delete "DodoStudios:build_undo"`);
    player.runCommand(`tag @e[tag=new] remove new`);
    player.runCommand(`event entity @e[type=dodo:tick] minecraft:instant_despawn`);

    player.runCommand(`structure save "DodoStudios:build_undo" ${placeX} ${placeY} ${placeZ} ${toX} ${toY} ${toZ} false disk`);
    player.runCommand(`structure load "builds/clear/build_${buildVariant}" ${placeX} ${placeY} ${placeZ} ${placeRotation}_degrees`);
    player.runCommand(`structure load "builds/build_${buildVariant}" ${placeX} ${placeY} ${placeZ} ${placeRotation}_degrees`);
    player.runCommand(`summon dodo:tick ${placeX} 275 ${placeZ} 0 0 minecraft:entity_born`);

    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 ~`);

    world.setDynamicProperty("undo", true);
    player.runCommandAsync(`execute unless entity @s[hasitem={item=${UNDO_TOOL}}] run give @s ${UNDO_TOOL}`);

    system.runTimeout(() => {
        player.runCommandAsync(`event entity @e[family=build_block,r=48] dodo:setup`);
    }, 10);
}