import { EntityComponentTypes, EquipmentSlot, ItemStack, MolangVariableMap } from "@minecraft/server";
import { actionBar, langText, Vector3Add, Vector3Scalar } from "./util";
import { getMorph } from "./main";
import { endermanAttemptTeleport } from "./abilities";
function successTransform(player, oldMorph, newMorph) {
  actionBar(player, langText(`squaredreams_bam.actionbar.transform`, ["\n", oldMorph.name, newMorph.name]));
  player.playSound(`squaredreams_bam.transform`);
  player.dimension.spawnParticle("squaredreams_bam:morph.tranform", player.location);
}
function failMorphPlayerHasHelmet(player) {
  player.playSound("note.bass");
  actionBar(player, langText("squaredreams_bam.actionbar.fail_morph_helmet"));
}
function successMorphUnlocked(player, morph) {
  player.playSound("note.chime");
  actionBar(player, langText("squaredreams_bam.actionbar.success_unlock_morph", ["\n", morph.name]));
}
function successUnlockAllMorphs(player) {
  player.playSound("note.chime");
  actionBar(player, langText("squaredreams_bam.actionbar.success_unlock_morph.all"));
}
function failMorphLocked(player, morph) {
  player.playSound("note.bass");
  actionBar(player, langText("squaredreams_bam.actionbar.fail_locked_morph"));
}
function successLockAllMorphs(player) {
  player.playSound("note.chime");
  actionBar(player, langText("squaredreams_bam.actionbar.success_lock_all_morphs"));
}
function successDisableUnlockAllMorphs(player) {
  player.playSound("note.piano");
  actionBar(player, langText("squaredreams_bam.actionbar.success_disable_unlock_morph"));
}
function failMorphAbility(player) {
  player.playSound("note.bass", { volume: 0.4 });
  actionBar(player, langText(`squaredreams_bam.actionbar.ability.fail`));
}
function failInsideBlock(player) {
  player.playSound("note.bass", { volume: 0.4 });
  actionBar(player, langText("squaredreams_bam.actionbar.fail_inside_block"));
}
function showMorphAbility(player, morph) {
  actionBar(player, langText(`squaredreams_bam.actionbar.ability.${morph.id}`));
}
function showMorphGenericAbility(player, text) {
  actionBar(player, langText(`squaredreams_bam.actionbar.ability.${text}`));
}
function showTeleportPrediction(player, mobs) {
  const equippableComponent = player.getComponent(EntityComponentTypes.Equippable);
  const itemStack = equippableComponent.getEquipment(EquipmentSlot.Mainhand) ?? new ItemStack("minecraft:air");
  const items = ["squaredreams_bam:morph_bracelet", "squaredreams_bam:morph_wand"];
  if (mobs.includes(getMorph(player).id) && items.includes(itemStack.typeId) && !player.isSneaking) {
    var distance = 10;
    const viewDir = player.getViewDirection();
    var teleport = false;
    while (!teleport && distance > 1) {
      var teleportLoc = Vector3Add(player.getHeadLocation(), Vector3Scalar(viewDir, distance));
      teleport = endermanAttemptTeleport(player, teleportLoc);
      distance--;
    }
    if (distance <= 1) {
      return;
    }
    const molang = new MolangVariableMap();
    molang.setVector3("direction", { x: 0, y: 0.1, z: 0 });
    if (endermanAttemptTeleport(player, Vector3Add(player.location, Vector3Scalar(viewDir, distance)))) {
      player.dimension.spawnParticle("squaredreams_bam:teleport_predicition", Vector3Add(player.location, Vector3Scalar(viewDir, distance)), molang);
    }
  }
}
function failTeleport(player) {
  player.playSound("note.bass", { volume: 0.4 });
  actionBar(player, langText("squaredreams_bam.actionbar.fail_teleport_location_unsafe"));
  return false;
}
export {
  failInsideBlock,
  failMorphAbility,
  failMorphLocked,
  failMorphPlayerHasHelmet,
  failTeleport,
  showMorphAbility,
  showMorphGenericAbility,
  showTeleportPrediction,
  successDisableUnlockAllMorphs,
  successLockAllMorphs,
  successMorphUnlocked,
  successTransform,
  successUnlockAllMorphs
};
