import * as mc from "@minecraft/server"
import { isBlockId } from "./stableTriggers.js"
import { allEvents } from "./allEvents.js"

export function setPermutation(block, stateAdd, stateValue, blockId) {
    if (block && blockId) {
        const result = block.permutation.getAllStates();
        result[stateAdd] = stateValue;
        block.setPermutation(mc.BlockPermutation.resolve(blockId, result));
    }
}


export function baobab_branchPattern(block, carriageType, transporterType, blockId, reset) {
    if (!isBlockId(block, 'minecraft:air')) {
        setPermutation(block, `ecbl_bc:above`, (block.above()?.permutation.getState("ecbl_bc:below") == `allOutput` || block.above()?.permutation.getState("ecbl_bc:below") == `${carriageType}Output` || block.above()?.hasTag(`${carriageType}_output`)) ? transporterType :
            (block.above()?.permutation.getState("ecbl_bc:below") == `allInput` || block.above()?.permutation.getState("ecbl_bc:below") == `${carriageType}Input` || block.above()?.hasTag(`${carriageType}_input`)) ? transporterType :
                (block.above()?.hasTag(`${carriageType}_${transporterType}`)) ? transporterType : "none", blockId);

        setPermutation(block, `ecbl_bc:below`, (block.below()?.permutation.getState("ecbl_bc:above") == `allOutput` || block.below()?.permutation.getState("ecbl_bc:above") == `${carriageType}Output` || block.below()?.hasTag(`${carriageType}_output`)) ? transporterType :
            (block.below()?.permutation.getState("ecbl_bc:above") == `allInput` || block.below()?.permutation.getState("ecbl_bc:above") == `${carriageType}Input` || block.below()?.hasTag(`${carriageType}_input`)) ? transporterType :
                (block.below()?.hasTag(`${carriageType}_${transporterType}`)) ? transporterType : "none", blockId);

        setPermutation(block, `ecbl_bc:north`, (block.north()?.permutation.getState("ecbl_bc:south") == `allOutput` || block.north()?.permutation.getState("ecbl_bc:south") == `${carriageType}Output` || block.north()?.hasTag(`${carriageType}_output`)) ? transporterType :
            (block.north()?.permutation.getState("ecbl_bc:south") == `allInput` || block.north()?.permutation.getState("ecbl_bc:south") == `${carriageType}Input` || block.north()?.hasTag(`${carriageType}_input`)) ? transporterType :
                (block.north()?.hasTag(`${carriageType}_${transporterType}`)) ? transporterType : "none", blockId);

        setPermutation(block, `ecbl_bc:south`, (block.south()?.permutation.getState("ecbl_bc:north") == `allOutput` || block.south()?.permutation.getState("ecbl_bc:north") == `${carriageType}Output` || block.south()?.hasTag(`${carriageType}_output`)) ? transporterType :
            (block.south()?.permutation.getState("ecbl_bc:north") == `allInput` || block.south()?.permutation.getState("ecbl_bc:north") == `${carriageType}Input` || block.south()?.hasTag(`${carriageType}_input`)) ? transporterType :
                (block.south()?.hasTag(`${carriageType}_${transporterType}`)) ? transporterType : "none", blockId);

        setPermutation(block, `ecbl_bc:west`, (block.west()?.permutation.getState("ecbl_bc:east") == `allOutput` || block.west()?.permutation.getState("ecbl_bc:east") == `${carriageType}Output` || block.west()?.hasTag(`${carriageType}_output`)) ? transporterType :
            (block.west()?.permutation.getState("ecbl_bc:east") == `allInput` || block.west()?.permutation.getState("ecbl_bc:east") == `${carriageType}Input` || block.west()?.hasTag(`${carriageType}_input`)) ? transporterType :
                (block.west()?.hasTag(`${carriageType}_${transporterType}`)) ? transporterType : "none", blockId);

        setPermutation(block, `ecbl_bc:east`, (block.east()?.permutation.getState("ecbl_bc:west") == `allOutput` || block.east()?.permutation.getState("ecbl_bc:west") == `${carriageType}Output` || block.east()?.hasTag(`${carriageType}_output`)) ? transporterType :
            (block.east()?.permutation.getState("ecbl_bc:west") == `allInput` || block.east()?.permutation.getState("ecbl_bc:west") == `${carriageType}Input` || block.east()?.hasTag(`${carriageType}_input`)) ? transporterType :
                (block.east()?.hasTag(`${carriageType}_${transporterType}`)) ? transporterType : "none", blockId);
    }

    const scoreValue = mc?.world?.scoreboard?.getObjective("resetAll")?.getScore("world");
    if (scoreValue > 0 && block || reset)
        setPermutation(block, `ecbl_bc:${carriageType}_type`, "none", blockId);
}

//setar a tag
// setar id do bloco
// setar bone
mc.world.afterEvents.playerPlaceBlock.subscribe((data) => {
    let block = data.block
    if (block.hasTag("wood_connect")) {
        if (isBlockId(block, "ecbl_bc:baobab_branch_block"))
        if (isBlockId(block.above(), "ecbl_bc:baobab_branch_block")) baobab_branchPattern(block.above(), "rf", "baobab_branch", 'ecbl_bc:baobab_branch_block')
        if (isBlockId(block.below(), "ecbl_bc:baobab_branch_block")) baobab_branchPattern(block.below(), "rf", "baobab_branch", 'ecbl_bc:baobab_branch_block')
        if (isBlockId(block.north(), "ecbl_bc:baobab_branch_block")) baobab_branchPattern(block.north(), "rf", "baobab_branch", 'ecbl_bc:baobab_branch_block')
        if (isBlockId(block.south(), "ecbl_bc:baobab_branch_block")) baobab_branchPattern(block.south(), "rf", "baobab_branch", 'ecbl_bc:baobab_branch_block')
        if (isBlockId(block.west(), "ecbl_bc:baobab_branch_block")) baobab_branchPattern(block.west(), "rf", "baobab_branch", 'ecbl_bc:baobab_branch_block')
        if (isBlockId(block.east(), "ecbl_bc:baobab_branch_block")) baobab_branchPattern(block.east(), "rf", "baobab_branch", 'ecbl_bc:baobab_branch_block')
    }
})
mc.world.beforeEvents.playerBreakBlock.subscribe((data) => {
    let player = data.player;
    let block = data.block
    mc.system.run(() => {
        if (isBlockId(block.above(), "ecbl_bc:baobab_branch_block")) baobab_branchPattern(block.above(), "rf", "baobab_branch", 'ecbl_bc:baobab_branch_block')
        if (isBlockId(block.below(), "ecbl_bc:baobab_branch_block")) baobab_branchPattern(block.below(), "rf", "baobab_branch", 'ecbl_bc:baobab_branch_block')
        if (isBlockId(block.north(), "ecbl_bc:baobab_branch_block")) baobab_branchPattern(block.north(), "rf", "baobab_branch", 'ecbl_bc:baobab_branch_block')
        if (isBlockId(block.south(), "ecbl_bc:baobab_branch_block")) baobab_branchPattern(block.south(), "rf", "baobab_branch", 'ecbl_bc:baobab_branch_block')
        if (isBlockId(block.west(), "ecbl_bc:baobab_branch_block")) baobab_branchPattern(block.west(), "rf", "baobab_branch", 'ecbl_bc:baobab_branch_block')
        if (isBlockId(block.east(), "ecbl_bc:baobab_branch_block")) baobab_branchPattern(block.east(), "rf", "baobab_branch", 'ecbl_bc:baobab_branch_block')
        if (mc.world.getDynamicProperty(`${block.location.x},${block.location.y},${block.location.z}`) == block.dimension.id) {
            mc.world.setDynamicProperty(`${block.location.x},${block.location.y},${block.location.z}`, '')
        }
    
    }
)})