import { AllEventProp } from "./utilities";
import { world, WeatherType } from "@minecraft/server";
import { randomOneOrMinusOne } from "./time_events";
import { tornadoSaverId } from "./main";
import { dayTimeRandomIntervals as rInterval } from "./configs/eventConfig";

export function meteoriteScheldule(dimension) {
  let data = AllEventProp.getProp();
  if (data.meteorite.is_available == false) return;
  if (
    data.meteorite.nextFallDay <= world.getDay() &&
    data.meteorite.nextFallTime <= world.getTimeOfDay()
  ) {
    const randomPlayer = getRandomPlayer(dimension);
    dimension.spawnEntity("tvf_randev:meteor", {
      x:
        randomPlayer.location.x +
        randomOneOrMinusOne() * Math.floor(Math.random() * 30),
      y: 200,
      z:
        randomPlayer.location.z +
        randomOneOrMinusOne() * Math.floor(Math.random() * 30),
    });
    world.sendMessage({
      translate: "tvf_randev.event.warn.meteor",
      with: [randomPlayer.name],
    });
    data.meteorite.nextFallDay =
      world.getDay() +
      randomInteger(
        rInterval.meteorite.natural.day.min,
        rInterval.meteorite.natural.day.max
      );
    data.meteorite.nextFallTime =
      world.getDay() +
      randomInteger(
        rInterval.meteorite.natural.time.min,
        rInterval.meteorite.natural.time.max
      );
    AllEventProp.update(data);
  }
}
export const randomInteger = function (min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min);
};
function getRandomPlayer(dimension) {
  const players = dimension.getPlayers();
  const random = Math.floor(Math.random() * (players.length - 0) + 0);
  return players[random];
}
export function tornadoScheldule(dimension) {
  let data = AllEventProp.getProp();
  if (data.tornado.is_available == false) return;
  if (
    data.tornado.nextDay <= world.getDay() &&
    data.tornado.nextTime <= world.getTimeOfDay()
  ) {
    if (data.tornado.is_available == false) return;
    data.tornado.nextDay =
      world.getDay() +
      randomInteger(
        rInterval.tornado.natural.day.min,
        rInterval.tornado.natural.day.max
      );
    data.tornado.nextTime =
      world.getDay() +
      randomInteger(
        rInterval.tornado.natural.time.min,
        rInterval.tornado.natural.time.max
      );
    try {
      summonTornado(getRandomPlayer(dimension));
    } catch {}
    AllEventProp.update(data);
  }
}
export function black_holeScheldule(dimension) {
  let data = AllEventProp.getProp();
  if (data.black_hole.is_available == false) return;
  if (
    data.black_hole.nextDay <= world.getDay() &&
    data.black_hole.nextTime <= world.getTimeOfDay()
  ) {
    data.black_hole.nextDay =
      world.getDay() +
      randomInteger(
        rInterval.black_hole.natural.day.min,
        rInterval.black_hole.natural.day.max
      );
    data.black_hole.nextTime =
      world.getDay() +
      randomInteger(
        rInterval.black_hole.natural.time.min,
        rInterval.black_hole.natural.time.max
      );
    try {
      summonBlackHole(getRandomPlayer(dimension));
    } catch {}
    AllEventProp.update(data);
  }
}
export function summonTornadoInRainWeather(dimension) {
  world.afterEvents.weatherChange.subscribe((event) => {
    const torndoList = world.getDynamicProperty(tornadoSaverId);
    //есть ли торнадо
    if (torndoList == "{}") {
      if (
        event.newWeather == WeatherType.Rain &&
        event.previousWeather == WeatherType.Clear
      ) {
        if (Math.random() > 0.6) {
          //обновим планировщик
          let data = AllEventProp.getProp();
          if (data.tornado.is_available == false) return;
          //data.tornado.nextDay = world.getDay() + randomInteger(2, 5)
          //data.tornado.nextTime = randomInteger(1000, 23000)
          data.tornado.nextDay =
            world.getDay() +
            randomInteger(
              rInterval.tornado.rain.day.min,
              rInterval.tornado.rain.day.max
            );
          data.tornado.nextTime =
            world.getDay() +
            randomInteger(
              rInterval.tornado.rain.time.min,
              rInterval.tornado.rain.time.max
            );
          try {
            summonTornado(getRandomPlayer(dimension));
          } catch {}
          AllEventProp.update(data);
        }
      }
    }
  });
}
export function summonBlackHole(player) {
  const rawLoc = cyl2decart(
    randomInteger(30, 50),
    Math.random() * 2 * Math.PI,
    0
  );
  const rawOffsetLoc = {
    x: player.location.x + rawLoc.x,
    y: 0,
    z: player.location.z + rawLoc.z,
  };
  const y =
    player.dimension.getTopmostBlock({
      x: rawOffsetLoc.x,
      z: rawOffsetLoc.z,
    }).location.y + 5;
  const loc = {
    x: rawOffsetLoc.x,
    y: y,
    z: rawOffsetLoc.z,
  };
  player.dimension.spawnEntity("tvf_randev:black_hole", loc);
  world.sendMessage({
    translate: "tvf_randev.event.warn.blackhole",
    with: [
      `${Math.floor(loc.x)}`,
      `${Math.floor(loc.y)}`,
      `${Math.floor(loc.z)}`,
    ],
  });
}
export function summonTornado(player) {
  const rawLoc = cyl2decart(
    randomInteger(30, 50),
    Math.random() * 2 * Math.PI,
    0
  );
  const rawOffsetLoc = {
    x: player.location.x + rawLoc.x,
    y: 0,
    z: player.location.z + rawLoc.z,
  };
  const loc = player.dimension.getTopmostBlock({
    x: rawOffsetLoc.x,
    z: rawOffsetLoc.z,
  }).location;
  player.dimension.spawnEntity("tvf_randev:tornado", loc);
  world.sendMessage({
    translate: "tvf_randev.event.warn.tornado",
    with: [
      `${Math.floor(loc.x)}`,
      `${Math.floor(loc.y)}`,
      `${Math.floor(loc.z)}`,
    ],
  });
}
function cyl2decart(r, tet, phi) {
  //phi - azimut
  //tet - zenit
  let dec = {
    x: r * Math.sin(tet) * Math.cos(phi),
    y: 0,
    z: r * Math.cos(tet),
  };
  return dec;
}
