import * as Minecraft from "@minecraft/server"

const directions = [
    { block: (block, i) => block.north(i), direction: "north", opposite: "south" },
    { block: (block, i) => block.south(i), direction: "south", opposite: "north" },
    { block: (block, i) => block.east(i), direction: "east", opposite: "west" },
    { block: (block, i) => block.west(i), direction: "west", opposite: "east" },
    { block: (block, i) => block.above(i), direction: "above", opposite: "below" },
    { block: (block, i) => block.below(i), direction: "below", opposite: "above" }
]


/** @type {Minecraft.BlockCustomComponent} */
const LeavesBlockComponent = {
    onPlayerBreak(event) {
        if (event.player?.getGameMode() == Minecraft.GameMode.Creative) {
            return
        }


        const handItem = event.player?.getComponent(Minecraft.EntityComponentTypes.Equippable)?.getEquipment(Minecraft.EquipmentSlot.Mainhand)

        if (handItem?.typeId == "minecraft:shears") {
            event.dimension.spawnItem(new Minecraft.ItemStack("aria_ah:dark_leaves", 1), event.block.center())
        } else {
            const dropChance = Math.floor(Math.random() * 199) + 1

            if (dropChance <= 10) {
                event.dimension.spawnItem(new Minecraft.ItemStack("aria_ah:dark_sapling", 1), event.block.center())
            } else if (dropChance <= 14) {
                event.dimension.spawnItem(new Minecraft.ItemStack("aria_ah:stick", Math.floor(Math.random() * 1) + 1), event.block.center())
            } else if (dropChance <= 15) {
                event.dimension.spawnItem(new Minecraft.ItemStack("aria_ah:apple", 1), event.block.center())
            }
        }
    },

    onPlace(event) {
        const belowBlock = event.block.below()

        if (Math.random() <= .35 && belowBlock.isAir && event.block.below(2).isAir) {
            belowBlock.setType(`aria_ah:slime`)
            belowBlock.setPermutation(belowBlock.permutation.withState("aria_ah:type", Math.floor(Math.random() * 3)))
        }
    }
/*
    onRandomTick(event) {
        if (event.block.isAir || !event.block.permutation.getState("aria_ah:should_decay")) {
            return
        }

        let highestDecayTier = 0

        for (const direction of directions) {
            const blockToCheck = direction.block(event.block, 1)

            if (blockToCheck?.typeId == "aria_ah:dark_log") {
                highestDecayTier = 4
            } else if (blockToCheck?.typeId == "aria_ah:dark_leaves") {
                const decayTier = ((blockToCheck?.permutation?.getState("aria_ah:decay_tier") ?? 4) - 1)
                
                if (decayTier > highestDecayTier) {
                    highestDecayTier = decayTier
                }
            }
        }

        if (highestDecayTier > 0) {
            event.block.setPermutation(Minecraft.BlockPermutation.resolve(event.block.typeId, { "aria_ah:decay_tier": highestDecayTier }))
        } else {
            event.block.setType("minecraft:air")
        }
    },

    beforeOnPlayerPlace(event) {
        Minecraft.system.run(() => {
            event.block.setPermutation(event.block.permutation.withState("aria_ah:should_decay", false))
        })
    }*/
}

    
/** @type {Minecraft.BlockCustomComponent} */
const SaplingBlockComponent = {
    onTick(event) {
        SaplingBlockComponent.generateTree(event.block)
    },

    onPlayerInteract(event) {
        const equippable = event.player?.getComponent(Minecraft.EntityComponentTypes.Equippable)
        const handItem = equippable?.getEquipment(Minecraft.EquipmentSlot.Mainhand)

        if (handItem?.typeId == "aria_ah:bone_meal") {
            SaplingBlockComponent.generateTree(event.block)

            if (event.player?.getGameMode() != Minecraft.GameMode.Creative) {
                if (handItem.amount > 1) {
                    handItem.amount--
                    equippable?.setEquipment(Minecraft.EquipmentSlot.Mainhand, handItem)
                } else {
                    equippable?.setEquipment(Minecraft.EquipmentSlot.Mainhand)
                }
            }

            const effectLocation = event.block.center()
            event.dimension.playSound("item.bone_meal.use", effectLocation)
        }
    },

    /**
     * 
     * @param {Minecraft.Block} middleBlock 
     */
    generateTree(middleBlock) {
        let allAir = true
        const aboveThree = middleBlock.above(3)

        if (!middleBlock.above(5).isAir || !aboveThree.isAir || !aboveThree.north(2).isAir || !aboveThree.east(2).isAir || !aboveThree.south(2).isAir || !aboveThree.east(2).isAir) {
            allAir = false
        }

        if (allAir) {
            Minecraft.world.structureManager.place("aria_ah:dark_tree", middleBlock.dimension, { x: middleBlock.location.x - 2, y: middleBlock.location.y, z: middleBlock.location.z - 2 })
        }
    }
}

Minecraft.system.beforeEvents.startup.subscribe((event) => {
    event.blockComponentRegistry.registerCustomComponent("aria_ah:leaves", LeavesBlockComponent)
    event.blockComponentRegistry.registerCustomComponent("aria_ah:sapling", SaplingBlockComponent)
})
