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

const doubleBlocks = ["sgs_farm:window_farm_painted_white_d",
    "sgs_farm:window_farm_oak_d",
    "sgs_farm:window_farm_spruce_d",
    "sgs_farm:window_farm_birch_d",
    "sgs_farm:window_farm_jungle_d",
    "sgs_farm:window_farm_acacia_d",
    "sgs_farm:window_farm_dark_oak_d",
    "sgs_farm:window_farm_mangrove_d",
    "sgs_farm:window_farm_cherry_d",
    "sgs_farm:window_farm_bamboo_d"
];

export class windowManager {

    static beforeplace(data) {
        const block = data.block;
        const top = block.above();

        mc.system.runTimeout(() => {
            if (top.isAir) top.setPermutation(mc.BlockPermutation.resolve(data.permutationToPlace.type.id, { 'minecraft:cardinal_direction': data.permutationToPlace.getState("minecraft:cardinal_direction"), 'sgs_farm:is_upper': 1 }));
        }, 0)

        data.permutationToPlace = mc.BlockPermutation.resolve(data.permutationToPlace.type.id, { 'minecraft:cardinal_direction': data.permutationToPlace.getState("minecraft:cardinal_direction"), 'sgs_farm:is_upper': 0 });
    }

    static placed(data) {
        const block = data.block;
        if (!block.above().hasTag("sgs_farm_window") && block.permutation.getState("sgs_farm:is_upper") == 0) {
            block.dimension.runCommandAsync(`setblock ${block.x} ${block.y} ${block.z} air [] destroy`);
        }
    }

    static destroy(data) {
        const block = data.block;
        const below = block.below();

        if (below.hasTag("sgs_farm_window")) {
            if (below.permutation.getState("sgs_farm:is_upper") == 0) {
                below.dimension.runCommandAsync(`setblock ${below.x} ${below.y} ${below.z} air [] destroy`);
            }

        }
    }
}
