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

system.runInterval(()=>{
  const m = world.getAllPlayers();
  m.forEach(player=> {
    const dim = world.getDimension(player.dimension.id).id;
    const score = world.scoreboard.getObjective(`parasitephasegs`);
    if (score === undefined) {
      world.scoreboard.addObjective("parasitephasegs", "parasitephasegs");
      world.scoreboard.getObjective("parasitephasegs").setScore(`parasitesaphase_${dim}`, 0);
      world.scoreboard.getObjective("parasitephasegs").setScore(`parasitesaphase_${dim}_num`, 0);
    }
    const ao = player.dimension.getEntities();
    try {
      const fa = world.scoreboard.getObjective("parasitephasegs").getScore(`parasitesaphase_${dim}`);
    } catch (d) {
      world.scoreboard.getObjective("parasitephasegs").setScore(`parasitesaphase_${dim}`, 0);
      world.scoreboard.getObjective("parasitephasegs").setScore(`parasitesaphase_${dim}_num`, 0);
    }
    const fases = world.scoreboard.getObjective("parasitephasegs").getScore(`parasitesaphase_${dim}`);
    if (fases < 0) world.scoreboard.getObjective("parasitephasegs").setScore(`parasitesaphase_${dim}`, 0);
    if (fases >= 0) {
      for (const entidad of ao) {
        entidad.addTag('parasitephase0');
      }
    }//0
    if (fases >= 500) {
      for (const entidad of ao) {
        entidad.addTag('parasitephase1');
      }
    }//1
    if (fases >= 2000) {
      for (const entidad of ao) {
        entidad.addTag('parasitephase2');
      }
    }//2
    if (fases >= 10000) {
      for (const entidad of ao) {
        entidad.addTag('parasitephase3');
      }
    }//3
    if (fases >= 60000) {
      for (const entidad of ao) {
        entidad.addTag('parasitephase4');
      }
    }//4
    if (fases >= 200000) {
      for (const entidad of ao) {
        entidad.addTag('parasitephase5');
      }
    }//5
    if (fases >= 3000000) {
      for (const entidad of ao) {
        entidad.addTag('parasitephase6');
      }
    }//6
    if (fases >= 20000000) {
      for (const entidad of ao) {
        entidad.addTag('parasitephase7');
      }
    }//7
    if (fases >= 60000000) {
      for (const entidad of ao) {
        entidad.addTag('parasitephase8');
      }
    }//8
    if (fases > 2000000000) world.scoreboard.getObjective("parasitephasegs").setScore(`parasitesaphase_${dim}`, 2000000000)
    const fases_num = world.scoreboard.getObjective("parasitephasegs").getScore(`parasitesaphase_${dim}_num`);
    if (fases >= 0 && fases <= 499 && fases_num != 0) {
      const rawMessage = { translate: 'num.parasitesap.zero'};
      world.sendMessage(rawMessage);
      world.scoreboard.getObjective("parasitephasegs").setScore(`parasitesaphase_${dim}_num`, 0);
    }
    if (fases >= 500 && fases <= 1999 && fases_num != 1) {
      const rawMessage = { translate: 'num.parasitesap.one'};
      world.sendMessage(rawMessage);
      for (const pl of world.getAllPlayers()) {
        pl.playSound('parasitesap.phase_1', {location: pl.location});
      }
      world.scoreboard.getObjective("parasitephasegs").setScore(`parasitesaphase_${dim}_num`, 1);
    }
    if (fases >= 2000 && fases <= 9999 && fases_num != 2) {
      const rawMessage = { translate: 'num.parasitesap.two'};
      world.sendMessage(rawMessage);
      for (const pl of world.getAllPlayers()) {
        pl.playSound('parasitesap.phase_2', {location: pl.location});
      }
      world.scoreboard.getObjective("parasitephasegs").setScore(`parasitesaphase_${dim}_num`, 2);
    }
    if (fases >= 10000 && fases <= 59999 && fases_num != 3) {
      const rawMessage = { translate: 'num.parasitesap.three'};
      world.sendMessage(rawMessage);
      for (const pl of world.getAllPlayers()) {
        pl.playSound('parasitesap.phase_3', {location: pl.location});
      }
      world.scoreboard.getObjective("parasitephasegs").setScore(`parasitesaphase_${dim}_num`, 3);
    }
    if (fases >= 60000 && fases <= 199999 && fases_num != 4) {
      const rawMessage = { translate: 'num.parasitesap.four'};
      world.sendMessage(rawMessage);
      for (const pl of world.getAllPlayers()) {
        pl.playSound('parasitesap.phase_4', {location: pl.location});
      }
      world.scoreboard.getObjective("parasitephasegs").setScore(`parasitesaphase_${dim}_num`, 4);
    }
    if (fases >= 200000 && fases <= 2999999 && fases_num != 5) {
      const rawMessage = { translate: 'num.parasitesap.five'};
      world.sendMessage(rawMessage);
      for (const pl of world.getAllPlayers()) {
        pl.playSound('parasitesap.phase_5', {location: pl.location});
      }
      world.scoreboard.getObjective("parasitephasegs").setScore(`parasitesaphase_${dim}_num`, 5);
    }
    if (fases >= 3000000 && fases <= 19999999 && fases_num != 6) {
      const rawMessage = { translate: 'num.parasitesap.six'};
      world.sendMessage(rawMessage);
      for (const pl of world.getAllPlayers()) {
        pl.playSound('parasitesap.phase_6', {location: pl.location});
      }
      world.scoreboard.getObjective("parasitephasegs").setScore(`parasitesaphase_${dim}_num`, 6);
    }
    if (fases >= 20000000 && fases <= 59999999 && fases_num != 7) {
      const rawMessage = { translate: 'num.parasitesap.seven'};
      world.sendMessage(rawMessage);
      for (const pl of world.getAllPlayers()) {
        pl.playSound('parasitesap.phase_7', {location: pl.location});
      }
      world.scoreboard.getObjective("parasitephasegs").setScore(`parasitesaphase_${dim}_num`, 7);
    }
    if (fases >= 60000000 && fases_num != 8) {
      const rawMessage = { translate: 'num.parasitesap.eight'};
      world.sendMessage(rawMessage);
      for (const pl of world.getAllPlayers()) {
        pl.playSound('parasitesap.phase_8', {location: pl.location});
      }
      world.scoreboard.getObjective("parasitephasegs").setScore(`parasitesaphase_${dim}_num`, 8);
    }
  });
});