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

system.beforeEvents.startup.subscribe((initEvent) => {
    initEvent.blockComponentRegistry.registerCustomComponent("rustic:lantern", {
        onTick: e => {
            const { block, dimension } = e;
            const blockCenter = block.center();

            if ( block.hasTag('wooden_lantern') ){
                dimension.runCommand(`particle minecraft:basic_flame_particle ${blockCenter.x} ${blockCenter.y + 0.2} ${blockCenter.z}`)
                dimension.runCommand(`particle minecraft:basic_smoke_particle ${blockCenter.x} ${blockCenter.y + 0.2} ${blockCenter.z}`)
            }
            else{
                const faceLocation = block.permutation.getState("minecraft:block_face");
                switch(faceLocation){
                    case 'up':
                        dimension.runCommand(`particle minecraft:basic_flame_particle ${blockCenter.x} ${blockCenter.y - 0.15} ${blockCenter.z}`)
                        dimension.runCommand(`particle minecraft:basic_smoke_particle ${blockCenter.x} ${blockCenter.y - 0.15} ${blockCenter.z}`)
                        break;
                    case 'down':
                        dimension.runCommand(`particle minecraft:basic_flame_particle ${blockCenter.x} ${blockCenter.y + 0.2} ${blockCenter.z}`)
                        dimension.runCommand(`particle minecraft:basic_smoke_particle ${blockCenter.x} ${blockCenter.y + 0.2} ${blockCenter.z}`)
                        break;
                    case 'north':
                        dimension.runCommand(`particle minecraft:basic_flame_particle ${blockCenter.x} ${blockCenter.y -0.12} ${blockCenter.z}`)
                        dimension.runCommand(`particle minecraft:basic_smoke_particle ${blockCenter.x} ${blockCenter.y -0.12} ${blockCenter.z}`)
                        break;
                    case 'south':
                        dimension.runCommand(`particle minecraft:basic_flame_particle ${blockCenter.x} ${blockCenter.y -0.12} ${blockCenter.z}`)
                        dimension.runCommand(`particle minecraft:basic_smoke_particle ${blockCenter.x} ${blockCenter.y -0.12} ${blockCenter.z}`)
                        break;
                    case 'east':
                        dimension.runCommand(`particle minecraft:basic_flame_particle ${blockCenter.x} ${blockCenter.y -0.12} ${blockCenter.z}`)
                        dimension.runCommand(`particle minecraft:basic_smoke_particle ${blockCenter.x} ${blockCenter.y -0.12} ${blockCenter.z}`)
                        break;
                    case 'west':
                        dimension.runCommand(`particle minecraft:basic_flame_particle ${blockCenter.x} ${blockCenter.y -0.12} ${blockCenter.z}`)
                        dimension.runCommand(`particle minecraft:basic_smoke_particle ${blockCenter.x} ${blockCenter.y -0.12} ${blockCenter.z}`)
                        break;
                }
            }
            
        },
    });
});