import * as mc from "@minecraft/server";

export class planterManager {
    static beforeplace(data) {
        const block = data.block;
        const blockId = data.permutationToPlace.type.id;

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

        const northConnects = (north.hasTag("sgs_farm_planter") && north.type.id == blockId);
        const eastConnects = (east.hasTag("sgs_farm_planter") && east.type.id == blockId);
        const southConnects = (south.hasTag("sgs_farm_planter") && south.type.id == blockId);
        const westConnects = (west.hasTag("sgs_farm_planter") && west.type.id == blockId);

        mc.system.runTimeout(() => {
            if (northConnects) north.setPermutation(north.permutation.withState('sgs_farm:south', 1));
            if (eastConnects) east.setPermutation(east.permutation.withState('sgs_farm:west', 1));
            if (southConnects) south.setPermutation(south.permutation.withState('sgs_farm:north', 1));
            if (westConnects) west.setPermutation(west.permutation.withState('sgs_farm:east', 1));
        }, 0)

        data.permutationToPlace = mc.BlockPermutation.resolve(data.permutationToPlace.type.id, {
            'sgs_farm:irrigation': 0,
            'sgs_farm:north': northConnects ? 1 : 0,
            'sgs_farm:south': southConnects ? 1 : 0,
            'sgs_farm:east': eastConnects ? 1 : 0,
            'sgs_farm:west': westConnects ? 1 : 0
        });
    }

    static destroy(data) {

        const northConnects = data.destroyedBlockPermutation.getState("sgs_farm:north");
        const eastConnects = data.destroyedBlockPermutation.getState("sgs_farm:east");
        const southConnects = data.destroyedBlockPermutation.getState("sgs_farm:south");
        const westConnects = data.destroyedBlockPermutation.getState("sgs_farm:west");

        const blockId = data.destroyedBlockPermutation.type.id;

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

        if (northConnects && north.hasTag("sgs_farm_planter")) north.setPermutation(north.permutation.withState('sgs_farm:south', 0));
        if (eastConnects && east.hasTag("sgs_farm_planter")) east.setPermutation(east.permutation.withState('sgs_farm:west', 0));
        if (southConnects && south.hasTag("sgs_farm_planter")) south.setPermutation(south.permutation.withState('sgs_farm:north', 0));
        if (westConnects && west.hasTag("sgs_farm_planter")) west.setPermutation(west.permutation.withState('sgs_farm:east', 0));
    }

    static tick(block) {
        try {
            if (block.isAir) return;

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

            const northConnects = (north.hasTag("sgs_farm_fence"));
            const eastConnects = (east.hasTag("sgs_farm_fence"));
            const southConnects = (south.hasTag("sgs_farm_fence"));
            const westConnects = (west.hasTag("sgs_farm_fence"));

            block.setPermutation(block.permutation.withState('sgs_farm:fence_north', northConnects ? 1 : 0));
            block.setPermutation(block.permutation.withState('sgs_farm:fence_south', southConnects ? 1 : 0));
            block.setPermutation(block.permutation.withState('sgs_farm:fence_east', eastConnects ? 1 : 0));
            block.setPermutation(block.permutation.withState('sgs_farm:fence_west', westConnects ? 1 : 0));
        }
        catch (error) { };
    };
}
