import { Block, Dimension, Direction, ItemStack, Player } from "@minecraft/server";

/**
 * Evento onPlayerInteract del componente fluidConverterComponent
 * Addon: Electrical Creativity BP
 * @param {{face: Direction, faceLocation: Vector3, player: Player, block: Block, dimension: Dimension}} event
 */
export const fluidConverterOnPlayerInteractEvent = ({ face, faceLocation, player, block, dimension }) => {
    try {
        const power = block.permutation.getState("nps_ec:power");
        const charged = block.permutation.getState('nps_ec:charged');
        if (!power || charged) return;


        const handItem = player.getComponent("inventory").container.getItem(player.selectedSlotIndex)

        let permutation = block.permutation;

        const bucket = new ItemStack("minecraft:bucket", 1)
        

        if (handItem != undefined) {
            if (handItem.typeId === "minecraft:water_bucket") {
                player.runCommand(`playsound bucket.empty_water @a ~ ~ ~`)
                permutation = permutation.withState("nps_ec:type", "water");
            } else if (handItem.typeId === "minecraft:lava_bucket") {
                player.runCommand(`playsound bucket.empty_lava @a ~ ~ ~`)
                permutation = permutation.withState("nps_ec:type", "lava");
            }
            else if (handItem.typeId === "minecraft:milk_bucket") {
                player.runCommand(`playsound bucket.empty_water @a ~ ~ ~`)
                permutation = permutation.withState("nps_ec:type", "milk");
            } else {
                return
            }

            
            player.getComponent("inventory").container.setItem(player.selectedSlotIndex, bucket);

            permutation = permutation.withState("nps_ec:charged", true);

            block.setPermutation(permutation);
        }
    } catch { }
}