import { world, system } from "@minecraft/server";
import { fogHandler } from "./biome/fog_handler";
import { moonHandler } from "./moon/moon_handler";
import { initVfxController } from "./biome/ambient_handler";

import { screamingHelmet, lostLeggings, deathWingBoots, vindicatorChestplate } from "./utilities/armor_handler";
import { breakBlockItemDurability, hitEntityItemDurability } from "./utilities/item_durability";
import { flashlightHandler, flashlightSwitch } from "./utilities/flashlight";
import { playerHandler, playerJoins } from "./utilities/player_joins";
import { jumpScareAnim, jumpScareInit } from "./utilities/jumpscare";
import { guideBook } from "./utilities/guidebook";
import { scaredMobs } from "./utilities/scared_mobs";
import { fakeBoneBlock } from "./utilities/fake_bone_block";
import { bonesBlock } from "./utilities/bones";
import { denySnowOnPlayer } from "./utilities/deny_snow";
import { chargedWeapons } from "./utilities/charged_weapon";
import { weaponsAnims } from "./utilities/weapon_anims";
import { pinkPetalsBlock } from "./utilities/pink_petals";
import { airBelowBlock } from "./biome/air_below_block";
import { hangingVine } from "./utilities/vines";
import { mossBehavior } from "./utilities/moss";
import { facelessSpawner, facelessVindicatorHit, facelessPillagerMediumHit } from "./utilities/faceless_spawner";
import { wall_Manager } from "./utilities/wall_Manager";
import { doublePlant } from "./utilities/double_plant";
import { grassPlant } from "./utilities/grass_plant";
import { grassBlock } from "./utilities/grassBlock";
import { roof } from "./new_wood/roof";
import { door } from "./new_wood/door";
import { fallingLeaves } from "./new_wood/fallingLeaves";
import { fence } from "./new_wood/fence";
import { new_wood_handler } from "./new_wood/new_wood_handler";
import { treeTicks } from "./new_wood/OnTick";
import { slabs } from "./new_wood/slabs";
import { trapdoor } from "./new_wood/trapdoor";
import { CustomWoodMechanic } from "./new_wood/tree";
import { boatHandler } from "./new_wood/boat";
import { blockHandlerB1 } from "./biome/b1/block_handler";
import { blockHandlerB2, placeFarmland } from "./biome/b2/block_handler";
import { blockHandlerB3 } from "./biome/b3/block_handler";
// import { lichenHandler } from "./biome/b2/custom_lichen";
import { creaking_heart, lostSoulTooFar } from "./biome/creaking_heart";
import { bannedWitherSkullPodest } from "./biome/b1/wither_skull_podest";
import { banned_wither } from "./banned_wither/banned_wither";
import { bannedWitherSkullBlockHandler } from "./banned_wither/skull_block_handler";

// Independent Scripts - the imports below are scripts that don't interact with anything
// else. Simply importing them registers all of the things that they do.
import "./biome/b2/undead_villager";
import "./biome/b2/undead_golem";
import "./fog_dweller/fog_dweller";
import "./fog_dweller/fog_dweller_gravestone";

import { vfxHandler } from "./vfx_handler";
import { hiveBlockTick } from "./biome/b1/death_moth";
import { chainSaw } from "./utilities/lumberjack";
import { chainsawStun } from "./utilities/chainsaw_stun";

playerJoins();
playerHandler();

world.afterEvents.playerBreakBlock.subscribe((data) => {
  wall_Manager.updateWallsAround(data.block);
});

world.afterEvents.playerPlaceBlock.subscribe((data) => {
  wall_Manager.updateWallsAround(data.block);
});
jumpScareInit();
fakeBoneBlock();
bonesBlock();
scaredMobs();
pinkPetalsBlock();
airBelowBlock();

//new fogHandler();
new moonHandler();
//new vfxHandler();
initVfxController();

chargedWeapons();
weaponsAnims();
flashlightSwitch();

hitEntityItemDurability();
breakBlockItemDurability();
facelessSpawner();
facelessVindicatorHit();
facelessPillagerMediumHit();
//initVfxController();
new boatHandler();
new blockHandlerB1();
new blockHandlerB2();
new blockHandlerB3();

banned_wither();
bannedWitherSkullBlockHandler();
bannedWitherSkullPodest();

hangingVine();
mossBehavior();
doublePlant();
grassPlant();
grassBlock();

creaking_heart();

placeFarmland();
door();
// fallingLeaves();
fence();
new_wood_handler();
roof();
treeTicks();
slabs();
trapdoor();
CustomWoodMechanic();

chainSaw();
chainsawStun();

let curTick = 0;
const INIT_TIME = 0;
system.runInterval(() => {
  try {
    if (curTick > INIT_TIME) {
      const allPlayers = world.getAllPlayers();
      allPlayers.forEach((player) => {
        if (!player.isValid()) return;
        else {
          hiveBlockTick(player);
          jumpScareAnim(player);

          if (system.currentTick % 100 == 0) {
            screamingHelmet(player);
          }
          if (system.currentTick % 20 == 0) {
            guideBook(player);
            lostLeggings(player);
            lostSoulTooFar(player);
          }
          vindicatorChestplate(player);
          deathWingBoots(player);
          flashlightHandler(player);
          // flashlight(player);
          // processPendingReplacementsForPlayer(player);
          // player.runCommand("/function hb/hr/replace_b3");

          // deacitaved because we have better biome placement now
          // denySnowOnPlayer(player, curTick);
          return;
        }
      });
    }
    curTick++;
  } catch (e) {
    // console.warn(e);
  }
}, 0);

function defaultInit() {
  let scoreboard = world.scoreboard.getObjective("hb_hr:tmp");
  // Run init if it haven't been run before
  if (scoreboard == undefined) {
    world.getDimension("overworld").runCommandAsync("function hb/hr/init");
  }
}

defaultInit();
