import { world,  ItemStack, Entity,  ItemTypes, EntityInventoryComponent, Block,  } from "@minecraft/server"
import { system } from "@minecraft/server";
import { EntityTypes } 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;
        }
          let [ toggle2, toggle3, toggle4, toggle1, slider ] = response.formValues

    

          if(toggle1 == true && !player.hasTag('selected2')) {
              player.runCommandAsync("give @p digger_test:copper_axe_2")
              player.runCommandAsync("give @p better_on_bedrock:wood_axe")
              player.runCommandAsync("give @p leather_chestplate")
              player.addTag("selected2")
              player.sendMessage("§eStarter Loot: §aYes")
          }else if(toggle1 == false) {
              player.sendMessage("§eStarter Loot: §cNo")
          }
		  
           if(toggle2 == true) {
              player.addTag("allow_corpse")
              player.sendMessage("§eAllow Corpse: §aYes")
          }else if(toggle2 == false) {
               player.sendMessage("§eAllow Corpse: §cNo")
              player.removeTag("allow_corpse")
          }
          if(toggle3 == true) {
            player.addTag("toolTip")
            player.sendMessage("§eWhat's That UI: §aYes")
        }else if(toggle3 == false) {
             player.sendMessage("§eWhat's That UI: §cNo")
            player.removeTag("toolTip")
        }
        if(toggle4 == true) {
          player.addTag("endFog")
          player.sendMessage("§eEnd Biome Fog: §aYes")
      }else if(toggle4 == false) {
           player.sendMessage("§eEnd Biome Fog: §cNo")
          player.removeTag("endFog")
          player.removeTag('abyssal')
          player.removeTag('vacant')
          player.removeTag('shroom')
          player.removeTag('void')
          player.removeTag('chorus')
          player.runCommandAsync(`fog @p remove test`)
      }
        if(slider == slider) {
          console.warn(response.formValues[2])
          player.sendMessage(`§cPlayers required to sleep: §a${response.formValues[2]}`)
          player.runCommandAsync(`gamerule playerssleepingpercentage ${response.formValues[2]}`)
        }
    });
  }

  export function uiForHardcore(player) {
    let form = new ModalFormData()
    form.title("Add-On Config")
    form.toggle("Corpse On Death - Enables Player Corpse", player.hasTag('allow_corpse'))
    form.toggle("What Block Is This? [UI] - Enables the 'WAILA' UI", player.hasTag('toolTip'))
    form.toggle("End Biome Fog - Enables fog for End biomes", player.hasTag('endFog'))
    form.slider("Required players to sleep", 1, 10, 1)
    if (!player.hasTag('selected2')) form.toggle("Starter Tools - Gives Starter Tools")
  return form;
}
function showWelcomeMessage(player) {
  // Get the player's name
  let playerName = player.name;

  // Display the welcome message to the player
  console.warn(`Welcome to the world, ${playerName}!`);
}

world.afterEvents.itemUse.subscribe((ev) => {
  if(ev.itemStack.typeId == 'better_on_bedrock:config'){
     hardcoreUI(ev.source);
  }
})

world.afterEvents.playerSpawn.subscribe((ev) => {
if (!ev.initialSpawn) return;
if(!ev.player.hasTag('gotConfig')){
ev.player.runCommandAsync('give @s better_on_bedrock:config')
ev.player.addTag('gotConfig')}
  console.warn('§cI am a test')
  
  if (!ev.player.hasTag("selected") && !ev.player.hasTag("selected2")){
    hardcoreUI(ev.player);
}
}
);