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

export function highPressureGenerator_MAIN(dimension) {
  system.runInterval(() => {
    dimension
      .getEntities({
        type: "tvf_randev:high_pressure_generator_fake",
      })
      .forEach((ent) => {
        reject_tornado(dimension, ent, 25);
        check_to_despawn(dimension, ent);
      });
  }, 10);
}
//----------------------------------------------------
function check_to_despawn(dimension, ent) {
  if (dimension.getBlock(ent.location) == undefined) return;
  if (
    dimension.getBlock(ent.location).typeId ==
    "tvf_randev:high_pressure_generator"
  )
    return;
  else ent.remove();
}
world.afterEvents.playerPlaceBlock.subscribe((event) => {
  const block = event.block;
  const dimension = block.dimension;
  if (block && dimension.id == "minecraft:overworld") {
    if (block.typeId == "tvf_randev:high_pressure_generator") {
      dimension.spawnEntity("tvf_randev:high_pressure_generator_fake", {
        x: block.location.x + 0.5,
        y: block.location.y + 0.5,
        z: block.location.z + 0.5,
      });
    }
  }
});
function reject_tornado(dimension, ent, radius) {
  dimension
    .getEntities({
      type: "tvf_randev:tornado",
      maxDistance: radius,
      location: ent.location,
    })
    .forEach((tornado) => {
      if (tornado.isValid) {
        //fall
        const ax = tornado.getProperty("property:path_x");
        const az = tornado.getProperty("property:path_z");
        const age = tornado.getProperty("property:age");
        const heal = 75 - age;
        const aax = tornado.location.x;
        const aaz = tornado.location.z;
        const center_x = ent.location.x;
        const center_z = ent.location.z;
        //normal
        let cx = -center_x + aax;
        let cz = -center_z + aaz;
        const c_lenght = Math.sqrt(cx * cx + cz * cz);
        cx = cx / c_lenght;
        cz = cz / c_lenght;
        const cos_fall = 2 * (ax * cx + az * cz);
        let bx = ax - cos_fall * cx;
        let bz = az - cos_fall * cz;
        const b_lenght = Math.sqrt(bx * bx + bz * bz);
        //reflect
        bx = (bx * 10) / b_lenght;
        bz = (bz * 10) / b_lenght;
        if (
          Math.sqrt((cx + ax) * (cx + ax) + (cz + az) * (cz + az)) <
          Math.sqrt((cx + bx) * (cx + bx) + (cz + bz) * (cz + bz))
        ) {
          dimension.playSound("beacon.activate", ent.location, {
            pitch: 3,
            volume: 5.0,
          });
          dimension.spawnParticle("tvf_randev:high_pressure_generator_work", {
            x: ent.location.x,
            y: ent.location.y + 0.5,
            z: ent.location.z,
          });
          //world.sendMessage('§bTornado was rejected!')
          tornado.setProperty("property:path_x", bx);
          tornado.setProperty("property:path_z", bz);
          tornado.applyImpulse({
            x: bx * 1,
            y: 0,
            z: bz * 1,
          });
          if (heal - 15 > 0) {
            tornado.setProperty("property:age", age + 15);
            tornado.getComponent("minecraft:health").setCurrentValue(heal - 15);
          } else {
            tornado.kill();
          }
        }
      }
    });
}
