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

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

    if (block.north().hasTag("rustic:fence")) block.north().setPermutation(block.north().permutation.withState("r:south", false));
    if (block.south().hasTag("rustic:fence")) block.south().setPermutation(block.south().permutation.withState("r:north", false));
    if (block.east().hasTag("rustic:fence")) block.east().setPermutation(block.east().permutation.withState("r:west", false));
    if (block.west().hasTag("rustic:fence")) block.west().setPermutation(block.west().permutation.withState("r:east", false));

});

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

    const allowedBlock = !excludeBlocksArrayFence.includes(block?.typeId);


    if (!allowedBlock) return;

    if (block.north().hasTag("rustic:fence")) block.north().setPermutation(block.north().permutation.withState("r:south", true));
    if (block.south().hasTag("rustic:fence")) block.south().setPermutation(block.south().permutation.withState("r:north", true));
    if (block.east().hasTag("rustic:fence")) block.east().setPermutation(block.east().permutation.withState("r:west", true));
    if (block.west().hasTag("rustic:fence")) block.west().setPermutation(block.west().permutation.withState("r:east", true));

});

system.beforeEvents.startup.subscribe((initEvent) => {
    initEvent.blockComponentRegistry.registerCustomComponent("rustic:fence_place", {
        beforeOnPlayerPlace: e => {
            const { player, block, face, permutationToPlace, dimension } = e;

            const north = block.north();
            const east = block.east();
            const south = block.south();
            const west = block.west();

            const northConnects = !excludeBlocksArrayFence.includes(north?.typeId);
            const eastConnects = !excludeBlocksArrayFence.includes(east?.typeId);
            const southConnects = !excludeBlocksArrayFence.includes(south?.typeId);
            const westConnects = !excludeBlocksArrayFence.includes(west?.typeId);
            
            if (north.hasTag("rustic:fence")) north.setPermutation(north.permutation.withState("r:south", true));
            if (south.hasTag("rustic:fence")) south.setPermutation(south.permutation.withState("r:north", true));
            if (east.hasTag("rustic:fence")) east.setPermutation(east.permutation.withState("r:west", true));
            if (west.hasTag("rustic:fence")) west.setPermutation(west.permutation.withState("r:east", true));

            e.permutationToPlace = BlockPermutation.resolve(permutationToPlace.type.id, {"r:placed_bit": 1, "r:north": northConnects ? true : false, "r:south": southConnects ? true : false, "r:east": eastConnects ? true : false, "r:west": westConnects ? true : false});
        },
    });
});


export const excludeBlocksArrayFence = [
    'minecraft:air',
    'minecraft:wooden_door', 
    'minecraft:iron_door', 
    'minecraft:acacia_door', 
    'minecraft:birch_door', 
    'minecraft:crimson_door', 
    'minecraft:dark_oak_door', 
    'minecraft:jungle_door', 
    'minecraft:oak_door', 
    'minecraft:spruce_door', 
    'minecraft:warped_door', 
    'minecraft:mangrove_door',
    'minecraft:cherry_door',
    'minecraft:bamboo_door',
    'minecraft:iron_trapdoor', 
    'minecraft:acacia_trapdoor', 
    'minecraft:birch_trapdoor', 
    'minecraft:crimson_trapdoor', 
    'minecraft:dark_oak_trapdoor', 
    'minecraft:jungle_trapdoor', 
    'minecraft:oak_trapdoor', 
    'minecraft:spruce_trapdoor', 
    'minecraft:warped_trapdoor',
    'minecraft:mangrove_trapdoor',
    'minecraft:cherry_trapdoor',
    'minecraft:bamboo_trapdoor',
    'minecraft:trapdoor', 
    'minecraft:glass',
    'minecraft:glass_pane'
];