import * as mc from "@minecraft/server"
import { baobab_branchPattern } from "./system.js"


export function allEvents(id, block, blockId) {
    if (id === "ecbl_bc:conveyor") {
        let states = block.permutation.getAllStates();
        if (states["minecraft:cardinal_direction"] == 'north') {
            setPermutation(block, "ecbl_bc:north", (block.north().hasTag("wood_connect") && (block.north().permutation.getState("minecraft:cardinal_direction") != 'south')) || (block.below().north().hasTag("wood_connect") && block.below().north().permutation.getState("ecbl_bc:state") != "default" && block.below().north().permutation.getState("minecraft:cardinal_direction") == 'south') || (block.north().hasTag("wood_connect") && block.north().permutation.getState("ecbl_bc:state") != "default" && block.north().permutation.getState("minecraft:cardinal_direction") == 'south'), 'ecbl_bc:conveyor');
            setPermutation(block, "ecbl_bc:south", (block.south()?.hasTag("wood_connect") && (block.south().permutation.getState("minecraft:cardinal_direction") == 'north')) || (block.below().south().hasTag("wood_connect") && block.below().south().permutation.getState("ecbl_bc:state") != "default" && block.below().south().permutation.getState("minecraft:cardinal_direction") == 'north') || (block.south().hasTag("wood_connect") && block.south().permutation.getState("ecbl_bc:state") != "default" && block.south().permutation.getState("minecraft:cardinal_direction") == 'south'), 'ecbl_bc:conveyor');
            setPermutation(block, "ecbl_bc:east", block.east().hasTag("wood_connect") && block.east().permutation.getState("minecraft:cardinal_direction") == 'west', 'ecbl_bc:conveyor');
            setPermutation(block, "ecbl_bc:west", block.west().hasTag("wood_connect") && block.west().permutation.getState("minecraft:cardinal_direction") == 'east', 'ecbl_bc:conveyor');
        }

        if (states["minecraft:cardinal_direction"] == 'south') {
            setPermutation(block, "ecbl_bc:south", (block.north().hasTag("wood_connect") && block.north().permutation.getState("minecraft:cardinal_direction") == 'south') || (block.below().north().hasTag("wood_connect") && block.below().north().permutation.getState("ecbl_bc:state") != "default" && block.below().north().permutation.getState("minecraft:cardinal_direction") == 'south') || (block.north().hasTag("wood_connect") && block.north().permutation.getState("ecbl_bc:state") != "default" && block.north().permutation.getState("minecraft:cardinal_direction") == 'north'), 'ecbl_bc:conveyor');
            setPermutation(block, "ecbl_bc:north", (block.south().hasTag("wood_connect") && block.south().permutation.getState("minecraft:cardinal_direction") != 'north') || (block.below().south().hasTag("wood_connect") && block.below().south().permutation.getState("ecbl_bc:state") != "default" && block.below().south().permutation.getState("minecraft:cardinal_direction") == 'north') || (block.south().hasTag("wood_connect") && block.south().permutation.getState("ecbl_bc:state") != "default" && block.south().permutation.getState("minecraft:cardinal_direction") == 'north'), 'ecbl_bc:conveyor');
            setPermutation(block, "ecbl_bc:west", block.east().hasTag("wood_connect") && block.east().permutation.getState("minecraft:cardinal_direction") == 'west', 'ecbl_bc:conveyor');
            setPermutation(block, "ecbl_bc:east", block.west().hasTag("wood_connect") && block.west().permutation.getState("minecraft:cardinal_direction") == 'east', 'ecbl_bc:conveyor');
        }

        if (states["minecraft:cardinal_direction"] == 'west') {
            setPermutation(block, "ecbl_bc:east", block.north().hasTag("wood_connect") && block.north().permutation.getState("minecraft:cardinal_direction") == 'south', 'ecbl_bc:conveyor');
            setPermutation(block, "ecbl_bc:west", block.south().hasTag("wood_connect") && block.south().permutation.getState("minecraft:cardinal_direction") == 'north', 'ecbl_bc:conveyor');
            setPermutation(block, "ecbl_bc:south", (block.east().hasTag("wood_connect") && block.east().permutation.getState("minecraft:cardinal_direction") == 'west') || (block.below().east().hasTag("wood_connect") && block.below().east().permutation.getState("ecbl_bc:state") != "default" && block.below().east().permutation.getState("minecraft:cardinal_direction") == 'west') || (block.east().hasTag("wood_connect") && block.east().permutation.getState("ecbl_bc:state") != "default" && block.east().permutation.getState("minecraft:cardinal_direction") == 'east'), 'ecbl_bc:conveyor');
            setPermutation(block, "ecbl_bc:north", (block.west().hasTag("wood_connect") && block.west().permutation.getState("minecraft:cardinal_direction") != 'east') || (block.below().west().hasTag("wood_connect") && block.below().west().permutation.getState("ecbl_bc:state") != "default" && block.below().west().permutation.getState("minecraft:cardinal_direction") == 'east') || (block.west().hasTag("wood_connect") && block.west().permutation.getState("ecbl_bc:state") != "default" && block.west().permutation.getState("minecraft:cardinal_direction") == 'east'), 'ecbl_bc:conveyor');
        }

        if (states["minecraft:cardinal_direction"] == 'east') {
            setPermutation(block, "ecbl_bc:west", block.north().hasTag("wood_connect") && block.north().permutation.getState("minecraft:cardinal_direction") == 'south', 'ecbl_bc:conveyor');
            setPermutation(block, "ecbl_bc:east", block.south().hasTag("wood_connect") && block.south().permutation.getState("minecraft:cardinal_direction") == 'north', 'ecbl_bc:conveyor');
            setPermutation(block, "ecbl_bc:north", (block.east()?.hasTag("wood_connect") && block.east().permutation.getState("minecraft:cardinal_direction") != 'west') || (block.below().east().hasTag("wood_connect") && block.below().east().permutation.getState("ecbl_bc:state") != "default" && block.below().east().permutation.getState("minecraft:cardinal_direction") == 'west') || (block.east().hasTag("wood_connect") && block.east().permutation.getState("ecbl_bc:state") != "default" && block.east().permutation.getState("minecraft:cardinal_direction") == 'west'), 'ecbl_bc:conveyor');
            setPermutation(block, "ecbl_bc:south", (block.west().hasTag("wood_connect") && block.west().permutation.getState("minecraft:cardinal_direction") == 'east') || (block.below().west().hasTag("wood_connect") && block.below().west().permutation.getState("ecbl_bc:state") != "default" && block.below().west().permutation.getState("minecraft:cardinal_direction") == 'east') || (block.west().hasTag("wood_connect") && block.west().permutation.getState("ecbl_bc:state") != "default" && block.west().permutation.getState("minecraft:cardinal_direction") == 'west'), 'ecbl_bc:conveyor');
        }

        if (states["minecraft:cardinal_direction"] == 'north' || states["minecraft:cardinal_direction"] == 'south') {
            if (block.south().hasTag("wood_connect") && block.above().north().hasTag("wood_connect") && block.above().north().permutation.getState("minecraft:cardinal_direction") == 'north' && block.south().permutation.getState("minecraft:cardinal_direction") == 'north') {
                setPermutation(block, "ecbl_bc:state", "diagonal_up", 'ecbl_bc:conveyor');
                setPermutation(block, "minecraft:cardinal_direction", 'north', 'ecbl_bc:conveyor');
            } else if (block.south().hasTag("wood_connect") && block.above().north().hasTag("wood_connect") && block.above().north().permutation.getState("minecraft:cardinal_direction") == 'south' && block.south().permutation.getState("minecraft:cardinal_direction") == 'south') {
                setPermutation(block, "ecbl_bc:state", "diagonal_down", 'ecbl_bc:conveyor');
                setPermutation(block, "minecraft:cardinal_direction", 'north', 'ecbl_bc:conveyor');
            } else if (block.north().hasTag("wood_connect") && block.above().south().hasTag("wood_connect") && block.above().south().permutation.getState("minecraft:cardinal_direction") == 'south' && block.north().permutation.getState("minecraft:cardinal_direction") == 'south') {
                setPermutation(block, "ecbl_bc:state", "diagonal_up", 'ecbl_bc:conveyor');
                setPermutation(block, "minecraft:cardinal_direction", 'south', 'ecbl_bc:conveyor');
            } else if (block.north().hasTag("wood_connect") && block.above().south().hasTag("wood_connect") && block.above().south().permutation.getState("minecraft:cardinal_direction") == 'north' && block.north().permutation.getState("minecraft:cardinal_direction") == 'north') {
                setPermutation(block, "ecbl_bc:state", "diagonal_down", 'ecbl_bc:conveyor');
                setPermutation(block, "minecraft:cardinal_direction", 'south', 'ecbl_bc:conveyor');
            } else {
                setPermutation(block, "ecbl_bc:state", "default");
            }
        }

        if (states["minecraft:cardinal_direction"] == 'west' || states["minecraft:cardinal_direction"] == 'east') {
            if (block.east().hasTag("wood_connect") && block.above().west().hasTag("wood_connect") && block.above().west().permutation.getState("minecraft:cardinal_direction") == 'west' && block.east().permutation.getState("minecraft:cardinal_direction") == 'west') {
                setPermutation(block, "ecbl_bc:state", "diagonal_up", 'ecbl_bc:conveyor');
                setPermutation(block, "minecraft:cardinal_direction", 'west', 'ecbl_bc:conveyor');
            } else if (block.east().hasTag("wood_connect") && block.above().west().hasTag("wood_connect") && block.above().west().permutation.getState("minecraft:cardinal_direction") == 'east' && block.east().permutation.getState("minecraft:cardinal_direction") == 'east') {
                setPermutation(block, "ecbl_bc:state", "diagonal_down", 'ecbl_bc:conveyor');
                setPermutation(block, "minecraft:cardinal_direction", 'west', 'ecbl_bc:conveyor');
            } else if (block.west().hasTag("wood_connect") && block.above().east().hasTag("wood_connect") && block.above().east().permutation.getState("minecraft:cardinal_direction") == 'east' && block.west().permutation.getState("minecraft:cardinal_direction") == 'east') {
                setPermutation(block, "ecbl_bc:state", "diagonal_up", 'ecbl_bc:conveyor');
                setPermutation(block, "minecraft:cardinal_direction", 'east', 'ecbl_bc:conveyor');
            } else if (block.west().hasTag("wood_connect") && block.above().east().hasTag("wood_connect") && block.above().east().permutation.getState("minecraft:cardinal_direction") == 'west' && block.west().permutation.getState("minecraft:cardinal_direction") == 'west') {
                setPermutation(block, "ecbl_bc:state", "diagonal_down", 'ecbl_bc:conveyor');
                setPermutation(block, "minecraft:cardinal_direction", 'east', 'ecbl_bc:conveyor');
            } else {
                setPermutation(block, "ecbl_bc:state", "default");
            }
        }
    }
}