import { system, world, BlockPermutation, ItemStack, EntityComponentTypes, EquipmentSlot } from "@minecraft/server";
import { removeFromHeldItemStack } from "./helper";


system.beforeEvents.startup.subscribe((initEvent) => {
    initEvent.blockComponentRegistry.registerCustomComponent("oct_dogs:bowl", {
        onPlayerBreak: (event) => {
            const { block, dimension, brokenBlockPermutation } = event;
            const state = brokenBlockPermutation.getState("oct_dogs:bowl_contents");

            if (state == "food") {
                try { dimension.spawnItem(new ItemStack("oct_dogs:dog_food", 1), block.center()); } catch (e) { }
            }
        },
        onPlayerInteract: (event) => {
            const { player, block, dimension } = event;

            const state = block.permutation.getState("oct_dogs:bowl_contents");
            const cardinalDir = block.permutation.getState("minecraft:cardinal_direction");
            const itemStack = player.getComponent(EntityComponentTypes.Equippable).getEquipment(EquipmentSlot.Mainhand);

            system.run(() => {
                switch (state) {
                    case "empty":
                        if (!itemStack) {
                            break;
                        }
                        // Fill with Food
                        else if (itemStack.typeId == "oct_dogs:dog_food") {
                            block
                                .setPermutation(
                                    BlockPermutation.resolve(block.typeId,
                                        {
                                            "oct_dogs:bowl_contents": "food",
                                            "minecraft:cardinal_direction": cardinalDir
                                        }
                                    ));
                            removeFromHeldItemStack(player, itemStack, 1);
                            dimension.playSound("block.composter.fill", block.location);

                        }
                        // Fill with Water from Bucket
                        else if (itemStack.typeId == "minecraft:water_bucket") {
                            block
                                .setPermutation(
                                    BlockPermutation.resolve(block.typeId,
                                        {
                                            "oct_dogs:bowl_contents": "water",
                                            "minecraft:cardinal_direction": cardinalDir
                                        }
                                    ));
                            player
                                .getComponent("minecraft:inventory")
                                .container
                                .setItem(player.selectedSlotIndex, new ItemStack("minecraft:bucket", 1));

                            dimension.playSound("bucket.empty_water", block.location);
                        };
                        break;

                    case "water":
                        // Empty Water
                        if (!itemStack) {
                            block
                                .setPermutation(
                                    BlockPermutation.resolve(block.typeId,
                                        {
                                            "oct_dogs:bowl_contents": "empty",
                                            "minecraft:cardinal_direction": cardinalDir
                                        }
                                    ));

                            dimension.playSound("cauldron.takewater", block.location);
                        }
                        // Fill Empty Bucket
                        else if (itemStack.typeId == "minecraft:bucket") {
                            block
                                .setPermutation(
                                    BlockPermutation.resolve(block.typeId,
                                        {
                                            "oct_dogs:bowl_contents": "empty",
                                            "minecraft:cardinal_direction": cardinalDir
                                        }
                                    ));
                            player
                                .getComponent("minecraft:inventory")
                                .container
                                .setItem(player.selectedSlotIndex, new ItemStack("minecraft:water_bucket", 1));

                            dimension.playSound("bucket.fill_water", block.location);
                        };
                        break;

                    case "food":
                        // Take Food
                        if (!itemStack) {
                            block
                                .setPermutation(
                                    BlockPermutation.resolve(block.typeId,
                                        {
                                            "oct_dogs:bowl_contents": "empty",
                                            "minecraft:cardinal_direction": cardinalDir
                                        }
                                    ));
                            player
                                .getComponent("minecraft:inventory")
                                .container
                                .setItem(player.selectedSlotIndex, new ItemStack("oct_dogs:dog_food", 1));

                            dimension.playSound("block.composter.empty", block.location);
                        };
                        break;

                    default:
                        break;
                }
            })
        },
    });
});