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"

world.events.worldInitialize.subscribe((eventData) => {
  let playerCompShowTick = new DynamicPropertiesDefinition()
  playerCompShowTick.defineBoolean("hardcoreDeath")
  playerCompShowTick.defineBoolean("hardcoreOption")
  playerCompShowTick.defineBoolean("joined")
  eventData.propertyRegistry.registerEntityTypeDynamicProperties(playerCompShowTick, MinecraftEntityTypes["player"])
})

system.events.beforeWatchdogTerminate.subscribe(data => {
  data.cancel = true;
});
      const DimensionNames = {
        ["minecraft:overworld"]: "Overworld",
        ["minecraft:nether"]: "Nether",
        ["minecraft:the_end"]: "End"
      };

      ///
      function onPlayerDeath() {
          let players = world.getPlayers()
          for (let player of players) {
            let playerHealt = player.getComponent("minecraft:health").current
            let isDeath = player.hasTag("death")
            let Dimension = world.getDimension(player.dimension.id)
            if (playerHealt == 0 && isDeath == false) {
              const dName = DimensionNames[player.dimension.id];
              player.addTag("death")
              let entity = Dimension.spawnEntity("pog:player_corpse", new Location(player.location.x, player.location.y, player.location.z));
              entity.nameTag = "Corpse of " + player.name;
              player.runCommandAsync(`tellraw @s{"rawtext":[{"text": "§a${player.nameTag} §rdied at: §l§e${Math.round(player.location.x)}, ${Math.round(player.location.y)}, ${Math.round(player.location.z)},§r§f in The §a${dName}"}]}`)
              player.addTag("lol")
            } else if (playerHealt > 0) {
              player.removeTag("death")
            } else if(playerHealt == 0 && player.hasTag("selected") == true) {
              player.addTag("hardcoreDeath")
              
            } if(playerHealt == 20 && player.hasTag("hardcoreDeath") == true) {
              player.runCommandAsync("function hardcore")
            }
        }
        }
      
      
        world.events.tick.subscribe(onPlayerDeath)
        world.events.entityHit.subscribe(data => {
            let { entity } = data
      if(data.hitEntity?.typeId == 'pog:player_corpse'){
        console.warn("lol");
        bounty_tier_page(entity)
          }})
      
          function bounty_tier_page(entity) {
            let form = new ActionFormData()
              form.title("Are You Sure?")
              form.body("You are about to turn your corpse into ashes.\n\nIf you decide to turn your corpse into ash, all stored items will be lost.")
              ///buttons
              form.button("Dust")
              form.button("Spare")
              form.show(entity).then((response) => {
                if (response.selection == 0) {
                  console.warn("Dust")
                  world.getDimension('overworld').runCommandAsync('function despawn_corpse_in_radius')
                }
                if (response.selection == 1) {
                  console.warn("Spare")
                }
              })
          }
          
              //BOUNTY TICKS
              world.events.tick.subscribe((data) => {
                let players = world.getPlayers()
                     for (let player of players) {
                  let inv = player.getComponent( 'inventory' ).container
                  let item = inv.getItem( player.selectedSlot )
                        const inventory = player.getComponent(EntityInventoryComponent.componentId);
                        for (let slot = 0; slot < inventory.container.size; slot++) {
                          var vars = slot;
                            const itemStack = inventory.container.getItem(slot);
                            const amount = itemStack?.amount
                            const data = itemStack?.data
                          

                            //WOOL
                            const wool = inventory.container.getItem(slot);
                            const woolamount = wool?.amount
                            if (itemStack?.typeId === 'minecraft:log' && woolamount >= 1 && player.getDynamicProperty("busy") == true){
                               player.setDynamicProperty("busy", false)
                               player.setDynamicProperty("newStartComplete", true)
                               player.setDynamicProperty("locked", true)
                               player.setDynamicProperty("claimed", false)
                               player.setDynamicProperty("gettinStarted", 2)
                               player.addTag("unlocked_2")
                               player.runCommandAsync("title @p actionbar complete_wood")
                            }
                            if (itemStack?.typeId === 'minecraft:log2' && woolamount >= 1 && player.getDynamicProperty("busy") == true){
                              player.setDynamicProperty("busy", false)
                              player.setDynamicProperty("newStartComplete", true)
                              player.setDynamicProperty("locked", true)
                              player.setDynamicProperty("claimed", false)
                              player.setDynamicProperty("gettinStarted", 1)
                              player.addTag("unlocked_2")
                              player.runCommandAsync("title @p actionbar complete_wood")
                           }
                            if (itemStack?.typeId === 'minecraft:wool' && woolamount == 3 && player.getDynamicProperty("busy3") == true){
                               player.setDynamicProperty("busy3", false)
                               player.setDynamicProperty("downGoCompleted", true)
                               player.setDynamicProperty("locked3", true)
                               player.setDynamicProperty("locked4", true)
                               player.setDynamicProperty("claimed3", false)
                               player.addTag("unlocked_4")
                               player.setDynamicProperty("gettinStarted", 0)
                               player.runCommandAsync("title @p actionbar wooled_complete")
                            }
                            if (itemStack?.typeId === 'pog:waystone' && woolamount == 1 && player.getDynamicProperty("busy5") == true){
                              player.setDynamicProperty("busy5", false)
                              player.setDynamicProperty("tpStoneCompleted", true)
                              player.setDynamicProperty("locked4", true)
                              player.setDynamicProperty("locked5", true)
                              player.setDynamicProperty("claimed5", false)
                              player.addTag("unlocked_6")
                              player.setDynamicProperty("betterBedrock", 6)
                              player.runCommandAsync("title @p actionbar waystone_end")
                           }
                           if (itemStack?.typeId === 'pog:backpack' && woolamount == 1 && player.getDynamicProperty("busy7") == true){
                              player.setDynamicProperty("busy7", false)
                              player.setDynamicProperty("spaceComplete", true)
                              player.setDynamicProperty("locked6", true)
                              player.setDynamicProperty("locked7", true)
                              player.setDynamicProperty("claimed7", false)
                              player.addTag("unlocked_8")
                              player.removeTag("unlocked_7")
                              player.setDynamicProperty("betterBedrock", 5)
                              player.runCommandAsync("title @p actionbar backpack_end")
                           }
                           if (itemStack?.typeId === 'pog:forge_table' && woolamount == 1 && player.getDynamicProperty("busy8") == true){
                              player.setDynamicProperty("busy8", false)
                              player.setDynamicProperty("forgeCompleted", true)
                              player.setDynamicProperty("locked7", true)
                              player.setDynamicProperty("locked8", true)
                              player.setDynamicProperty("claimed8", false)
                              player.addTag("unlocked_9")
                              player.removeTag("unlocked_8")
                              player.setDynamicProperty("betterBedrock", 4)
                              player.runCommandAsync("title @p actionbar forge_end")
                           }
                           if (itemStack?.typeId === 'pog:enchant_bench' && player.getDynamicProperty("busy9") == true){
                            player.setDynamicProperty("busy9", false)
                            player.setDynamicProperty("mineBountyComplete", true)
                            player.setDynamicProperty("locked8", true)
                            player.setDynamicProperty("locked9", true)
                            player.setDynamicProperty("claimed9", false)
                            player.addTag("unlocked_0")
                            player.setDynamicProperty("betterBedrock", 3)
                            player.runCommandAsync("title @p actionbar miner_end")
                         }
                           //busy9
                            if (itemStack?.typeId === 'pog:stone_pickaxe' && player.getDynamicProperty("busy2") == true){
                              player.setDynamicProperty("busy2", false)
                              player.setDynamicProperty("upgradeComplete", true)
                              player.setDynamicProperty("locked2", true)
                              player.addTag("unlocked_3")
                              player.runCommandAsync("title @p actionbar upgrade_complete")
                              player.setDynamicProperty("betterBedrock", 1)
                              //upgrade_complete
                              player.setDynamicProperty("claimed2", false)
                           }


                        };
                        
       
        

        }
    })
  