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

const a_frame_items = ["sgs_farm:tomato_cherry_seed", "sgs_farm:tomato_beef_seed", "sgs_farm:peas_garden_seed", "sgs_farm:beans_green_seed", "sgs_farm:cucumber_seed", "sgs_farm:eggplant_seed"];
const p_frame_items = ["sgs_farm:peppers_bell_seed", "sgs_farm:peppers_chili_seed", "sgs_farm:blackcurrants_seed", "sgs_farm:redcurrants_seed"];

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 a_data = a_frame_items.find((i) => i == heldItem.typeId);
        if (a_data != undefined)
        {
            block.setPermutation(mc.BlockPermutation.resolve(heldItem.typeId.substring(0, heldItem.typeId.length - 5), { "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));
            }
        }

        const p_data = p_frame_items.find((i) => i == heldItem.typeId);
        if (p_data != undefined)
        {
            block.setPermutation(mc.BlockPermutation.resolve(heldItem.typeId.substring(0, heldItem.typeId.length - 5), { "sgs_farm:growth": 0}));
            
            if (player.getGameMode() != mc.GameMode.creative) {
                mainhand.setItem(itemManager.reduceAmount(heldItem, 1));
            }
        }
    }
}
