import { EquipmentSlot, EntityEquippableComponent, GameMode } from '@minecraft/server';
import { itemManager } from '../sgs_farm_lib';
import * as mc from "@minecraft/server";
import { cropBlocks } from './sgs_farm_cropconfig';

export class frameManager {
    static interact(block, player) {
        //get the crop data

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

        const cropdata = cropBlocks.find((f) => f.seedID == heldItem.typeId);
        if (cropdata == undefined) return;

        if (cropdata.type == "aframe" && block.typeId == "sgs_farm:a_frame") {
            block.setPermutation(mc.BlockPermutation.resolve(cropdata.blockID, { "sgs_farm:growth": 0, "minecraft:cardinal_direction": block.permutation.getState("minecraft:cardinal_direction") }));

            if (player.getGameMode() != mc.GameMode.creative) {
                mainhand.setItem(itemManager.reduceAmount(heldItem, 1));
            }
            return;
        }

        if (cropdata.type == "pframe" && block.typeId == "sgs_farm:p_frame") {
            block.setPermutation(mc.BlockPermutation.resolve(cropdata.blockID, { "sgs_farm:growth": 0 }));

            if (player.getGameMode() != mc.GameMode.creative) {
                mainhand.setItem(itemManager.reduceAmount(heldItem, 1));
            }
            return;
        }
    }
}
