import * as mc from "@minecraft/server";
import { highlightBlock, itemManager } from "../sgs_cake_lib";

export const INTERACT_ITEMS = [
    {
        id: "minecraft:bucket",
        return_id: "minecraft:water_bucket"
    },
    {
        id: "minecraft:glass_bottle",
        return_id: "minecraft:potion"
    },
    {
        id: "sgs_cake:bottle",
        return_id: "sgs_cake:water_bottle"
    },
    {
        id: "minecraft:water_bucket",
        return_id: "minecraft:bucket"
    },
    {
        id: "minecraft:potion",
        return_id: "minecraft:glass_bottle"
    },
    {
        id: "sgs_cake:water_bottle",
        return_id: "sgs_cake:bottle"
    }
]

export const INTERACT_ITEMS_MILK = [
    {
        id: "minecraft:bucket",
        return_id: "minecraft:milk_bucket"
    },
    {
        id: "sgs_cake:bottle",
        return_id: "sgs_cake:milk_bottle"
    },
    {
        id: "minecraft:milk_bucket",
        return_id: "minecraft:bucket"
    },
    {
        id: "sgs_cake:milk_bottle",
        return_id: "sgs_cake:bottle"
    }
]

export class waterBarrelManager {

    static interact(data) {
        const block = data.block;
        const player = data.player;
        const location = block.bottomCenter();

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

        const itemdata = INTERACT_ITEMS.find((f) => f.id == heldItem.typeId);
        if (itemdata == undefined) return;

        mc.system.runTimeout(() => {
            const dropItem = new mc.ItemStack(itemdata.return_id, 1);
            const itemEntity = block.dimension.spawnItem(dropItem, player.location);
            itemEntity.clearVelocity();
        }, 1)

        block.dimension.spawnParticle("sgs_cake:water_splash_instant", location);
        block.dimension.playSound("bucket.fill_water", location);
        if (player.getGameMode() != mc.GameMode.Creative) mainhand.setItem(itemManager.reduceAmount(heldItem, 1));
        return;
    }

    static interactMilk(data) {
        const block = data.block;
        const player = data.player;
        const location = block.bottomCenter();

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

        const itemdata = INTERACT_ITEMS_MILK.find((f) => f.id == heldItem.typeId);
        if (itemdata == undefined) return;

        mc.system.runTimeout(() => {
            const dropItem = new mc.ItemStack(itemdata.return_id, 1);
            const itemEntity = block.dimension.spawnItem(dropItem, player.location);
            itemEntity.clearVelocity();
        }, 1)

        block.dimension.spawnParticle("sgs_cake:milk_splash_instant", location);
        block.dimension.playSound("bucket.fill_water", location);
        if (player.getGameMode() != mc.GameMode.Creative) mainhand.setItem(itemManager.reduceAmount(heldItem, 1));
        return;
    }
}
