import { world, system, ItemStack, BlockPermutation, MolangVariableMap } from "@minecraft/server";

world.afterEvents.playerPlaceBlock.subscribe((eventData) => {
    const block = eventData.block
    const player = eventData.player
    if (block.typeId == 'bumble:jabena_block') {
        if (block.below(1).typeId == 'minecraft:furnace') {
            block.setPermutation(block.permutation.withState('bumble:is_heated', 1))
        }
        //Super Heated
        if (block.below(1).typeId == 'minecraft:magma' || block.below(1).typeId == 'minecraft:campfire' || block.below(1).typeId == 'minecraft:soul_campfire' || block.below(1).typeId == 'minecraft:fire' || block.below(1).typeId == 'minecraft:soul_fire') {
            block.setPermutation(block.permutation.withState('bumble:is_heated', 2))
        }
    }
    if (block.typeId == 'minecraft:furnace' || block.typeId == 'minecraft:magma' || block.typeId == 'minecraft:campfire' || block.typeId == 'minecraft:smoker' || block.typeId == 'minecraft:soul_campfire' || block.typeId == 'minecraft:fire' || block.typeId == 'minecraft:soul_fire') {
        if (block.above(1).typeId == 'bumble:jabena_block') {
            block.above(1).setPermutation(block.above(1).permutation.withState('bumble:is_heated', 1))
        }
    }
})

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:cold_jabena', {
        onPlayerInteract: e => {
            const block = e.block
            const player = e.player
			if (block.typeId == 'bumble:jabena_block' && block.permutation.getState('bumble:is_heated') < 1) {
				player.sendMessage(`This Jabena needs a heat source.`);
			}
        },
    });
    initEvent.blockComponentRegistry.registerCustomComponent('bumble:jabena_smoke', {
        onTick: e => {
            const block = e.block
            var x = block.location.x
            var y = block.location.y
            var z = block.location.z
            if (block.permutation.getState('minecraft:cardinal_direction') == 'south') {
				block.dimension.spawnParticle('minecraft:basic_smoke_particle', { x: x + 0.5, y: y + 0.9, z: z + 0.005 })
            }
            if (block.permutation.getState('minecraft:cardinal_direction') == 'north') {
                block.dimension.spawnParticle('minecraft:basic_smoke_particle', { x: x + 0.5, y: y + 0.9, z: z + 0.955 })
            }
            if (block.permutation.getState('minecraft:cardinal_direction') == 'west') {
                block.dimension.spawnParticle('minecraft:basic_smoke_particle', { x: x + 0.955, y: y + 0.9, z: z + 0.5 })
            }
            if (block.permutation.getState('minecraft:cardinal_direction') == 'east') {
                block.dimension.spawnParticle('minecraft:basic_smoke_particle', { x: x + 0.005, y: y + 0.9, z: z + 0.5 })
            }
        },
    });
});