import {
  world,
  system,
  EntityDamageCause
} from "@minecraft/server";
world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('we:termite_nest_destroy', {
    onPlayerDestroy(e) {
      if (e.player.matches({
          gameMode: 'creative'
        })) return;
      const block = e.block.center();
      const xpCount = Math.floor(Math.random() * 2) + 1;
      for (let i = 0; i < xpCount; i++) {
        const orb = e.dimension.spawnEntity('minecraft:xp_orb', block);
      }
    }
  });

  initEvent.blockComponentRegistry.registerCustomComponent('we:termite_nest_ticking', {
    onRandomTick(e) {

      const validLogBlocks = ["minecraft:warped_stem", "minecraft:crimson_stem", "minecraft:cherry_log", "minecraft:mangrove_log", "minecraft:jungle_log", "we:palm_log", "we:baobab_log", "we:azalea_log", "we:banana_log", "we:willow_log", "we:pine_log", "we:maple_log"];

      const block = e.block;
      const direction = Math.floor(Math.random() * 6);

      if (direction == 0) {
        if (validLogBlocks.includes(block.below().typeId) || block.below().hasTag('log')) {

          block.dimension.spawnParticle('we:termite', {
            x: block.center().x,
            y: block.center().y,
            z: block.center().z
          })
          block.dimension.playSound('dig.stem', block.center())
          block.below().setType('we:termite_nest')
          return;
        } else if (block.below().typeId === 'we:termite_nest') {
        block.dimension.spawnParticle('we:termite', {
            x: block.center().x,
            y: block.center().y,
            z: block.center().z
          })
          block.dimension.playSound('dig.stem', block.center())
          block.below().setType('we:active_termite_nest')
       
        }
      } else if (direction == 1) {
        if (validLogBlocks.includes(block.above().typeId) || block.above().hasTag('log')) {

          block.dimension.spawnParticle('we:termite', {
            x: block.center().x,
            y: block.center().y,
            z: block.center().z
          })
          block.dimension.playSound('dig.stem', block.center())
          block.above().setType('we:termite_nest')
          return;
        } else if (block.above().typeId === 'we:termite_nest') {
        block.dimension.spawnParticle('we:termite', {
            x: block.center().x,
            y: block.center().y,
            z: block.center().z
          })
          block.dimension.playSound('dig.stem', block.center())
          block.above().setType('we:active_termite_nest')
          return;
        }
      } else if (direction == 2) {
        if (validLogBlocks.includes(block.north().typeId) || block.north().hasTag('log')) {

          block.dimension.spawnParticle('we:termite', {
            x: block.center().x,
            y: block.center().y,
            z: block.center().z
          })
          block.dimension.playSound('dig.stem', block.center())
          block.north().setType('we:termite_nest')
          return;
        } else if (block.north().typeId === 'we:termite_nest') {
        block.dimension.spawnParticle('we:termite', {
            x: block.center().x,
            y: block.center().y,
            z: block.center().z
          })
          block.dimension.playSound('dig.stem', block.center())
          block.north().setType('we:active_termite_nest')
          return;
        }
      } else if (direction == 3) {
        if (validLogBlocks.includes(block.south().typeId) || block.south().hasTag('log')) {

          block.dimension.spawnParticle('we:termite', {
            x: block.center().x,
            y: block.center().y,
            z: block.center().z
          })
          block.dimension.playSound('dig.stem', block.center())
          block.south().setType('we:termite_nest')
          return;
        } else if (block.south().typeId === 'we:termite_nest') {
        block.dimension.spawnParticle('we:termite', {
            x: block.center().x,
            y: block.center().y,
            z: block.center().z
          })
          block.dimension.playSound('dig.stem', block.center())
          block.south().setType('we:active_termite_nest')
          return;
        } 
      } else if (direction == 4) {
        if (validLogBlocks.includes(block.east().typeId) || block.east().hasTag('log')) {

          block.dimension.spawnParticle('we:termite', {
            x: block.center().x,
            y: block.center().y,
            z: block.center().z
          })
          block.dimension.playSound('dig.stem', block.center())
          block.east().setType('we:termite_nest')
          return;
        } else if (block.east().typeId === 'we:termite_nest') {
        block.dimension.spawnParticle('we:termite', {
            x: block.center().x,
            y: block.center().y,
            z: block.center().z
          })
          block.dimension.playSound('dig.stem', block.center())
          block.east().setType('we:active_termite_nest')
          return;
        }
      } else if (direction == 5) {
        if (validLogBlocks.includes(block.west().typeId) || block.west().hasTag('log')) {

          block.dimension.spawnParticle('we:termite', {
            x: block.center().x,
            y: block.center().y,
            z: block.center().z
          })
          block.dimension.playSound('dig.stem', block.center())
          block.west().setType('we:termite_nest')
          return;
        } else if (block.west().typeId === 'we:termite_nest') {
        block.dimension.spawnParticle('we:termite', {
            x: block.center().x,
            y: block.center().y,
            z: block.center().z
          })
          block.dimension.playSound('dig.stem', block.center())
          block.west().setType('we:active_termite_nest')
          return;
        }
      }
    }
  });
});