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

const eventId = "earth_quake";
const foodIds = [
  "minecraft:apple",
  "minecraft:baked_potato",
  "minecraft:beef",
  "minecraft:beetroot",
  "minecraft:beetroot_soup",
  "minecraft:bread",
  "minecraft:carrot",
  "minecraft:chicken",
  "minecraft:cod",
  "minecraft:cooked_chicken",
  "minecraft:cooked_cod",
  "minecraft:cooked_porkchop",
  "minecraft:cooked_rabbit",
  "minecraft:cooked_salmon",
  "minecraft:cookie",
  "minecraft:dried_kelp",
  "minecraft:golden_apple",
  "minecraft:golden_carrot",
  "minecraft:mushroom_stew",
  "minecraft:poisonous_potato",
  "minecraft:porkchop",
  "minecraft:potato",
  "minecraft:pufferfish",
  "minecraft:pumpkin_pie",
  "minecraft:rabbit",
  "minecraft:rabbit_stew",
  "minecraft:rotten_flesh",
  "minecraft:salmon",
  "minecraft:spider_eye",
  "minecraft:sweet_berries",
  "minecraft:tropical_fish",
];
export function earthquake_MAIN(dimension) {
  system.runInterval(() => {
    const event = CurrentEventProp.getProp();
    if (!(event.id == eventId && event.status == "is_happening")) return;
    playerEating();
    if (event.timer < event.maxDuration) {
      if (event.timer == 1) {
        world.sendMessage([
          {
            translate: `tvf_randev.event.start.${event.id}`,
          },
        ]);
      }
      playerEffects(dimension);
      event.timer += 1;
    }
    if (event.forcedEnd == true || event.timer >= event.maxDuration) {
      event.status = "is_end";
      world.sendMessage([
        {
          translate: `tvf_randev.event.end.${event.id}`,
        },
      ]);
    }
    CurrentEventProp.update(event);
  });
}
function playerEffects(dimension) {
  if (system.currentTick % 100 != 0) return;
  dimension.getPlayers().forEach((player) => {
    player.runCommand("/camerashake add @s 0.4 5 positional");
    player.addEffect("nausea", 5 * 20);
    player.playSound("sound.tvf_randev.earthquake_ambient");
  });
}
function playerEating() {
  world.afterEvents.itemCompleteUse.subscribe((eating) => {
    const event = CurrentEventProp.getProp();
    if (!(event.id == eventId && event.status == "is_happening")) return;
    const { source, itemStack } = eating;
    const player = source;
    if (foodIds.includes(itemStack.typeId)) {
      player.addEffect("poison", 200);
    }
  });
}
