import { world } from "@minecraft/server";

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('we:fence_gate', {
        onPlayerInteract: e => {
            const { block, face } = e;
            const open_bit = block.permutation.getState('we:open_bit');
            const cardinal_direction = block.permutation.getState('minecraft:cardinal_direction');
            if(open_bit == false) {
                if(face == 'South' && (cardinal_direction == 'north'|| cardinal_direction == 'south')) {
                    block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', 'north'));
                    block.setPermutation(block.permutation.withState('we:open_bit', true));
                }
                if(face == 'North' && (cardinal_direction == 'north'|| cardinal_direction == 'south')) {
                    block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', 'south'));
                    block.setPermutation(block.permutation.withState('we:open_bit', true));
                }
                if(cardinal_direction == 'north' || cardinal_direction == 'south') {
                    block.setPermutation(block.permutation.withState('we:open_bit', true));
                    block.dimension.playSound('open.cherry_wood_fence_gate', block.location);
                }
                if(face == 'East' && (cardinal_direction == 'west'|| cardinal_direction == 'east')) {
                    block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', 'east'));
                    block.setPermutation(block.permutation.withState('we:open_bit', true));
                }
                if(face == 'West' && (cardinal_direction == 'west'|| cardinal_direction == 'east')) {
                    block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', 'west'));
                    block.setPermutation(block.permutation.withState('we:open_bit', true));
                }
                if(cardinal_direction == 'west' || cardinal_direction == 'east') {
                    block.setPermutation(block.permutation.withState('we:open_bit', true));
                    block.dimension.playSound('open.cherry_wood_fence_gate', block.location);
                }
            }
            if(open_bit == true) {
                if(face == 'South' && (cardinal_direction == 'north'|| cardinal_direction == 'south')) {
                    block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', 'north'));
                    block.setPermutation(block.permutation.withState('we:open_bit', false));
                }
                if(face == 'North' && (cardinal_direction == 'north'|| cardinal_direction == 'south')) {
                    block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', 'south'));
                    block.setPermutation(block.permutation.withState('we:open_bit', false));
                }
                if(cardinal_direction == 'north' || cardinal_direction == 'south') {
                    block.setPermutation(block.permutation.withState('we:open_bit', false));
                    block.dimension.playSound('close.cherry_wood_fence_gate', block.location);
                }
                if(face == 'East' && (cardinal_direction == 'west'|| cardinal_direction == 'east')) {
                    block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', 'east'));
                    block.setPermutation(block.permutation.withState('we:open_bit', false));
                }
                if(face == 'West' && (cardinal_direction == 'west'|| cardinal_direction == 'east')) {
                    block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', 'west'));
                    block.setPermutation(block.permutation.withState('we:open_bit', false));
                }
                if(cardinal_direction == 'west' || cardinal_direction == 'east') {
                    block.setPermutation(block.permutation.withState('we:open_bit', false));
                    block.dimension.playSound('close.cherry_wood_fence_gate', block.location);
                }
            }
        },
        onPlace: e => {
            const { block } = e;
            block.setPermutation(block.permutation.withState('we:placed_bit', 1));
        }
    });
});