import { world, Direction } from "@minecraft/server";
import { NAMESPACE, ITEM_ID } from './main';
import { buildPlace } from './buildPlace';

function adjustBlockLocation(block, blockFace) {
    let adjustedBlockLocX = Math.floor(block.location.x + 0.5);
    let adjustedBlockLocY = Math.floor(block.location.y + 0.5);
    let adjustedBlockLocZ = Math.floor(block.location.z + 0.5);

    switch(blockFace) {
        case Direction.East:
            adjustedBlockLocX += 1;
            break;
        case Direction.West:
            adjustedBlockLocX -= 1;
            break;
        case Direction.Up:
            adjustedBlockLocY += 1;
            break;
        case Direction.Down:
            adjustedBlockLocY -= 1;
            break;
        case Direction.South:
            adjustedBlockLocZ += 1;
            break;
        case Direction.North:
            adjustedBlockLocZ -= 1;
            break;
    }

    return [adjustedBlockLocX, adjustedBlockLocY, adjustedBlockLocZ];
}

function placeRotation(yaw) {
    if (yaw >= -45 && yaw <= 45) {
        return 0;
    } else if (yaw > 45 && yaw <= 135) {
        return 90;
    } else if (yaw > -135 && yaw < -45) {
        return 270;
    } else {
        return 180;
    }
}

function getPlaceRot(player) {
    const rawYaw = player.getRotation();
    return placeRotation(rawYaw.y);
}

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

    const [adjustedBlockLocX, adjustedBlockLocY, adjustedBlockLocZ] = adjustBlockLocation(event.block, event.blockFace);

    if (item.startsWith(ITEM_ID)) {
        const block = world.getDimension(player.dimension.id).getBlock({ x: adjustedBlockLocX, y: adjustedBlockLocY, z: adjustedBlockLocZ });
        if (block && (block.typeId === "minecraft:water" || block.typeId === "minecraft:flowing_water")) {
            player.runCommandAsync(`playsound random.pop @s ~ ~ ~ 0.75 1 0.75`);
            player.sendMessage({ translate: `${NAMESPACE}.builds.cant_place_in_water` });
            event.cancel = true;
        } else if (block && (block.typeId === "minecraft:lava" || block.typeId === "minecraft:flowing_lava")) {
            player.runCommandAsync(`playsound random.pop @s ~ ~ ~ 0.75 1 0.75`);
            player.sendMessage({ translate: `${NAMESPACE}.builds.cant_place_in_lava` });
            event.cancel = true;
        }
    }
});

world.afterEvents.itemUseOn.subscribe(event => {
    const player = event.source;
    const item = event.itemStack.typeId;
    
    const [adjustedBlockLocX, adjustedBlockLocY, adjustedBlockLocZ] = adjustBlockLocation(event.block, event.blockFace);

    const undoCooldown = player.getItemCooldown("undo_tool");

    if (!undoCooldown) {
        if (item.startsWith(ITEM_ID)) {
            player.startItemCooldown("undo_tool", 20);
            
            const placeRotation = getPlaceRot(player);
            const buildVariant = parseInt(item.split("_")[2]);
    
            buildPlace(player, placeRotation, adjustedBlockLocX, adjustedBlockLocY, adjustedBlockLocZ, buildVariant)
        }
    }
});