import { world, EquipmentSlot } from "@minecraft/server";
import * as scores from "./Scores";
import * as spells from "./Spells/SpellsList";
import * as cooldown from "./Cooldowns";
import { namespace } from "./Namespace";

let spellsArray = spells.spellsArray

export function cast(player) {
  for (let spell in spellsArray) {
    if (spell == scores.getScore(player, "bc_magic_asp")) {
      //Cancel if player doesn't have enough XP - UNUSED
      if (spellsArray[spell].xpCost > player.getTotalXp()) {
        player.sendMessage("You don't have enough XP to cast this spell.");
        return;
      }
      //Otherwise take XP and continue - UNUSED
      else {
        removeCostXp(player, spellsArray[spell].xpCost);
      }

      //Check if spell is on cooldown
      let cooldownTime = cooldown.getCooldown(player, parseInt(spell));
      let cooldownSeconds = (cooldownTime/20).toString();
      if (cooldownTime > 0) {
        player.onScreenDisplay.setActionBar(`§c${spellsArray[spell].displayName} is on cooldown for ${cooldownSeconds}s.§r`);
        player.dimension.playSound("spell.fail", player.location);
        return;
      }
      //If spell is usable, set cooldown
      cooldown.setCooldown(player, parseInt(spell));

      //Play casting sound
      let castSound = "spell." + spellsArray[spell].id;
      player.dimension.playSound(castSound, player.location);
      switch (parseInt(spell)) {
        case 0: {
          player.sendMessage("No spell currently selected.");
          break;
        }
        case 1: {
          spells.sparksSpell(player);
          break;
        }
        case 2: {
          spells.explosionSpell(player);
          break;
        }
        case 3: {
          spells.pushSpell(player);
          break;
        }
        case 4: {
          spells.growSpell(player);
          break;
        }
        case 5: {
          spells.waterSpell(player);
          break;
        }
        case 6: {
          spells.fireSpell(player);
          break;
        }
        case 7: {
          spells.lightSpell(player);
          break;
        }
        case 8: {
          spells.poisonSpell(player);
          break;
        }
        case 9: {
          spells.healingSpell(player);
          break;
        }
        case 10: {
          spells.levitatingSpell(player);
          break;
        }
        case 11: {
          spells.shieldSpell(player);
          break;
        }
        case 12: {
          spells.fogSpell(player);
          break;
        }
        case 13: {
          spells.iceSpell(player);
          break;
        }
        case 14: {
          spells.stunSpell(player);
          break;
        }
        case 15: {
          spells.spongeSpell(player);
          break;
        }
        case 16: {
          spells.musicSpell(player);
          break;
        }
        case 17: {
          spells.revealingSpell(player);
          break;
        }
        case 18: {
          spells.slowFallSpell(player);
          break;
        }
        case 19: {
          spells.weatherSpell(player);
          break;
        }
        case 20: {
          spells.transformSpell(player);
          break;
        }
        default: {
          break;
        }
      };
    }
  }
}

function removeCostXp(player, amount) {
  if (amount == 0) {
    return;
  }
  if (player.xpEarnedAtCurrentLevel < amount) {
    let removeAmount = amount - player.xpEarnedAtCurrentLevel;
    player.addExperience(-player.xpEarnedAtCurrentLevel);
    player.addLevels(-1);
    let giveAmount = player.totalXpNeededForNextLevel - removeAmount;
    player.addExperience(giveAmount);
  }
  else {
    player.addExperience(-amount);
  }
}