import { world, Items, ItemStack, Location, Entity, ItemEnchantsComponent, ItemTypes, EntityInventoryComponent, Block, Enchantment, MinecraftEnchantmentTypes } from "@minecraft/server"
import { system } from "@minecraft/server";
import { MinecraftEntityTypes, DynamicPropertiesDefinition, EntityQueryOptions } from "@minecraft/server"
import { ActionFormData, MessageFormData, ModalFormData } from "@minecraft/server-ui"

function hardcoreUI(player, ui = undefined) {
    if(!ui) ui = uiForHardcore(player);
    ui.show(player).then(response => {
        if(response.cancelationReason == "userBusy") {
          hardcoreUI(player, ui);
            return;
        } else if(response.cancelationReason == "userClosed") {
            return;
        }
        if (response.selection == 0) {
          console.warn("Yes")
          player.addTag("selected")
          player.runCommandAsync("tellraw @s {\"rawtext\": [{\"text\": \"You activated Hardcore Mode. If you die you will enter spectator mode and you'll force stay in spectator mode.\"}]}");
          player.runCommandAsync("tellraw @s {\"rawtext\": [{\"text\": \"\n\n\n\nWelcome to §aBetter on Bedrock§f Made by §eXxPoggyisLitxX§f. Use §e!contributors§f to view all users that contributed!\"}]}");
          player.runCommandAsync("give @p pog:quest_paper");
          player.runCommandAsync("function run_tags");
        }
        if (response.selection == 1) {
          player.runCommandAsync("tellraw @s {\"rawtext\": [{\"text\": \"\n\n\n\nWelcome to §aBetter on Bedrock§f Made by §eXxPoggyisLitxX§f. Use §e!contributors§f to view all users that contributed!\"}]}");
          player.runCommandAsync("give @p pog:quest_paper");
          player.runCommandAsync("function run_tags");
          player.addTag("selected2")
        }
    });
  }

  export function uiForHardcore(player) {
  let form = new ActionFormData();
  form.title("Choose Mode.");
  form.body("Choose between Hardcore Mode or Normal Mode.");
  ///buttons
  form.button("Hardcore");
  form.button("Normal");
  return form;
}

world.events.playerJoin.subscribe((ev) => {
  ev.player.setDynamicProperty("gettinStarted", 3)
  ev.player.setDynamicProperty("betterBedrock", 10)
  console.warn(ev.player.getDynamicProperty("gettinStarted"))
  if (!ev.player.hasTag("selected") && !ev.player.hasTag("selected2")){
    hardcoreUI(ev.player);
    ev.player.setDynamicProperty("showButton", true)
}
  ev.player.runCommandAsync("tellraw @s {\"rawtext\": [{\"text\": \"Welcome to §eBetter on Bedrock, Made by XxPoggyisLitxX.\nTo view the list of contributors, type [§e!contributors§f] in chat.\"}]}");
}
);