import { world, EquipmentSlot } from "@minecraft/server";
import * as scores from "../Scores";
import { spellsArray } from "./SpellsList";
import { Vector3 } from "../Vector3";
import { namespace } from "../Namespace";

/**
 * @type {EntityEquippableComponent}
*/
export function equipSpellSlot(x, player) {
  //Get player's wand as item
  const item = player.getComponent("equippable").getEquipmentSlot(EquipmentSlot.Mainhand);
  //Set player's activespell score to the score in the slot they have selected
  let scoreboardId = "bc_magic_sp" + (x+1).toString();
  let spellId = world.scoreboard.getObjective(scoreboardId).getScore(player);
  scores.setScore(player, "bc_magic_asp", scores.getScore(player, scoreboardId));
  scores.setScore(player, "bc_magic_asps", (x+1));
  //Equip spell
  if (spellId == 0 ) {
    player.sendMessage("Empty spell slot");
    return;
  }
  equipSpell(spellId, x, item, player);
}

//Function run in sorcery table when adding a new spell
export function equipToSlot(slot, spell, player) {
  //Sets the slot score to the ID of the spell
  let scoreboardId = "bc_magic_sp" + slot;
  world.scoreboard.getObjective(scoreboardId).setScore(player, spell);
}

const wandNames = {
  [`${namespace}:wand_acacia`]: "Acacia Wand",
  [`${namespace}:wand_bamboo`]: "Bamboo Wand",
  [`${namespace}:wand_birch`]: "Birch Wand",
  [`${namespace}:wand_cherry`]: "Cherry Wand",
  [`${namespace}:wand_crimson`]: "Crimson Wand",
  [`${namespace}:wand_darkoak`]: "Dark Oak Wand",
  [`${namespace}:wand_jungle`]: "Jungle Wand",
  [`${namespace}:wand_mangrove`]: "Mangrove Wand",
  [`${namespace}:wand_oak`]: "Oak Wand",
  [`${namespace}:wand_paleoak`]: "Pale Oak Wand",
  [`${namespace}:wand_spruce`]: "Spruce Wand",
  [`${namespace}:wand_warped`]: "Warped Wand",
}

//Function run when equipping a spell from wand menu
function equipSpell(spell, slot, wand, player) {
  //Send message to the player with equipped spell
  let playerMessage = "Equipped §6" + spellsArray[spell].displayName + "§r in slot " + slot.toString();
  player.sendMessage(playerMessage);

  //Rename player's wand to include spell name
  let wandName;
  if (wand.nameTag) {
    let splitName = wand.nameTag.split("§");
    if (splitName.length <= 2) {
      wandName = wand.nameTag.trim();
    } else {
      wandName = splitName.slice(0, -2).join("§").trim();
    }
  } else {
    wandName = wandNames[wand.typeId];
  };
  let prefix;
  if (wandName.startsWith("§r§f")) {
    prefix = "";
  } else {
    prefix = "§r§f";
  }
  wand.nameTag = prefix + wandName + " §6(" + spellsArray[spell].displayName + ")§r";
}

export function castSpellProjectile(player, projectile, velocity) {
  const viewDirection = new Vector3 (player.getViewDirection().x, player.getViewDirection().y, player.getViewDirection().z);
  const spawnLoc = new Vector3(player.getHeadLocation().x, player.getHeadLocation().y, player.getHeadLocation().z).add(viewDirection);
  const spellProjectile = player.dimension.spawnEntity(projectile, spawnLoc);
  spellProjectile.setRotation(player.getRotation());
  spellProjectile.applyImpulse(viewDirection.multiply(velocity));
}

