import "./gauntletCombat/gauntletMain.js";
import "./entities/trainingDummy.js";
import "./blocks/blocks.js";
import "./entities/projectiles.js";
import "./pet/petMain.js";
import "./entities/gauntlet_upgrader.js";
import "./chat_commands.js";
import "./guidebook.js";

import { Player, system, world } from "@minecraft/server";
import { openSettingsMenu, openUpgradeMenu } from "./forms/gauntletMenu.js";
import { moltenRockImpact, fireTrailTick, enderTrailTick, shulkerBulletTick, seismicSmashTick } from "./gauntletCombat/attackClasses.js";
import { tornadoTick } from "./entities/tornado.js";
import { riftTick } from "./entities/rift.js";
import { stoneWallSetBlocks, stoneWallRemoveBlocks, stoneWallTick } from "./entities/stoneWall.js";

system.afterEvents.scriptEventReceive.subscribe(
   (event) => {
      const { id, message, sourceEntity } = event;

      // /scriptevent oct_eg:debug knockback_test
      // /scriptevent oct_eg:show_form settings_menu
      // /scriptevent oct_eg:show_form upgrade_menu_new
      if (id == "oct_eg:debug") {
         switch (message) {
            case "knockback_test_player":
               const dir = sourceEntity.getViewDirection();
               const baseStrengthV = 0.2;
               const baseStrengthH = 4;

               const netheriteHelmetEquipped =
                  sourceEntity.getComponent("minecraft:equippable").getEquipment("Head")?.typeId ===
                     "minecraft:netherite_helmet"
                     ? 1
                     : 0;
               const netheriteChestplateEquipped =
                  sourceEntity.getComponent("minecraft:equippable").getEquipment("Chest")?.typeId ===
                     "minecraft:netherite_chestplate"
                     ? 1
                     : 0;
               const netheriteLeggingsEquipped =
                  sourceEntity.getComponent("minecraft:equippable").getEquipment("Legs")?.typeId ===
                     "minecraft:netherite_leggings"
                     ? 1
                     : 0;
               const netheriteBootsEquipped =
                  sourceEntity.getComponent("minecraft:equippable").getEquipment("Feet")?.typeId ===
                     "minecraft:netherite_boots"
                     ? 1
                     : 0;

               const totalKnockbackRessistance =
                  (netheriteHelmetEquipped +
                     netheriteChestplateEquipped +
                     netheriteLeggingsEquipped +
                     netheriteBootsEquipped) *
                  0.15; // should be 10%, but 15% works better

               const modifiedStrengthV = baseStrengthV + totalKnockbackRessistance * baseStrengthV;
               const modifiedStrengthH = baseStrengthH + totalKnockbackRessistance * baseStrengthH;

               sourceEntity.applyKnockback(dir.x, dir.z, modifiedStrengthH, modifiedStrengthV);

               world.sendMessage(`Knockback Res: ${totalKnockbackRessistance}`);
               world.sendMessage(`KH: ${modifiedStrengthH} | KV: ${modifiedStrengthV}`);

               break;
            case "knockback_test_mob":
               const mobs = sourceEntity.dimension.getEntities({
                  location: sourceEntity.location,
                  maxDistance: 10,
                  excludeTypes: ["minecraft:item", "minecraft:experience_orb"],
                  excludeFamilies: ["inanimate"],
               });
               for (const mob of mobs) {
                  if (!mob.isValid() || mob.typeId === "minecraft:player") continue;
                  const str = 2;
                  const impulseVector = { x: 0, y: str, z: 0 };

                  // mob?.applyKnockback(0, 0, 0, str);
                  mob?.applyImpulse(impulseVector);
               }

               break;
            case "deflect_start":
               world.sendMessage("§cDEFLECT START");
               sourceEntity.setDynamicProperty("oct_eg:deflecting", true);
               break;
            case "deflect_end":
               world.sendMessage("§cDEFLECT END");
               sourceEntity.setDynamicProperty("oct_eg:deflecting", undefined);
               break;
            case "soul_meter_add":
               const gauntlet = sourceEntity.gauntletManager.gauntlets.get("oct_eg:necro_gauntlet");
               gauntlet.addSouls(1);
               break;
            case "soul_meter_remove":
               const gauntlet2 = sourceEntity.gauntletManager.gauntlets.get("oct_eg:necro_gauntlet");
               gauntlet2.removeSouls(1);
               break;
            case "test_sparks":
               sourceEntity.addSparks(160);
               break;
            default:
               break;
         }
      }
      if (id == "oct_eg:show_form") {
         switch (message) {
            case "upgrade_menu":
               openUpgradeMenu(sourceEntity);
               break;
            case "settings_menu":
               openSettingsMenu(sourceEntity);
               break;
            default:
               break;
         }
      }
      if (id == "oct_eg:gauntlet_attack") {
         switch (message) {
            case "tornado_tick":
               try {
                  tornadoTick(sourceEntity);
               } catch (e) {
                  sourceEntity?.remove();
               }
               break;
            case "rift_tick":
               try {
                  riftTick(sourceEntity);
               } catch (e) {
                  sourceEntity?.remove();
               }
               break;
            case "molten_rock_impact":
               try {
                  moltenRockImpact(sourceEntity);
                  sourceEntity.remove();
               } catch (e) {
                  sourceEntity?.remove();
               }
               break;
            case "shulker_bullet_tick":
               try {
                  shulkerBulletTick(sourceEntity);
               } catch (e) {
                  sourceEntity?.remove();
               }
               break;
            case "seismic_smash":
               try {
                  seismicSmashTick(sourceEntity);
               } catch (e) {
                  sourceEntity?.remove();
               }
               break;
            case "stone_wall_spawned":
               try {
                  system.runTimeout(() => stoneWallSetBlocks(sourceEntity), 10);
               } catch (e) {
                  sourceEntity?.remove();
               }
               break;
            case "stone_wall_despawned":
               try {
                  stoneWallRemoveBlocks(sourceEntity);
               } catch (e) {
                  sourceEntity?.remove();
               }
               break;
            case "stone_wall_tick":
               try {
                  stoneWallTick(sourceEntity);
               } catch (e) {
                  sourceEntity?.remove();
               }
               break;
            default:
               break;
         }
      }
      if (id == "oct_eg:fire_trail" && message == "tick") {
         fireTrailTick(sourceEntity);
      }
      if (id == "oct_eg:ender_trail" && message == "tick") {
         enderTrailTick(sourceEntity);
      }
   },
   { namespaces: ["oct_eg"] }
);
