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

//set block fence tag here
const fenceTag = 'rns:bush'

export class bush_Manager {
    static update_Fence_States(Fence) {
        let north = undefined;
        try {
            north = Fence.north(1);
        } catch { }
        let south = undefined;
        try {
            south = Fence.south(1);
        } catch { }
        let east = undefined;
        try {
            east = Fence.east(1);
        } catch { }
        let west = undefined;
        try {
            west = Fence.west(1);
        } catch { }
        const blocks = [
            { block: north, side: "north" },
            { block: south, side: "south" },
            { block: east, side: "east" },
            { block: west, side: "west" },
        ];
        for (const blockData of blocks) {
            if (blockData.block != undefined) {
                if (blockData.block.hasTag(fenceTag)) {
                    Fence.setPermutation(Fence.permutation.withState("rns:" + blockData.side, 1));
                } else {
                    Fence.setPermutation(Fence.permutation.withState("rns:" + blockData.side, 0));
                }
            } else {
                Fence.setPermutation(Fence.permutation.withState("rns:" + blockData.side, 0));
            }
        }
    }
    static updateBushesAround(Block) {
        let north = undefined;
        try {
            north = Block.north(1);
        } catch { }
        let south = undefined;
        try {
            south = Block.south(1);
        } catch { }
        let east = undefined;
        try {
            east = Block.east(1);
        } catch { }
        let west = undefined;
        try {
            west = Block.west(1);
        } catch { }
        const blocks = [Block, north, south, east, west];
        for (const block of blocks) {
            if (block != undefined) {
                if (block.hasTag(fenceTag)) this.update_Fence_States(block);
            }
        }
    }
}


const branchTag = 'rns:branch'
export class branch_Manager {
    static update_Branch_States(Fence) {
        let north = undefined;
        try {
            north = Fence.north(1);
        } catch { }
        let south = undefined;
        try {
            south = Fence.south(1);
        } catch { }
        let east = undefined;
        try {
            east = Fence.east(1);
        } catch { }
        let west = undefined;
        try {
            west = Fence.west(1);
        } catch { }
        const blocks = [
            { block: north, side: "north" },
            { block: south, side: "south" },
            { block: east, side: "east" },
            { block: west, side: "west" },
        ];
        for (const blockData of blocks) {
            if (!Fence.hasTag('rns:cantupdate')) {
                if (blockData.block != undefined) {
                    if (!(blockData.block.isLiquid || blockData.block.isAir)) {
                        Fence.setPermutation(Fence.permutation.withState("rns:" + blockData.side, 1));
                    } else {
                        Fence.setPermutation(Fence.permutation.withState("rns:" + blockData.side, 0));
                    }
                } else {
                    Fence.setPermutation(Fence.permutation.withState("rns:" + blockData.side, 0));
                }
            }
        }

    }
    static updateBranchesAround(Block) {
        let north = undefined;
        try {
            north = Block.north(1);
        } catch { }
        let south = undefined;
        try {
            south = Block.south(1);
        } catch { }
        let east = undefined;
        try {
            east = Block.east(1);
        } catch { }
        let west = undefined;
        try {
            west = Block.west(1);
        } catch { }
        const blocks = [Block, north, south, east, west];
        for (const block of blocks) {
            if (block != undefined) {
                if (block.hasTag(branchTag)) this.update_Branch_States(block);
            }
        }
    }
}

const cactusTag = 'rns:cactus'
export class cactus_Manager {
    static update_Cactus_States(Fence) {
        let north = undefined;
        try {
            north = Fence.north(1);
        } catch { }
        let south = undefined;
        try {
            south = Fence.south(1);
        } catch { }
        let east = undefined;
        try {
            east = Fence.east(1);
        } catch { }
        let west = undefined;
        try {
            west = Fence.west(1);
        } catch { }
        let up = undefined;
        try {
            up = Fence.above(1);
        } catch { }
        let down = undefined;
        try {
            down = Fence.below(1);
        } catch { }
        const blocks = [
            { block: north, side: "north" },
            { block: south, side: "south" },
            { block: east, side: "east" },
            { block: west, side: "west" },
            { block: up, side: "up" },
            { block: down, side: "down" },
        ];
        for (const blockData of blocks) {
            if (!Fence.hasTag('rns:cantupdate')) {
                if (blockData.block != undefined) {
                    if (!(blockData.block.isLiquid || blockData.block.isAir || blockData.block.typeId == 'minecraft:structure_void')) {
                        Fence.setPermutation(Fence.permutation.withState("rns:" + blockData.side, 1));
                    } else {
                        Fence.setPermutation(Fence.permutation.withState("rns:" + blockData.side, 0));
                    }
                } else {
                    Fence.setPermutation(Fence.permutation.withState("rns:" + blockData.side, 0));
                }
            }
        }

    }
    static updateCactusAround(Block) {
        let north = undefined;
        try {
            north = Block.north(1);
        } catch { }
        let south = undefined;
        try {
            south = Block.south(1);
        } catch { }
        let east = undefined;
        try {
            east = Block.east(1);
        } catch { }
        let west = undefined;
        try {
            west = Block.west(1);
        } catch { }
        let up = undefined;
        try {
            up = Block.above(1);
        } catch { }
        let down = undefined;
        try {
            down = Block.below(1);
        } catch { }
        const blocks = [Block, north, south, east, west, up, down];
        for (const block of blocks) {
            if (block != undefined) {
                if (block.hasTag(cactusTag)) this.update_Cactus_States(block);
            }
        }
    }
}

