import { world, BlockPermutation, system } from "@minecraft/server";
import { flowers } from "../flower_pot/flower_pot_additions";

system.beforeEvents.startup.subscribe((initEvent) => {
    initEvent.blockComponentRegistry.registerCustomComponent("rustic:crop_grow", {
        onRandomTick: e => {
            const { block, dimension } = e;

            if (block.hasTag("maxStage")) return;

            const farmland = surroundingFarmland(block);

            const growChanceData = growChance.find(data => data.nFarmland === farmland);

            if (!growChanceData) return;

            if ( (Math.random() * 100) > growChanceData.probability ) return;

            if (block.hasTag("preMaxStage")) {
                if (block.typeId == "rustic:apple_seeds_tree")block.setType("rustic:apple_sapling")
                else if (block.typeId == "rustic:olive_seed")block.setType("rustic:olive_sapling")
                return;
            }
            else if (flowers[block.typeId]) {
                const herbToPlace = block.typeId + '_grow'
                block.setType(herbToPlace)
                return;
            }
            const growState = block.permutation.getState("r:growStage");

            block.setPermutation(block.permutation.withState("r:growStage", growState + 1));
        },
        onPlayerInteract: e => {
            const { player, block, dimension, face, faceLocation } = e;
            const item = player.getComponent('inventory').container.getItem(player.selectedSlotIndex);

            if (item == undefined) return;

            if (item.typeId == 'minecraft:bone_meal'){

                if (block.hasTag("maxStage")) return;

                block.dimension.spawnParticle("minecraft:crop_growth_emitter", block.center());
                block.dimension.playSound("item.bone_meal.use", block.center());
                const equipment = player.getComponent('equippable');
                if (player.getGameMode() !== "creative") {
                    if (item.amount > 1) {
                        item.amount -= 1;
                        equipment.setEquipment('Mainhand', item);
                    } else if (item.amount === 1) {
                        equipment.setEquipment('Mainhand', undefined);
                    }
                }
                
                if ( (Math.random() * 100) > 80 ) return;

                if (block.hasTag("preMaxStage")) {
                    if (block.typeId == "rustic:apple_seeds_tree")block.setType("rustic:apple_sapling")
                    else if (block.typeId == "rustic:olive_seed")block.setType("rustic:olive_sapling")
                    return;
                }
                if ( flowers[block.typeId] ){
                    const herbToPlace = block.typeId + '_grow'
                    block.setType(herbToPlace)
                    return;
                }
                const growState = block.permutation.getState("r:growStage");
                block.setPermutation(block.permutation.withState("r:growStage", growState + 1));
            }
        },
    });
});


export const growChance =[
    { nFarmland: 0, probability: 14 },
    { nFarmland: 1, probability: 16 },
    { nFarmland: 2, probability: 20 },
    { nFarmland: 3, probability: 20 },
    { nFarmland: 4, probability: 25 },
    { nFarmland: 5, probability: 25 },
    { nFarmland: 6, probability: 33 },
    { nFarmland: 7, probability: 33 },
    { nFarmland: 8, probability: 33 },
]

export function surroundingFarmland(block){
    let farmlandCounter = 0
    if (block.below().north().typeId === "minecraft:farmland") farmlandCounter++;
    if (block.below().south().typeId === "minecraft:farmland") farmlandCounter++;
    if (block.below().east().typeId === "minecraft:farmland") farmlandCounter++;
    if (block.below().west().typeId === "minecraft:farmland") farmlandCounter++;
    if (block.below().north().east().typeId === "minecraft:farmland") farmlandCounter++;
    if (block.below().north().west().typeId === "minecraft:farmland") farmlandCounter++;
    if (block.below().south().east().typeId === "minecraft:farmland") farmlandCounter++;
    if (block.below().south().west().typeId === "minecraft:farmland") farmlandCounter++;

    return farmlandCounter
}