import * as mc from "@minecraft/server";
import { randomWholeNum } from "../sgs_farm_lib";
import { vanillaBlocks } from "../crops/sgs_farm_cropconfig";

const HARVEST_WITH_SICKLE = "mob.sheep.shear";
const SOUND_OPTIONS = { pitch: 1.0, volume: 5.0, };

export class sickleManager {

    static onUseOn(data) {

        const player = data.source
        const block = data.block;

        const cropData = vanillaBlocks.find((f) => f.blockID == block.typeId);
        if (cropData == undefined) return;
        this.sickle(block, cropData, player);
    }

    static sickle(block, data, player) {
        let setStage = 0;
        player.playSound(HARVEST_WITH_SICKLE, SOUND_OPTIONS);
        block.setPermutation(block.permutation.withState(data.stateID, setStage));
        if (data.height == 2) block.above(1).setPermutation(mc.BlockPermutation.resolve("minecraft:air"));

        let amount = 1;
        if(data.amount != undefined) amount = data.amount;
        const num = randomWholeNum(1, amount);

        const dropItem = new mc.ItemStack(data.cropID, num);
        const { x, y, z } = block.location;
        block.dimension.spawnItem(dropItem, { x: x + 0.5, y: y + 0.5, z: z + 0.5 });
    }
}