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

const SEED_PROCESSING = "random.pop";
const SOUND_OPTIONS = { pitch: 1.0, volume: 5.0, };

const grassBlocks = ["minecraft:short_grass", "minecraft:tall_grass", "minecraft:fern", "minecraft:large_fern"];

dropWildSeed();

export class seedhandlerManager {

    static interactProcessor(data) {
        const block = data.block;
        const player = data.player;
        const mainhand = player.getComponent(mc.EntityEquippableComponent.componentId).getEquipmentSlot(mc.EquipmentSlot.Mainhand);
        const heldItem = mainhand.getItem();
        if (heldItem == undefined) return;

        if (heldItem.typeId == "sgs_farm:wild_seed") {
            const seedItems = cropsCombined();
            const dropItem = new mc.ItemStack(seedItems[Math.floor(Math.random() * seedItems.length)].seedID, 1);
            const { x, y, z } = block.location;
            block.dimension.spawnItem(dropItem, { x: x + 0.5, y: y + 1.0, z: z + 0.5 });

            player.playSound(SEED_PROCESSING, SOUND_OPTIONS);
            block.dimension.spawnParticle("minecraft:crop_growth_emitter", block.center());

            if (player.getGameMode() == mc.GameMode.survival) mainhand.setItem(itemManager.reduceAmount(heldItem, 1));
        }
        else
        {
            player.onScreenDisplay.setActionBar("§cOnly works with §oHarvestCraft§c Wild Seed");
            //player.sendMessage("§c[Seed Processor] Only works with Wild Seed");
        }
    }

    static interactRecycler(data) {
        const block = data.block;
        const player = data.player;

        const mainhand = player.getComponent(mc.EntityEquippableComponent.componentId).getEquipmentSlot(mc.EquipmentSlot.Mainhand);
        const heldItem = mainhand.getItem();
        if (heldItem == undefined) return;

        const seedItems = cropsCombined();
        const seedItem = seedItems.find((f) => f.cropID == heldItem.typeId);
        if (seedItem == undefined){
            player.onScreenDisplay.setActionBar("§cOnly works with §oHarvestCraft§c Crops");
            //player.sendMessage("§c[Seed Recycler] Only works with §oHarvestCraft§c Crops");
            return;
        } 

        if (heldItem.amount < 4){
            player.onScreenDisplay.setActionBar("§cUse at least 4 Crops");
            //player.sendMessage("§c[Seed Recycler] Use at least 4 Crops");
            return;
        }

        const dropItem = new mc.ItemStack(seedItems[Math.floor(Math.random() * seedItems.length)].seedID, 1);
        const { x, y, z } = block.location;
        block.dimension.spawnItem(dropItem, { x: x + 0.5, y: y + 1.0, z: z + 0.5 });

        player.playSound(SEED_PROCESSING, SOUND_OPTIONS);
        block.dimension.spawnParticle("minecraft:crop_growth_emitter", block.center());

        if (player.getGameMode() == mc.GameMode.survival) mainhand.setItem(itemManager.reduceAmount(heldItem, 4));
    }
}

function dropWildSeed() {
    mc.world.afterEvents.playerBreakBlock.subscribe((eventData) => {
        const block = eventData.block;
        const permutation = eventData.brokenBlockPermutation;

        if (!grassBlocks.includes(permutation.type.id)) return;
        //if(!permutation.hasTag("plant")) return;
        const num = randomWholeNum(0, 1);
        if (num == 0) {
            block.dimension.spawnItem(new mc.ItemStack("sgs_farm:wild_seed", 1), block.center());
            block.dimension.spawnParticle("sgs_farm:harvest_crop", block.center());
        }
    })
}