import { world, system, ItemStack, BlockPermutation, MolangVariableMap } from "@minecraft/server";
import { decrementstack } from "./template_components.js";


// 			bonemeal foo

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:bonemealable_sapling', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            bonemeal(player, block, item, undefined, false)
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:template_leaves_drops', {
        onPlayerDestroy: e => {
            const block = e.block
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            const player = e.player
            if (item?.typeId == "minecraft:shears") {
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/trees/template/template_leaves_sheared"')
            }
            if (!(item?.typeId == "minecraft:shears")) {
                player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/trees/template/template_leaves"')
            }

        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:grow_sapling', {
        onRandomTick: e => {
            const block = e.block
            grow_sapling(block)
        },
    });
});


//						functions


function bonemeal(player, block, item, max_growth, should_check_air, how_high) {
    if (how_high == undefined) {
        how_high = 0
    }
    switch (should_check_air) {
        case true:
            if (block.above(how_high).isAir || block.above(how_high).hasTag('bumble:replaceable')) {
            }
            else {
                break;
            }
        case false:
            if (item?.typeId == "minecraft:bone_meal" && block.hasTag('bumble:sapling')) {

                let num = Math.round(Math.random() * 10)
                if (num < 1) {
                    grow_sapling(block)
                }
                block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
                block.dimension.playSound('item.bone_meal.use', block.center())
                decrementstack(item, player)
                break;
            }
    }
}


function grow_sapling(block) {
    //let num = Math.round(Math.random() * 2)
    let num = 0
    if (num < 1) {
		let num = Math.round(Math.random() * 4)
        if (block.typeId == "bumble:template_sapling") {
            tree_grow(block, num, "template", 2, 2)
        }
    }
}


function tree_grow(block, num, type, offsetx, offsetz) {
    if (num == 4) {
        block.dimension.runCommandAsync("structure load " + type + "_tree1 " + JSON.stringify(block.location.x - offsetx) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - offsetz) + " 0_degrees")
    }
    if (num == 3) {
        block.dimension.runCommandAsync("structure load " + type + "_tree1 " + JSON.stringify(block.location.x - offsetx) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - offsetz) + " 90_degrees")
    }
    if (num == 2) {
        block.dimension.runCommandAsync("structure load " + type + "_tree1 " + JSON.stringify(block.location.x - offsetx) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - offsetz) + " 180_degrees")
    }
    if (num <= 1) {
        block.dimension.runCommandAsync("structure load " + type + "_tree1 " + JSON.stringify(block.location.x - offsetx) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - offsetz) + " 270_degrees")
    }
}
