import { system, world } from "@minecraft/server";
export const slimeyPotionTagId = "arcedge_horr:slimey_potion";

// Boss Spawns
import "./subcores/bosses.js";

// Weapons
import "./subcores/weapons.js";

// Armors
import "./subcores/armors.js";

// Stealthy Core
export let all_players = world.getAllPlayers();

// # Enviornmental Tag System #
system.runInterval(() => {
  all_players = world.getAllPlayers();

  for (const player of all_players) {
    const timeofday = world.getTimeOfDay();
    player.runCommandAsync("scoreboard players set @a ingametime " + timeofday);
    if (
      player.hasTag("arcedge_horr:IsSneaking") &&
      player.hasTag("arcedge_horr:IsJumping") &&
      (player.hasTag("arcedge_horr:wnged_dv") ||
        player.hasTag("arcedge_horr:wnged_bt") ||
        player.hasTag("arcedge_horr:wnged_dm") ||
        player.hasTag("arcedge_horr:wnged_sk"))
    ) {
      player.addEffect("slow_falling", 40, {
        amplifier: 1,
        showParticles: false,
      });
      if (player.hasTag("arcedge_horr:wng_flyable")) {
        WingDash(player, 1, 1);
      }
    }

    if (player.hasTag("arcedge_horr:goondrag")) {
      player.addEffect("slowness", 60, {
        amplifier: 3,
        showParticles: false,
      });
      player.applyKnockback(0, 0, 0, -0.5);
    }

    if (player.isSprinting) {
      player.addTag("arcedge_horr:IsSprinting");
    } else {
      player.removeTag("arcedge_horr:IsSprinting");
    }

    if (player.isOnGround) {
      player.addTag("arcedge_horr:IsOnGround");
    } else {
      player.removeTag("arcedge_horr:IsOnGround");
    }

    if (player.isJumping) {
      player.addTag("arcedge_horr:IsJumping");
    } else {
      player.removeTag("arcedge_horr:IsJumping");
    }

    if (player.isSneaking) {
      player.addTag("arcedge_horr:IsSneaking");
    } else {
      player.removeTag("arcedge_horr:IsSneaking");
    }
  }
});

// # Mob Mechanics

world.afterEvents.entityHitEntity.subscribe((event) => {
  const attacker = event.damagingEntity;
  const victim = event.hitEntity;

  // Dracula
  if (attacker.typeId === "arcedge_horr:drac") {
    system.run(() => {
      const DracHealth = attacker.getComponent("minecraft:health");
      if (!DracHealth) return;
      if (DracHealth.currentValue > 20) {
        DracHealth.setCurrentValue(DracHealth.currentValue + 3);
      }
    });
  }
  if (attacker.typeId === "arcedge_horr:guidebook") {
    victim.runCommand("event entity @s arcedge_horr:change_page_back");
  }
});

world.afterEvents.playerSpawn.subscribe((event) => {
  event.player.runCommandAsync(
    "scoreboard objectives add arcedge_horr_difficultyscore dummy"
  );
  event.player.runCommandAsync(
    "scoreboard players set arcedge_horr:difficulty arcedge_horr_difficultyscore 0"
  );
  //playerJoin doesn't seem to trigger for host player?
  if (!event.player.hasTag("arcedge_horr.joinTag")) {
    event.player.runCommandAsync(
      `loot spawn ~ ~ ~ loot "arcedge/horr/guidebook" `
    );
    event.player.addTag("arcedge_horr.joinTag");
  }
});

function WingDash(player, horz, vert) {
  const ViewDir = player.getViewDirection();
  const HPwr = Math.sqrt(ViewDir.x ** 2 + ViewDir.z ** 2) * horz;
  const Vpwr = ViewDir.y * vert;

  player.applyKnockback(ViewDir.x, ViewDir.z, HPwr, Vpwr);
}
