import * as mc from "@minecraft/server";
import * as effects from "./effects";
import * as vector from "./utils/vector";
import * as utils from "./utils/generalUtils";
import * as entityAbilities from "./entities";
import * as playerAbilities from "./playerAbilities";
import * as blockEvents from "./blockEvents";

import * as structureManager from "./structure/structureManager";
import { structureFeatureRegister } from "./structure/structureFeatures";

import * as umvuthi from "./entities/umvuthi";
import * as boulder from "./entities/boulderProjectile";
import * as earthrend from "./items/earthrendGauntlet";
import * as sculptor from "./entities/sculptor";
import * as geomancy from "./items/geomancerArmor";
import * as staff from "./items/sculptorStaff";
import * as timer from "./timer";
import * as jigsaw from "./customJigsaw";
import * as sandRake from "./items/sandRake";
import * as foliaathSeed from "./items/foliaathSeed";
import * as gong from "./misc/gong";
import * as mobRemover from "./items/mobRemover";
import * as caveCarver from "./caveCarver";

import { onPlaceBlockUmvuthana } from "./misc/umvuthanaTargetBehavior";

var players = mc.world.getPlayers();

mc.world.afterEvents.itemCompleteUse.subscribe((event) => {
  playerAbilities.finishUse(event);
});
mc.world.afterEvents.itemStartUse.subscribe((event) => {
  playerAbilities.startUse(event);
});
mc.world.afterEvents.itemStopUse.subscribe((event) => {
  playerAbilities.stopUse(event);
});
mc.world.beforeEvents.itemUse.subscribe((event) => {
  playerAbilities.beforeUse(event);
});
mc.world.beforeEvents.itemUseOn.subscribe((event) => {
  if (event.itemStack !== undefined && event.itemStack.typeId === "hf_mzs:gong") gong.onUseOn(event);
  sandRake.useSandRake(event);
  foliaathSeed.onUse(event);
});
mc.world.afterEvents.itemUseOn.subscribe((event) => {
  playerAbilities.useOnBlock(event);
});
mc.world.afterEvents.entityHitBlock.subscribe((event) => {
  playerAbilities.hitBlock(event);
});
mc.system.afterEvents.scriptEventReceive.subscribe((event) => {
  boulder.boulderDestroy(event);
  sculptor.startAttack(event);
  sculptor.endTestInstant(event);
  sculptor.startTest(event);
  sculptor.onDeath(event);
  sculptor.testPassed(event);
  sculptor.giveGauntlet(event);
  timer.tick(event);
  jigsaw.placeJigsawPiece(event);
  jigsaw.removeJigsaw(event);
  structureManager.triggerStructureGen(event);
  caveCarver.carveCave(event);
});

mc.world.afterEvents.playerSpawn.subscribe((event) => {
  event.player.setDynamicProperty("hf_mzs:sneaking_counter", 0);
  event.player.setDynamicProperty("hf_mzs:can_count_sneak", true);
  event.player.setDynamicProperty("hf_mzs:can_supernova", false);
  event.player.setDynamicProperty("hf_mzs:using_ability", false);
  event.player.setDynamicProperty("hf_mzs:supernova", 0);
  event.player.setDynamicProperty("hf_mzs:jumping", 0);
  event.player.setDynamicProperty("hf_mzs:solar_beam", 0);
  event.player.setDynamicProperty("hf_mzs:cold", 0);
  event.player.setDynamicProperty("hf_mzs:freezing", 0);
  event.player.getComponent("movement").setCurrentValue(0.1);
  event.player.setDynamicProperty("hf_mzs:sculptor_test_height", -0.5);
  event.player.runCommand("/function hf/mzs/effect/unfreeze");
  event.player.runCommand("/fog @s remove supernova");
  players = mc.world.getPlayers();
  // Return if player is just respawning
  if (!event.initialSpawn) {
    //event.player.runCommand("/function hf/mzs/respawn");
    event.player.setDynamicProperty("hf_mzs:umuthana_target_timer", 0);
    event.player.removeTag("hf_mzs_targetted_by_umvuthana");
    return;
  } else event.player.runCommand("/function hf/mzs/start");
});

mc.world.afterEvents.entityDie.subscribe((event) => {
  entityAbilities.entityDeath(event);
  effects.removeEntity(event);
});

mc.world.afterEvents.dataDrivenEntityTrigger.subscribe((event) => {
  if (event.entity === undefined || !event.entity.isValid()) return;
  entityAbilities.eventBehavior(event);
  effects.removeEntity(event);
});

mc.world.afterEvents.projectileHitEntity.subscribe((event) => {
  entityAbilities.projectileBehavior(event);
});
mc.world.afterEvents.projectileHitBlock.subscribe((event) => {
  entityAbilities.projectileBlockBehavior(event);
});

mc.world.afterEvents.entityHurt.subscribe((event) => {
  entityAbilities.hurtBehavior(event);
  effects.onEntityHurt(event);
});
mc.world.afterEvents.entityHitEntity.subscribe((event) => {
  entityAbilities.hitBehavior(event);
  mobRemover.useMobRemover(event);
  if (event.damagingEntity !== undefined && event.damagingEntity.typeId === "minecraft:player")
    playerAbilities.hitBehavior(event);
});

mc.world.afterEvents.playerPlaceBlock.subscribe((event) => {
  onPlaceBlockUmvuthana(event);
  entityAbilities.blockUpdateBehavior(event);
  blockEvents.onBlockUpdate(event);
  jigsaw.placeJigsawBlock(event);
});
mc.world.afterEvents.playerBreakBlock.subscribe((event) => {
  entityAbilities.blockUpdateBehavior(event);
});
mc.world.beforeEvents.playerBreakBlock.subscribe((event) => {
  playerAbilities.breakBlock(event);
});
mc.world.beforeEvents.worldInitialize.subscribe((event) => {
  structureFeatureRegister(event);
  gong.registerComponent(event);
  blockEvents.registerComponent(event);
});

mc.system.runInterval(() => {
  sculptor.tick100();
  earthrend.tick100();
}, 100);
mc.system.runInterval(() => {
  playerAbilities.tick100(players);
}, 100);
mc.system.runInterval(() => {
  effects.tick20();
  entityAbilities.tick20();
  playerAbilities.tick20(players);
  sculptor.tick20();
}, 20);
mc.system.runInterval(() => {
  entityAbilities.tick5();
  playerAbilities.tick5(players);
  sculptor.tick5();
}, 5);
mc.system.runInterval(() => {
  playerAbilities.tick1(players);
  entityAbilities.tick1();
  boulder.tick1();
  sculptor.tick1();
  earthrend.tick1();
  geomancy.tick1();
  effects.tick1();
}, 1);

//Waila for testing
// disabled bc we'll send this to mowzie

// mc.system.runInterval(() => {
//   const players = mc.world.getAllPlayers();
//   for (let i = 0; i < players.length; i++) {
//     const player = players[i];
//     try {
//       const { block, face } = player.getBlockFromViewDirection();
//       if (!block) {
//         player.onScreenDisplay.setActionBar("Not looking at a Block.");
//         return;
//       }
//       player.onScreenDisplay.setActionBar(
//         `§rblock: §7${block.typeId}§r, face: §7${face}§r, xyz: §6${block.location.x} §r/ §6${block.location.y} §r/ §6${block.location.z}§r,\n` +
//           `data: §7${JSON.stringify(block.permutation.getAllStates(), null, 4)}`
//       );
//     } catch {
//       player.onScreenDisplay.setActionBar("Not looking at a Block.");
//     }
//   }
// });
