import { ActionFormData, MessageFormData, ModalFormData } from "@minecraft/server-ui";
import { world, system } from "@minecraft/server"


//initiate form
world.afterEvents.itemUse.subscribe(event => {
  if (event.itemStack.typeId === "redstone:ruby_shop") {
    const player = event.source
    //form code
    let form = new ActionFormData();
    form.title("§g§lRuby Shop - §dBeta");
    form.body("§dWelcome! §bThis is for those who have rubies to spend, brought to you by §g§lRubies Trade Interdimensional");
    form.button("Call Trader", "textures/items/ruby")

    //form button effects
    form.show(player).then(r => {
      if (r.canceled) return;

      let response = r.selection;
      switch (response) {
        case 0:
          if (player.getDynamicProperty('mte_RuTrcooldown') == undefined) {
            player.setDynamicProperty('mte_RuTrcooldown', false)
          }
          if (player.getDynamicProperty('mte_RuTrcooldown') == false) {
            player.sendMessage("§oSent a request to §g§lRubies Trade Interdimensional")
            system.runTimeout(() => {
              player.sendMessage("§lA trade officer has been dispatched and will drop down from above, please keep the roof above you open and clear. Over.")
            }, 80);
            player.runCommandAsync("summon redstone:ruby_trader ~ ~20 ~")
            player.sendMessage("[!]§lThe trader will now stay for 5 minutes before being extracted back to HQ")
            player.setDynamicProperty('mte_RuTrcooldown', true)
          };
          if (player.getDynamicProperty('mte_RuTrcooldown') == true) {
            player.sendMessage("You have called for a trader recently, please wait...")
            system.runTimeout(() => {
              player.setDynamicProperty('mte_RuTrcooldown', false)
            }, 1200);
          }
          break;

        case 1:

          break;

        case 2:

          break;

        case 3:


        default:
      }
    }).catch(e => {
      console.error(e, e.stack);
    });
  };
});

