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

world.beforeEvents.chatSend.subscribe(ev =>{
  const ms = ev.message;
  const jugador = ev.sender;
  const mss = ms.split(" ");
  if (ms.startsWith("+/parasitesap addpoints")) {
    let fl = parseFloat(mss[2]);
    if (isNaN(fl)) {
      fl = mss[2];
      if (fl === undefined) fl = "";
      jugador.sendMessage({
        rawtext: [
          { text: "§c" },
          { text: `"${fl}" ` },
          { translate: "msg.parasitesap.error" }
        ]
      });
    }
    else {
      system.run(()=>{
        const dim = world.getDimension(jugador?.dimension.id).id;
        const puntos = world.scoreboard.getObjective('parasitephasegs');
        if (ms.includes("-") || fl > 2147483647 || ms.includes(".")) return jugador.sendMessage({
        rawtext: [
          { text: "§c" },
          { text: `"${fl}" ` },
          { translate: "msg.parasitesap.errorb" }
        ]
      });
        puntos.addScore(`parasitesaphase_${dim}`, fl);
        jugador.sendMessage({
        rawtext: [
          { translate: "msg.parasitesap.true" },
          { text: ` "${fl}" ` },
          { translate: "msg.parasitesap.trueb" }
        ]
      });
      });
    }
    ev.cancel = true;
  }
  if (ms.startsWith("+/parasitesap removepoints")) {
    let fl = parseFloat(mss[2]);
    if (isNaN(fl)) {
      fl = mss[2];
      if (fl === undefined) fl = "";
      jugador.sendMessage({
        rawtext: [
          { text: "§c" },
          { text: `"${fl}" ` },
          { translate: "msg.parasitesap.error" }
        ]
      });
    }
    else {
      system.run(()=>{
      const dim = world.getDimension(jugador?.dimension.id).id;
      const puntos = world.scoreboard.getObjective('parasitephasegs');
      if (ms.includes("-") || fl > 2147483648 || ms.includes(".")) return jugador.sendMessage({
        rawtext: [
          { text: "§c" },
          { text: `"${fl}" ` },
          { translate: "msg.parasitesap.errorb" }
        ]
      });
      puntos.addScore(`parasitesaphase_${dim}`, -fl);
      jugador.sendMessage({
        rawtext: [
          { text: ` -"${fl}" ` },
        ]
      });
      });
    }
    ev.cancel = true;
  }
  if (ms.startsWith("+/parasitesap setphase")) {
    let fl = parseFloat(mss[2]);
    if (isNaN(fl)) {
      fl = mss[2];
      if (fl === undefined) fl = "";
      jugador.sendMessage({
        rawtext: [
          { text: "§c" },
          { text: `"${fl}" ` },
          { translate: "msg.parasitesap.error" }
        ]
      });
    }
    else {
      system.run(()=>{
      const dim = world.getDimension(jugador?.dimension.id).id;
      const puntos = world.scoreboard.getObjective('parasitephasegs');
      if (ms.includes("-") || fl > 8 || ms.includes(".")) return jugador.sendMessage({
        rawtext: [
          { text: "§c" },
          { text: `"${fl}" ` },
          { translate: "msg.parasitesap.errorb" }
        ]
      });
      let fase = '';
      switch (fl) {
        case 0: fase = 0; break;
        case 1: fase = 500; break;
        case 2: fase = 2000; break;
        case 3: fase = 10000; break;
        case 4: fase = 60000; break;
        case 5: fase = 200000; break;
        case 6: fase = 3000000; break;
        case 7: fase = 20000000; break;
        case 8: fase = 60000000; break;
      }
      puntos.setScore(`parasitesaphase_${dim}`, fase);
      jugador.sendMessage({
        rawtext: [
          { translate: "msg.parasitesap.truec" },
          { text: ` "${fl}"` },
        ]
      });
      });
    }
    ev.cancel = true;
  }
});