import { world } from "@minecraft/server";
import { ActionFormData } from "@minecraft/server-ui";
import * as spells from "./Spells/Spells";
import * as scores from "./Scores";
import { spellsArray } from "./Spells/SpellsList";

//Wand menu (for equipping active spell)
export function wandMenu(player) {
  //Get equipped spells from scoreboard
  let spell1 = spellsArray[scores.getScore(player, "bc_magic_sp1")].displayName
  let spell2 = spellsArray[scores.getScore(player, "bc_magic_sp2")].displayName
  let spell3 = spellsArray[scores.getScore(player, "bc_magic_sp3")].displayName
  let spell4 = spellsArray[scores.getScore(player, "bc_magic_sp4")].displayName
  //Form
  let form = new ActionFormData();
  form.title("Wand Menu");
  form.button(`${spell1}`, itemImg(spellsArray[world.scoreboard.getObjective("bc_magic_sp1").getScore(player)].id));
  form.button(`${spell2}`, itemImg(spellsArray[world.scoreboard.getObjective("bc_magic_sp2").getScore(player)].id));
  form.button(`${spell3}`, itemImg(spellsArray[world.scoreboard.getObjective("bc_magic_sp3").getScore(player)].id));
  form.button(`${spell4}`, itemImg(spellsArray[world.scoreboard.getObjective("bc_magic_sp4").getScore(player)].id));
  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    }
    switch (response.selection) {
      case 0: {
        spells.equipSpellSlot(0, player)
        break;
      }
      case 1: {
        spells.equipSpellSlot(1, player)
        break;
      }
      case 2: {
        spells.equipSpellSlot(2, player)
        break;
      }
      case 3: {
        spells.equipSpellSlot(3, player)
        break;
      }
      default: {
        break;
      }
    }
  });
}

//Wand menu for modifying active spells in sorcery table
export function spellsMenu(player) {
  player.dimension.playSound("table.open", player.location);
  //Get equipped spells from scoreboard
  let spell1 = spellsArray[world.scoreboard.getObjective("bc_magic_sp1").getScore(player)].displayName
  let spell2 = spellsArray[world.scoreboard.getObjective("bc_magic_sp2").getScore(player)].displayName
  let spell3 = spellsArray[world.scoreboard.getObjective("bc_magic_sp3").getScore(player)].displayName
  let spell4 = spellsArray[world.scoreboard.getObjective("bc_magic_sp4").getScore(player)].displayName
  //Form
  let form = new ActionFormData();
  form.title("Spells Menu");
  form.button(`Slot 1 - ${spell1}`, itemImg(spellsArray[world.scoreboard.getObjective("bc_magic_sp1").getScore(player)].id));
  form.button(`Slot 2 - ${spell2}`, itemImg(spellsArray[world.scoreboard.getObjective("bc_magic_sp2").getScore(player)].id));
  form.button(`Slot 3 - ${spell3}`, itemImg(spellsArray[world.scoreboard.getObjective("bc_magic_sp3").getScore(player)].id));
  form.button(`Slot 4 - ${spell4}`, itemImg(spellsArray[world.scoreboard.getObjective("bc_magic_sp4").getScore(player)].id));
  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    }
    //Open slots menu for each slot
    switch (response.selection) {
      case 0: {
        slotsMenu(player, 1);
        break;
      }
      case 1: {
        slotsMenu(player, 2);
        break;
      }
      case 2: {
        slotsMenu(player, 3);
        break;
      }
      case 3: {
        slotsMenu(player, 4);
        break;
      }
      default: {
        break;
      }
    }
  });
}

//List of spells menu (to select for active slot)
function slotsMenu(player, slot) {
  if (player == undefined) {
    return;
  }
  //Get active spell
  let scoreboardID = "bc_magic_sp" + slot;
  let activeSpell = spellsArray[world.scoreboard.getObjective(scoreboardID).getScore(player)].displayName;
  let activeSpellSlot = world.scoreboard.getObjective("bc_magic_asps").getScore(player).toString();
  //Form
  let form = new ActionFormData();
  form.title("Spells List");
  form.body("Active Spell: " + activeSpell);
  for (let x = 0; x < spellsArray.length; x++) {
    let buttonName = spellsArray[x].displayName;
    if (spellsArray[x].locked) {
      buttonName = buttonName + " - §8Locked§r";
    }
    form.button(buttonName, itemImg(spellsArray[x].id));
  };
  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    };
    if (spellsArray[response.selection].locked) {
      player.sendMessage("You haven't discovered this spell yet");
      return;
    };
    //Open slots menu for each slot
    spells.equipToSlot(slot, response.selection, player);
    if (slot == activeSpellSlot) {
      spells.equipSpellSlot(slot-1, player);
    };
  });
}

function itemImg(name) {
  return `textures/blockception/magic/ui/${name}`
}