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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:rose_quartz_lamp_switch", {
    onPlayerInteract: eventData => {
      const player = eventData.player;
      const block = eventData.block;
      const blockLocation = block.location;
      if (player.isSneaking) return;
      if (block.typeId === "nicothekid:rose_quartz_lamp") {
        block.dimension.playSound("random.lever_click", blockLocation, { pitch: 1.0, volume: 1.0 });
        if (block.permutation.matches("nicothekid:rose_quartz_lamp", { "nicothekid:light_emission": 0 })) {
          block.setPermutation(BlockPermutation.resolve("nicothekid:rose_quartz_lamp", { "nicothekid:light_emission": 1 }));
          return;
        }
        if (block.permutation.matches("nicothekid:rose_quartz_lamp", { "nicothekid:light_emission": 1 })) {
          block.setPermutation(BlockPermutation.resolve("nicothekid:rose_quartz_lamp", { "nicothekid:light_emission": 2 }));
          return;
        }
        if (block.permutation.matches("nicothekid:rose_quartz_lamp", { "nicothekid:light_emission": 2 })) {
          block.setPermutation(BlockPermutation.resolve("nicothekid:rose_quartz_lamp", { "nicothekid:light_emission": 3 }));
          return;
        }
        if (block.permutation.matches("nicothekid:rose_quartz_lamp", { "nicothekid:light_emission": 3 })) {
          block.setPermutation(BlockPermutation.resolve("nicothekid:rose_quartz_lamp", { "nicothekid:light_emission": 4 }));
          return;
        }
        if (block.permutation.matches("nicothekid:rose_quartz_lamp", { "nicothekid:light_emission": 4 })) {
          block.setPermutation(BlockPermutation.resolve("nicothekid:rose_quartz_lamp", { "nicothekid:light_emission": 0 }));
          return;
        }
      }
      else return;
    }
  });
})
