import { world, EquipmentSlot, EntityEquippableComponent, system } from '@minecraft/server';

const turn_on = {
    onPlayerInteract(event) {
        const block = event.block;
        const player = event.player;
        const blockPermutations = block.permutation.getAllStates();
        const dimension = block.dimension;
        const mainhand = player.getComponent(EntityEquippableComponent.componentId).getEquipmentSlot(EquipmentSlot.Mainhand);
        const item = mainhand.getItem();
        const { x, y, z } = block.location;
        if (blockPermutations.hasOwnProperty('furnideco:is_on')) {
            let currentOnStatus = blockPermutations['furnideco:is_on'];
            let newOnStatus = currentOnStatus === 1 ? 0 : currentOnStatus + 1;
            let newPermutation = block.permutation.withState('furnideco:is_on', newOnStatus);
            if (item?.typeId != "furnideco:paint_brush") {
                system.run(() => {
                    if (block.typeId === "furnideco:rustic.fire_pit" && item?.typeId == "minecraft:flint_and_steel" && player.isSneaking == false && newOnStatus === 1) {
                        player.playSound("fire.ignite", { location: block });
                        dimension.runCommandAsync(`execute unless block ${x} ${y + 1} ${z} furnideco:dummy_fire run setblock ${x} ${y + 1} ${z} furnideco:dummy_fire`);
                        block.setPermutation(newPermutation);
                        return;
                    } else if (block.typeId === "furnideco:rustic.fire_pit" && item?.typeId != "minecraft:flint_and_steel" && player.isSneaking == true && newOnStatus === 0) {
                        player.playSound("random.fizz", { location: block });
                        dimension.runCommandAsync(`execute if block ${x} ${y + 1} ${z} furnideco:dummy_fire run setblock ${x} ${y + 1} ${z} air`);
                        console.warn("oi");
                        block.setPermutation(newPermutation);
                        return;
                    } else {
                        if (block.typeId === "furnideco:rustic.fire_pit") {
                            return;
                        };
                        block.setPermutation(newPermutation);
                        if (block.typeId === "furnideco:rustic.chandelier" || block.typeId === "furnideco:rustic.lamp") {
                            player.playSound("furnideco.switch", { location: block });
                        } else if (block.typeId === "furnideco:rustic.ceiling_fan") {
                            player.playSound("furnideco.switch2", { location: block });
                        } else if (block.typeId === "furnideco:rustic.curtain" && newOnStatus === 1) {
                            player.playSound("furnideco.untie", { location: block, volume: 0.1 });
                            dimension.spawnParticle("furnideco:dust", { x: block.location.x + 0.5, y: block.location.y + 0.5, z: block.location.z + 0.5 });
                        } else if (block.typeId === "furnideco:rustic.curtain" && newOnStatus === 0) {
                            player.playSound("furnideco.tie", { location: block, volume: 0.1 });
                        } else if (block.typeId === "furnideco:rustic.mailbox" && newOnStatus === 0) {
                            player.playSound("furnideco.mailbox_open", { location: block, volume: 0.1 });
                        } else if (block.typeId === "furnideco:rustic.mailbox" && newOnStatus === 1) {
                            player.playSound("furnideco.mailbox_close", { location: block, volume: 0.1 });
                        } else if (block.typeId === "furnideco:rustic.radio") {
                            player.playSound("furnideco.aerial", { location: block });
                        } else if (block.typeId === "furnideco:rustic.television" && newOnStatus === 0) {
                            player.playSound("furnideco.television_on", { location: block, volume: 0.1 });
                        } else if (block.typeId === "furnideco:rustic.television" && newOnStatus === 1) {
                            player.playSound("furnideco.television_off", { location: block, volume: 0.1 });
                        }
                    }
                });
            }
        }
    }
};

world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
    blockComponentRegistry.registerCustomComponent(
        "furnideco:turn_on",
        turn_on
    );
});
