import { world, system } from "@minecraft/server";
import { ModalFormData } from "@minecraft/server-ui";
/* 
Checks if the player joins for the first time; if so, do nothing;
*/

export function playerJoins(event) {
  // stuff everytime the player joins for the first time
  if (!event.player.hasTag("hb_cb:FirstJoined")) {
    event.player.runCommand("/gamerule sendcommandfeedback false");
    event.player.runCommand("/gamerule showtags false");
    event.player.runCommand("/give @s hb_cb:guide_book_spawn_egg");
    event.player.runCommand('/tellraw @s {"rawtext":[{"text":"§l§aCamping Backpack§r Add-On!"}]}');
    event.player.addTag("hb_cb:FirstJoined");
  }
  system.runTimeout(() => {
    if (!event.player.hasTag("hb_cb:update_1_0_1")) {
      const form = new ModalFormData();
      form.title({ translate: "hb_cb.update_1_0_1_title" });
      form.toggle({ translate: "hb_cb.update_1_0_1_text" });
      form.submitButton({ translate: "hb_cb.update.submit" });
      form.show(event.player).then((data) => {
        if (data.formValues == undefined) {
          update_1_0_2(event.player);
          return;
        }
        if (data.formValues[0]) event.player.addTag("hb_cb:update_1_0_1");
        update_1_0_2(event.player);
      });
    }
  }, 220);

  if (event.player.hasTag("hb_cb:sleeping_in_tent")) {
    system.runTimeout(() => {
      event.player.runCommand("/ride @s stop_riding");
      event.player.runCommand("/inputpermission set @s camera enabled");
      event.player.runCommand("/inputpermission set @s movement enabled");
      event.player.removeTag("hb_cb:sleeping_in_tent");
    }, 100);
  }
}

function update_1_0_2(player) {
  if (!player.hasTag("hb_cb:update_1_2_0")) {
    const form2 = new ModalFormData();
    form2.title({ translate: "hb_cb.update_1_2_0_title" });
    form2.toggle({ translate: "hb_cb.update_1_2_0_text" });
    form2.submitButton({ translate: "hb_cb.update.submit" });
    form2.show(player).then((data) => {
      if (data.formValues == undefined) return;
      if (data.formValues[0]) player.addTag("hb_cb:update_1_2_0");
    });
  }
}
