import { world, BlockPermutation, ItemStack, GameMode, system } from '@minecraft/server';
import { fallingBlocks, checkBlocksToFall } from '../chandelierFalling';

world.afterEvents.playerPlaceBlock.subscribe(eventData => {
    const player = eventData.player;
    const block = eventData.block;

    if (block.typeId != 'rustic:rope')return;

    const blocFace = block.permutation.getState("minecraft:block_face");

    if (block.north().typeId == 'rustic:rope' || block.north().hasTag("leashed")){
        if (block.north().permutation.getState("minecraft:block_face") != 'north' && block.north().permutation.getState("minecraft:block_face") != 'south'){
            block.north().setPermutation(block.north().permutation.withState("r:south", true))
        }
        if (blocFace != 'north' && blocFace != 'south') {
            block.setPermutation(block.permutation.withState("r:north", true));
        }
    }
    if (block.south().typeId == 'rustic:rope' || block.south().hasTag("leashed")){
        if (block.south().permutation.getState("minecraft:block_face") != 'north' && block.south().permutation.getState("minecraft:block_face") != 'south'){
            block.south().setPermutation(block.south().permutation.withState("r:north", true))
        }
        if (blocFace != 'north' && blocFace != 'south') {
            block.setPermutation(block.permutation.withState("r:south", true));
        }
    }
    if (block.east().typeId == 'rustic:rope' || block.east().hasTag("leashed")){
        if (block.east().permutation.getState("minecraft:block_face") != 'east' && block.east().permutation.getState("minecraft:block_face") != 'west'){
            block.east().setPermutation(block.east().permutation.withState("r:west", true))
        }
        if (blocFace != 'east' && blocFace != 'west') {
            block.setPermutation(block.permutation.withState("r:east", true));
        }
    }
    if (block.west().typeId == 'rustic:rope' || block.west().hasTag("leashed")){
        if (block.west().permutation.getState("minecraft:block_face") != 'east' && block.west().permutation.getState("minecraft:block_face") != 'west'){
            block.west().setPermutation(block.west().permutation.withState("r:east", true))
        }
        if (blocFace != 'east' && blocFace != 'west') {
            block.setPermutation(block.permutation.withState("r:west", true));
        }
    }
    if (block.above().typeId == 'rustic:rope'){
        if (block.above().permutation.getState("minecraft:block_face") != 'up' && block.above().permutation.getState("minecraft:block_face") != 'down'){
            block.above().setPermutation(block.above().permutation.withState("r:down", true))
        }
        if (blocFace != 'up' && blocFace != 'down') {
            block.setPermutation(block.permutation.withState("r:up", true));
        }
    }
    if (block.below().typeId == 'rustic:rope'){
        if (block.below().permutation.getState("minecraft:block_face") != 'up' && block.below().permutation.getState("minecraft:block_face") != 'down'){
            block.below().setPermutation(block.below().permutation.withState("r:up", true))
        }
        if (blocFace != 'up' && blocFace != 'down') {
            block.setPermutation(block.permutation.withState("r:down", true));
        }
    }

});

world.afterEvents.playerBreakBlock.subscribe(eventData => {
    const player = eventData.player;
    const brokenBlock = eventData.brokenBlockPermutation;
    const block = eventData.block

    if (brokenBlock.type.id != 'rustic:rope' && !brokenBlock.hasTag("leashed"))return;

    if (block.north().typeId == 'rustic:rope' || block.north().hasTag("leashed")) block.north().setPermutation(block.north().permutation.withState("r:south", false));
    if (block.south().typeId == 'rustic:rope' || block.south().hasTag("leashed")) block.south().setPermutation(block.south().permutation.withState("r:north", false));
    if (block.east().typeId == 'rustic:rope' || block.east().hasTag("leashed")) block.east().setPermutation(block.east().permutation.withState("r:west", false));
    if (block.west().typeId == 'rustic:rope' || block.west().hasTag("leashed")) block.west().setPermutation(block.west().permutation.withState("r:east", false));
    if (block.above().typeId == 'rustic:rope') block.above().setPermutation(block.above().permutation.withState("r:down", false));
    if (block.below().typeId == 'rustic:rope') block.below().setPermutation(block.below().permutation.withState("r:up", false));

});

world.afterEvents.projectileHitBlock.subscribe(eventData => {
    const dimension = eventData.dimension;
    const block = eventData.getBlockHit().block
    const projectile = eventData.projectile
    if (projectile.typeId != "minecraft:arrow")return;
    if (block.typeId != "rustic:rope")return;
    dimension.runCommand(`setblock ${block.x} ${block.y} ${block.z} air destroy`)
    if (block.below().hasTag("isFallingBlock")){
        for(const fallingBlock of fallingBlocks) {
            checkBlocksToFall(block.below(), `${fallingBlock.identifier}`, `${fallingBlock.entity}`);
        }
    }
})