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


// 			bonemeal foo

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('lv_zo: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('lv_zo:corrupted_leaves_drops', {
        onPlayerDestroy: e => {
            const block = e.block
            const item = e.player.getComponent('equippable').getEquipment('Mainhand')
            const player = e.player
            
            let gameMode;
            try {
                gameMode = player.getGameMode();
            } catch (error) {
                console.warn("Failed to get player game mode:", error);
                return;
            }
    
            // Skip logic if in Creative mode
            if (gameMode === "creative") return;

            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 "lv_zo/corrupted_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 "lv_zo/corrupted_leaves"')
            }

        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('lv_zo: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('lv_zo:replaceable')) {
            }
            else {
                break;
            }
        case false:
            if (item?.typeId == "minecraft:bone_meal" && block.hasTag('lv_zo: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() * 3)
        if (block.typeId == "lv_zo:corrupted_sapling") {
            tree_grow(block, num, "lv_zo:corrupted", 2, 2)
        }
    }
}


function tree_grow(block, num, type, offsetx, offsetz) {
    if (num == 3) {
        block.dimension.runCommandAsync("structure load " + type + "_tree_2 " + 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 + "_tree_1 " + 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 + "_tree_0 " + JSON.stringify(block.location.x - offsetx) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z - offsetz) + " 270_degrees")
    }
}
