import { world, Items, ItemStack, MinecraftEntityTypes, DynamicPropertiesDefinition, ScreenDisplay, ItemTypes } from "@minecraft/server"
import { ActionFormData, MessageFormData } from "@minecraft/server-ui"
 

//variables and booleans
let bountyXP = 0
let bountyMaxXP = 1000
let BountyPage1 = [
  "First Time",
  "Getting an upgrade",
  "Wooled"
]
let BountyPage2 = [
  "The Pillager Captian",
  "War"
]
let BountyPage3 = [
  "The Nether",
  "Ghasts?",
  "Withered",
  "Rods"
]
let bountyTiers = [
  "A new start",
  "Savior of the villagers",
  "Beyond the Overworld",
]
let randomXP = Math.floor(Math.random() * 64) + 32;
world.events.worldInitialize.subscribe((eventData) => {
    let playerCompShowTick = new DynamicPropertiesDefinition()
    playerCompShowTick.defineBoolean("showButton")
    playerCompShowTick.defineBoolean("newStartComplete")
    playerCompShowTick.defineBoolean("busy")
    playerCompShowTick.defineBoolean("locked")
    playerCompShowTick.defineBoolean("busy2")
    playerCompShowTick.defineBoolean("claimed2")
    playerCompShowTick.defineBoolean("claimed")
    playerCompShowTick.defineBoolean("locked2")
    playerCompShowTick.defineBoolean("busy3")
    playerCompShowTick.defineBoolean("claimed3")
    playerCompShowTick.defineBoolean("locked3")
    playerCompShowTick.defineBoolean("upgradeComplete")
    playerCompShowTick.defineBoolean("downGoCompleted")
    playerCompShowTick.defineBoolean("villageBountyComplete")
    playerCompShowTick.defineNumber("ironBountyComplete")
    playerCompShowTick.defineBoolean("netherBountyComplete")
    playerCompShowTick.defineBoolean("boneBountyComplete")
    playerCompShowTick.defineNumber("xpVal")
    playerCompShowTick.defineNumber("gettinStarted")
    playerCompShowTick.defineNumber("betterBedrock")

    playerCompShowTick.defineBoolean("wholeNewComplete")
    playerCompShowTick.defineBoolean("tpStoneCompleted")
    playerCompShowTick.defineBoolean("tpToBountyComplete")
    playerCompShowTick.defineBoolean("spaceComplete")
    playerCompShowTick.defineBoolean("forgeCompleted")
    playerCompShowTick.defineBoolean("mineBountyComplete")
    playerCompShowTick.defineBoolean("copperComplete")
    playerCompShowTick.defineBoolean("yummCompleted")

    playerCompShowTick.defineBoolean("busy4")
    playerCompShowTick.defineBoolean("claimed4")
    playerCompShowTick.defineBoolean("locked4")

    playerCompShowTick.defineBoolean("busy5")
    playerCompShowTick.defineBoolean("claimed5")
    playerCompShowTick.defineBoolean("locked5")

    playerCompShowTick.defineBoolean("busy6")
    playerCompShowTick.defineBoolean("claimed6")
    playerCompShowTick.defineBoolean("locked6")

    playerCompShowTick.defineBoolean("busy7")
    playerCompShowTick.defineBoolean("claimed7")
    playerCompShowTick.defineBoolean("locked7")

    playerCompShowTick.defineBoolean("busy8")
    playerCompShowTick.defineBoolean("claimed8")
    playerCompShowTick.defineBoolean("locked8")

    playerCompShowTick.defineBoolean("busy9")
    playerCompShowTick.defineBoolean("claimed9")
    playerCompShowTick.defineBoolean("locked9")

    playerCompShowTick.defineBoolean("busy10")
    playerCompShowTick.defineBoolean("claimed10")
    playerCompShowTick.defineBoolean("locked10")

    playerCompShowTick.defineBoolean("busy11")
    playerCompShowTick.defineBoolean("claimed11")
    playerCompShowTick.defineBoolean("locked11")

    eventData.propertyRegistry.registerEntityTypeDynamicProperties(playerCompShowTick, MinecraftEntityTypes["player"])
  })
  world.events.blockBreak.subscribe(data => {
      if (data.brokenBlockPermutation.type.typeId == 'minecraft:iron_ore' && player.hasTag("busy_6") && !player.getDynamicProperty("ironBountyComplete") == 1) {
        console.warn('1')
        player.setDynamicProperty("ironBountyComplete", 1)
      } else if (data.brokenBlockPermutation.type.typeId == 'minecraft:iron_ore' && player.hasTag("busy_6") && player.getDynamicProperty("ironBountyComplete") == 1) {
        console.warn("2")
        player.setDynamicProperty("ironBountyComplete", 2)
      } else if (data.brokenBlockPermutation.type.typeId == 'minecraft:iron_ore' && player.hasTag("busy_6") && player.getDynamicProperty("ironBountyComplete") == 2) {
        console.warn("3")
        player.setDynamicProperty("ironBountyComplete", 3)
        player.removeTag("busy_6")
      }


  })

  //bounty sections
  function coal_bounty(i) {
    let form = new MessageFormData()
    form.title("Start Bounty?")
    form.body("Obtain your first log.\n\nRewards: 100Xp")
    form.button1("Start Bounty!")
    form.button2("Cancel")
    form.show(i).then((response) => {
        if (response.selection === 1 && !i.hasTag('busy_1')) {
          i.onScreenDisplay.setActionBar('lol')
          i.removeTag("unlocked_1")
          i.setDynamicProperty('busy', true)
          i.addTag('busy_1')
          world.getDimension('overworld').runCommandAsync('event entity @p bounty_one_for_animation_runtimes')
        }
        else if (response.selection === 0) {
          test(i)
  
        }
      
    })
  }
  
  //Bacon Bounty
  //Trouble Maker Bounty

  function nether_bounty(i) {
    let form = new ActionFormData()
    form.title("Start Bounty?")
    form.body("Make a wooden pickaxe, then upgrade it to a stone pickaxe, then an iron pickaxe.\nRewards: 4 apples\n60 xp")
    form.button("Start Bounty!")
    form.button("Cancel")
    form.show(i).then((response) => {
        if (response.selection === 0) {
          
          i.removeTag("unlocked_2")
          i.onScreenDisplay.setActionBar('upgrade_start')
          //upgrade_start
          i.setDynamicProperty('busy2', true)
          i.addTag('busy_2')
        }
        else if (response.selection === 1) {
  
        }
      
    })
  }//bounty_one_for_animation_runtimes
  function Wooled(i) {
    let form = new ActionFormData()
    form.title("Start Bounty?")
    form.body("Get 3 Wool and make a Bed.\nRewards: 2 Iron Ingots\n100 xp")
    form.button("Start Bounty!")
    form.button("Cancel")
    form.show(i).then((response) => {
        if (response.selection === 0) {
          i.removeTag("unlocked_3")
          i.onScreenDisplay.setActionBar('wooled_start')
          i.setDynamicProperty('busy3', true)
          i.addTag('busy_3')
          //wooled_start
          world.getDimension('overworld').runCommandAsync('event entity @p bounty_one_for_animation_runtimes')
        }
        else if (response.selection === 1) {
  
        }
    })
  }
  function bounty_tier_page(i) {
        
    let form = new ActionFormData()
      let bountyxp = i.getDynamicProperty("xpVal")
      let questAvailable = i.getDynamicProperty("gettinStarted")
      let betterBedrockAvailable = i.getDynamicProperty("betterBedrock")
      form.title("§fBounty Tiers")
      form.body("This is the tier list. Earn a certain amount of §eBounty XP§r to unlock your next tier.")
      ///buttons
      form.button("§fA new start\n[§a" + questAvailable + " Quests Available]", "textures/items/emerald")
      if (bountyxp < 300){
      form.button("§fBetter on Bedrock\n[§a" + bountyxp + "§e/300xp§f]", "textures/items/iron_ingot")
} else if (bountyxp >= 300){
  form.button("§fBetter on Bedrock\n[§a" + betterBedrockAvailable + " Quests Available]", "textures/items/iron_ingot")
}
      form.button("§fBeyond the Overworld\n[§a" + bountyxp + "§e/1200xp§f]", "textures/items/gold_ingot")
      form.show(i).then((response) => {
        if (response.selection == 0) {
          test(i)
        }
        if (response.selection == 1 && bountyxp > 250) {
          bob(i)
        }
      })
  }

//bob ui
function bob(i) {
  let form = new ActionFormData()
    form.title("§fBetter on Bedrock")
    form.body("Complete Better on Bedrock Quests to unlock more. Completing a quest will unlock another one.")
    ///one button
    if (i.hasTag("unlocked_4")) {
      form.button("§fA whole new world\n[§eUnlocked§f]", "textures/items/emerald")
    } else if (i.getDynamicProperty('busy4') == true) {
      form.button("A whole new world [§dBusy§f]", "textures/items/emerald")
    } else if (i.getDynamicProperty("wholeNewComplete") == true) {
      form.button("A whole new world [§aCompleted§f]", "textures/items/emerald")
    }

    if (i.hasTag("unlocked_5")) {
      form.button("§fTeleport Stone?\n[§eUnlocked§f]", "textures/ui/3d_blocks_for_bounty_ui/waystone")
    } else if (i.getDynamicProperty('busy5') == true) {
      form.button("Teleport Stone? [§dBusy§f]", "textures/ui/3d_blocks_for_bounty_ui/waystone")
    } else if (i.getDynamicProperty("tpStoneCompleted") == true) {
      form.button("Teleport Stone? [§aCompleted§f]", "textures/ui/3d_blocks_for_bounty_ui/waystone")
    } else if (i.getDynamicProperty("locked5") == true) {
      form.button("Teleport Stone? [§cLocked§f]", "textures/ui/3d_blocks_for_bounty_ui/waystone")
    }

    if (i.hasTag("unlocked_6")) {
      form.button("§fTeleport To Stone?\n[§eUnlocked§f]", "textures/items/waystone/waypoint_marker")
    } else if (i.getDynamicProperty('busy6') == true) {
      form.button("Teleport To Stone? [§dBusy§f]", "textures/items/waystone/waypoint_marker")
    } else if (i.getDynamicProperty("tpToBountyComplete") == true) {
      form.button("Teleport To Stone? [§aCompleted§f]", "textures/items/waystone/waypoint_marker")
    } else if (i.getDynamicProperty("locked6") == true) {
      form.button("Teleport To Stone? [§cLocked§f]", "textures/items/waystone/waypoint_marker")
    }

    if (i.hasTag("unlocked_7")) {
      form.button("§fMore space\n[§eUnlocked§f]", "textures/items/backpack")
    } else if (i.getDynamicProperty('busy7') == true) {
      form.button("More space [§dBusy§f]", "textures/items/backpack")
    } else if (i.getDynamicProperty("spaceComplete") == true) {
      form.button("More space [§aCompleted§f]", "textures/items/backpack")
    } else if (i.getDynamicProperty("locked7") == true) {
      form.button("More space [§cLocked§f]", "textures/items/backpack")
    }

    if (i.hasTag("unlocked_8")) {
      form.button("§fForging Time\n[§eUnlocked§f]", "textures/ui/3d_blocks_for_bounty_ui/foge_table")
    } else if (i.getDynamicProperty('busy8') == true) {
      form.button("Forging Time [§dBusy§f]", "textures/ui/3d_blocks_for_bounty_ui/foge_table")
    } else if (i.getDynamicProperty("forgeCompleted") == true) {
      form.button("Forging Time [§aCompleted§f]", "textures/ui/3d_blocks_for_bounty_ui/foge_table")
    } else if (i.getDynamicProperty("locked8") == true) {
      form.button("Forging Time [§cLocked§f]", "textures/ui/3d_blocks_for_bounty_ui/foge_table")
    }

    if (i.hasTag("unlocked_9")) {
      form.button("§fMiners dream\n[§eUnlocked§f]", "textures/ui/3d_blocks_for_bounty_ui/enchant_table")
    } else if (i.getDynamicProperty('busy9') == true) {
      form.button("Miners dream [§dBusy§f]", "textures/ui/3d_blocks_for_bounty_ui/enchant_table")
    } else if (i.getDynamicProperty("mineBountyComplete") == true) {
      form.button("Miners dream [§aCompleted§f]", "textures/ui/3d_blocks_for_bounty_ui/enchant_table")
    } else if (i.getDynamicProperty("locked9") == true) {
      form.button("Miners dream [§cLocked§f]", "textures/ui/3d_blocks_for_bounty_ui/enchant_table")
    }

    if (i.hasTag("unlocked_10")) {
      form.button("§fOxedised\n[§eUnlocked§f]", "textures/items/copper_update/copper_chestplate")
    } else if (i.getDynamicProperty('busy10') == true) {
      form.button("Oxedised [§dBusy§f]", "textures/items/copper_update/copper_chestplate")
    } else if (i.getDynamicProperty("copperComplete") == true) {
      form.button("Oxedised [§aCompleted§f]", "textures/items/copper_update/copper_chestplate")
    } else if (i.getDynamicProperty("locked10") == true) {
      form.button("Oxedised [§cLocked§f]", "textures/items/copper_update/copper_chestplate")
    }

    if (i.hasTag("unlocked_11")) {
      form.button("§fYumm!\n[§eUnlocked§f]", "textures/items/biome_update/grape")
    } else if (i.getDynamicProperty('busy11') == true) {
      form.button("Yumm [§dBusy§f]", "textures/items/biome_update/grape")
    } else if (i.getDynamicProperty("yummCompleted") == true) {
      form.button("Yumm [§aCompleted§f]", "textures/items/biome_update/grape")
    } else if (i.getDynamicProperty("locked11") == true) {
      form.button("Yumm [§cLocked§f]", "textures/items/biome_update/grape")
    }

    ///one button
    //button interactions
    form.show(i).then((response) => {
      let returnProperty = i.getDynamicProperty("wholeNewComplete")
      if (response.selection === 0 && i.hasTag("unlocked_4")) {
        bobStart(i)
      } else if (response.selection === 0 && i.getDynamicProperty('locked4') == true && !i.getDynamicProperty("wholeNewComplete") == true) {
        world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§cThis bounty is Locked. Complete an existing or busy bounty to unlock this one.\"}]}')
      } else if (response.selection === 0 && i.getDynamicProperty('busy4') == true) {
        bobStartInfo(i)
      } else if (response.selection === 0 && i.getDynamicProperty("wholeNewComplete") == true && i.getDynamicProperty('locked4') == true && i.getDynamicProperty("claimed4") == false) {
        bobStartComplete(i)
        world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§aYou completed this bounty.\"}]}')
      }

      if (response.selection === 1 && i.hasTag("unlocked_5")) {
        bobTp(i)
      } else if (response.selection === 1 && i.getDynamicProperty('locked5') == true && !i.getDynamicProperty("tpStoneCompleted") == true) {
        world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§cThis bounty is Locked. Complete an existing or busy bounty to unlock this one.\"}]}')
      } else if (response.selection === 1 && i.getDynamicProperty('busy5') == true) {
        bobTpInfo(i)
      } else if (response.selection === 1 && i.getDynamicProperty("tpStoneCompleted") == true && i.getDynamicProperty('locked5') == true && i.getDynamicProperty('claimed5') == false) {
        bobTpComplete(i)
        world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§aYou completed this bounty.\"}]}')
      }

      if (response.selection === 2 && i.hasTag("unlocked_6")) {
        bobTpTo(i)
      } else if (response.selection === 2 && i.getDynamicProperty('locked6') == true && !i.getDynamicProperty("tpToBountyComplete") == true) {
        world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§cThis bounty is Locked. Complete an existing or busy bounty to unlock this one.\"}]}')
      } else if (response.selection === 2 && i.getDynamicProperty('busy6') == true) {
        bobTpToInfo(i)
      } else if (response.selection === 2 && i.getDynamicProperty("tpToBountyComplete") == true && i.getDynamicProperty('locked6') == true && i.getDynamicProperty('claimed6') == false) {
        bobTpToComplete(i)
        world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§aYou completed this bounty.\"}]}')
      }

      if (response.selection === 3 && i.hasTag("unlocked_7")) {
        bobSpace(i)
      } else if (response.selection === 3 && i.getDynamicProperty('locked7') == true && !i.getDynamicProperty("spaceComplete") == true) {
        world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§cThis bounty is Locked. Complete an existing or busy bounty to unlock this one.\"}]}')
      } else if (response.selection === 3 && i.getDynamicProperty('busy7') == true) {
        bobSpaceInfo(i)
      } else if (response.selection === 3 && i.getDynamicProperty("spaceComplete") == true && i.getDynamicProperty('locked7') == true && i.getDynamicProperty('claimed7') == false) {
        bobSpaceComplete(i)
        world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§aYou completed this bounty.\"}]}')
      }

      if (response.selection === 4 && i.hasTag("unlocked_8")) {
        bobforge(i)
      } else if (response.selection === 4 && i.getDynamicProperty('locked8') == true && !i.getDynamicProperty("forgeCompleted") == true) {
        world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§cThis bounty is Locked. Complete an existing or busy bounty to unlock this one.\"}]}')
      } else if (response.selection === 4 && i.getDynamicProperty('busy8') == true) {
        bobforgeInfo(i)
      } else if (response.selection === 4 && i.getDynamicProperty("forgeCompleted") == true && i.getDynamicProperty('locked8') == true && i.getDynamicProperty('claimed8') == false) {
        bobforgeComplete(i)
        world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§aYou completed this bounty.\"}]}')
      }

      if (response.selection === 5 && i.hasTag("unlocked_9")) {
        bobvein(i)
      } else if (response.selection === 5 && i.getDynamicProperty('locked9') == true && !i.getDynamicProperty("mineBountyComplete") == true) {
        world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§cThis bounty is Locked. Complete an existing or busy bounty to unlock this one.\"}]}')
      } else if (response.selection === 5 && i.getDynamicProperty('busy9') == true) {
        bobveinInfo(i)
      } else if (response.selection === 5 && i.getDynamicProperty("mineBountyComplete") == true && i.getDynamicProperty('locked9') == true && i.getDynamicProperty('claimed9') == false) {
        bobveinComplete(i)
        world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§aYou completed this bounty.\"}]}')
      }
    })
}
// bob quests
function bobStart(i) {
  let form = new ActionFormData()
  form.title("Start Bounty?")
  form.body("Start your new journey?\nRewards: §a500 xp")
  form.button("Start Bounty!")
  form.button("Cancel")
  form.show(i).then((response) => {

      if (response.selection === 0 && !i.hasTag('busy 1')) {
        i.onScreenDisplay.setActionBar('new_journey')
        world.getDimension('overworld').runCommandAsync('xp 500 @p')
        i.removeTag("unlocked_4")
        i.addTag("unlocked_5")
        i.setDynamicProperty('wholeNewComplete', true)
        i.setDynamicProperty('betterBedrock', 9)
      }
      else if (response.selection === 1) {
        test(i)

      }
  })
}
function bobStartInfo(i) {
  let form = new ActionFormData()
    form.title("§fA new start")
    form.body("Start your new journey?\nRewards: §a500 xp")
    form.button("Ok")
          form.show(i).then((response) => {
            
          })
}
function bobStartComplete(i) {
  let form = new ActionFormData()
    form.title("§fA new start")
    form.body("Claim: §a500 xp")
    form.button("Claim")
          form.show(i).then((response) => {
            if(response.selection == 0){
              console.warn(randomXP)
              world.getDimension('overworld').runCommandAsync('xp 100 @p')
              i.setDynamicProperty('claimed', true)
              i.setDynamicProperty("xpVal", 100)
            }
          })
}

function bobTp(i) {
  let form = new ActionFormData()
  form.title("Start Bounty?")
  form.body("Craft or Obtain an Over Waystone.\nRewards: 1 Chainmail Chestplate\n50 xp")
  form.button("Start Bounty!")
  form.button("Cancel")
  form.show(i).then((response) => {
      if (response.selection === 0) {
        i.removeTag("unlocked_5")
        i.onScreenDisplay.setActionBar('waystone_start')
        i.setDynamicProperty('busy5', true)
        i.addTag('busy_3')
        //wooled_start
      }
      else if (response.selection === 1) {

    }
  })
}
function bobTpInfo(i) {
  let form = new ActionFormData()
    form.title("§fTeleport Stone?")
    form.body("Craft or Obtain an Over Waystone.\nRewards: 1 Chainmail Chestplate\n50 xp")
    form.button("Ok")
          form.show(i).then((response) => {
            
          })
}
function bobTpComplete(i) {
  let form = new ActionFormData()
    form.title("§fTeleport Stone?")
    form.body("Claim: 1 Chainmail Chestplate\n50 xp")
    form.button("Claim")
          form.show(i).then((response) => {
            if(response.selection == 0){
              world.getDimension('overworld').runCommandAsync('xp 50 @p')
              world.getDimension('overworld').runCommandAsync('give @p chainmail_chestplate 1')
              i.setDynamicProperty('claimed4', true)
              i.setDynamicProperty("xpVal", 300)
            }
          })
}

function bobTpTo(i) {
  let form = new ActionFormData()
  form.title("Start Bounty?")
  form.body("Teleport to your Waystone.\nRewards: 2 Obsidian\n50 xp")
  form.button("Start Bounty!")
  form.button("Cancel")
  form.show(i).then((response) => {
      if (response.selection === 0) {
        i.removeTag("unlocked_6")
        i.onScreenDisplay.setActionBar('tp_start')
        i.setDynamicProperty('busy6', true)
        i.addTag('busy_3')
        //wooled_start
      }
      else if (response.selection === 1) {

      }
  })
}
function bobTpToInfo(i) {
  let form = new ActionFormData()
    form.title("§fTelelport to Stone?")
    form.body("Teleport to your Waystone.\nRewards: 2 Obsidian\n50 xp")
    form.button("Ok")
          form.show(i).then((response) => {
            
          })
}
function bobTpToComplete(i) {
  let form = new ActionFormData()
    form.title("§fWooled")
    form.body("Claim: 2 Obsidian\n50 xp")
    form.button("Claim")
          form.show(i).then((response) => {
            if(response.selection == 0){
              world.getDimension('overworld').runCommandAsync('xp 50 @p')
              world.getDimension('overworld').runCommandAsync('give @p obsidian 2')
              i.setDynamicProperty('claimed6', true)
              i.setDynamicProperty("xpVal", 300)
            }
          })
}

function bobSpace(i) {
  let form = new ActionFormData()
  form.title("Start Bounty?")
  form.body("Craft a Backpack.\nRewards: 2 Copper Ingots\n50 xp")
  form.button("Start Bounty!")
  form.button("Cancel")
  form.show(i).then((response) => {
      if (response.selection === 0) {
        
        i.removeTag("unlocked_7")
        i.onScreenDisplay.setActionBar('backpack_start')
        i.setDynamicProperty('busy7', true)
        i.addTag('busy_3')
        //wooled_start
      }
      else if (response.selection === 1) {

      }
  })
}
function bobSpaceInfo(i) {
  let form = new ActionFormData()
    form.title("§fMore Space")
    form.body("Craft a Backpack.\nRewards: 2 Copper Ingots\n50 xp")
    form.button("Ok")
          form.show(i).then((response) => {
            
          })
}
function bobSpaceComplete(i) {
  let form = new ActionFormData()
    form.title("§fMore Space")
    form.body("Claim: 2 Copper Ingots\n50 xp")
    form.button("Claim")
          form.show(i).then((response) => {
            if(response.selection == 0){
              world.getDimension('overworld').runCommandAsync('xp 50 @p')
              world.getDimension('overworld').runCommandAsync('give @p copper_ingot 2')
              i.setDynamicProperty('claimed7', true)
              i.setDynamicProperty("xpVal", 300)
            }
          })
}

function bobforge(i) {
  let form = new ActionFormData()
  form.title("Start Bounty?")
  form.body("Craft a Forge Table.\nRewards: 2 Gold Ingots\n4 Planks\n100 xp")
  form.button("Start Bounty!")
  form.button("Cancel")
  form.show(i).then((response) => {
      if (response.selection === 0) {
        
        i.removeTag("unlocked_8")
        i.onScreenDisplay.setActionBar('forge_start')
        i.setDynamicProperty('busy8', true)
        i.addTag('busy_3')
        //wooled_start
      }
      else if (response.selection === 1) {

      }
  })
}
function bobforgeInfo(i) {
  let form = new ActionFormData()
    form.title("§fForging Time")
    form.body("Craft a Forge Table.\nRewards: 2 Gold Ingots\n4 Planks\n100 xp")
    form.button("Ok")
          form.show(i).then((response) => {
            
          })
}
function bobforgeComplete(i) {
  let form = new ActionFormData()
    form.title("§fForging Time")
    form.body("Claim: 2 Gold Ingots\n4 Planks\n100 xp")
    form.button("Claim")
          form.show(i).then((response) => {
            if(response.selection == 0 && i.getDynamicProperty('claimed8') == false){
              world.getDimension('overworld').runCommandAsync('xp 100 @p')
              world.getDimension('overworld').runCommandAsync('give @p gold_ingot 2')
              world.getDimension('overworld').runCommandAsync('give @p planks 4')
              i.setDynamicProperty('claimed8', true)
              i.setDynamicProperty("xpVal", 300)
            }
          })
}

function bobvein(i) {
  let form = new ActionFormData()
  form.title("Start Bounty?")
  form.body("Craft a Miners Bench\nRewards: 1 Enchanted Book: Vein Miner I\n100 xp")
  form.button("Start Bounty!")
  form.button("Cancel")
  form.show(i).then((response) => {
      if (response.selection === 0) {
        
       i.removeTag("unlocked_9")
        i.onScreenDisplay.setActionBar('miner_start')
        i.setDynamicProperty('busy9', true)
        i.addTag('busy_3')
        //wooled_start
      }
      else if (response.selection === 1) {

      }
  })
}
function bobveinInfo(i) {
  let form = new ActionFormData()
    form.title("§fMiners Dream")
    form.body("Craft a Miners Bench\nRewards: 1 Enchanted Book: Vein Miner I\n100 xp")
    form.button("Ok")
          form.show(i).then((response) => {
            
          })
}
function bobveinComplete(i) {
  let form = new ActionFormData()
    form.title("§fMiners Dream")
    form.body("Claim: 1 Enchanted Book: Vein Miner I\n100 xp")
    form.button("Claim")
          form.show(i).then((response) => {
            if(response.selection == 0){
              world.getDimension('overworld').runCommandAsync('xp 100 @p')
              world.getDimension('overworld').runCommandAsync('give @p pog:vein_miner_book 1')
              i.setDynamicProperty('claimed9', true)
              i.setDynamicProperty("xpVal", 300)
            }
          })
}

  //main bounty UI
  function test(i) {
    let form = new ActionFormData()
      form.title("§fA new start")
      form.body("Complete Bounties to unlock more. Completing a bounty will unlock another one.")
      ///one button
      if (i.hasTag("unlocked_1")) {
        form.button("§fA new start\n[§eUnlocked§f]", "textures/items/coal")
      } else if (i.getDynamicProperty('busy') == true) {
        form.button("A new start [§dBusy§f]", "textures/items/coal")
      } else if (i.getDynamicProperty("newStartComplete") == true) {
        form.button("A new start [§aCompleted§f]", "textures/items/coal")
      }

      if (i.hasTag("unlocked_2")) {
        form.button("§fGetting an Upgrade\n[§eUnlocked§f]", "textures/items/stone_pickaxe")
      } else if (i.getDynamicProperty('busy2') == true) {
        form.button("Getting an Upgrade [§dBusy§f]", "textures/items/stone_pickaxe")
      } else if (i.getDynamicProperty("upgradeComplete") == true) {
        form.button("Getting an Upgrade [§aCompleted§f]", "textures/items/stone_pickaxe")
      } else if (i.getDynamicProperty("locked2") == true) {
        form.button("Getting an Upgrade [§cLocked§f]", "textures/items/stone_pickaxe")
      }

      if (i.hasTag("unlocked_3")) {
        form.button("§fDown we go\n[§eUnlocked§f]", "textures/items/stone_pickaxe")
      } else if (i.getDynamicProperty('busy3') == true) {
        form.button("Down we go [§dBusy§f]", "textures/items/stone_pickaxe")
      } else if (i.getDynamicProperty("downGoCompleted") == true) {
        form.button("Down we go [§aCompleted§f]", "textures/items/stone_pickaxe")
      } else if (i.getDynamicProperty("locked3") == true) {
        form.button("Down we go [§cLocked§f]", "textures/items/stone_pickaxe")
      }

      ///one button
      //button interactions
      form.show(i).then((response) => {
        let returnProperty = i.getDynamicProperty("newStartComplete")
        if (response.selection === 0 && i.hasTag("unlocked_1")) {
          coal_bounty(i)
        } else if (response.selection === 0 && i.getDynamicProperty('locked') == true && !i.getDynamicProperty("newStartComplete") == true) {
          world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§cThis bounty is Locked. Complete an existing or busy bounty to unlock this one.\"}]}')
        } else if (response.selection === 0 && i.getDynamicProperty('busy') == true) {
          coalBountyInfo(i)
        } else if (response.selection === 0 && i.getDynamicProperty("newStartComplete") == true && i.getDynamicProperty('locked') == true && i.getDynamicProperty("claimed") == false) {
          coalBountyComplete(i)
          world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§aYou completed this bounty.\"}]}')
        }

        if (response.selection === 1 && i.hasTag("unlocked_2")) {
          nether_bounty(i)
        } else if (response.selection === 1 && i.getDynamicProperty('locked2') == true && !i.getDynamicProperty("upgradeComplete") == true) {
          world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§cThis bounty is Locked. Complete an existing or busy bounty to unlock this one.\"}]}')
        } else if (response.selection === 1 && i.getDynamicProperty('busy2') == true) {
          upgradelBountyInfo(i)
        } else if (response.selection === 1 && i.getDynamicProperty("upgradeComplete") == true && i.getDynamicProperty('locked2') == true && i.getDynamicProperty('claimed2') == false) {
          upgradeBountyComplete(i)
          world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§aYou completed this bounty.\"}]}')
        }

        if (response.selection === 2 && i.hasTag("unlocked_3")) {
          Wooled(i)
        } else if (response.selection === 2 && i.getDynamicProperty('locked3') == true && !i.getDynamicProperty("downGoCompleted") == true) {
          world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§cThis bounty is Locked. Complete an existing or busy bounty to unlock this one.\"}]}')
        } else if (response.selection === 2 && i.getDynamicProperty('busy3') == true) {
          WooledBountyInfo(i)
        } else if (response.selection === 2 && i.getDynamicProperty("downGoCompleted") == true && i.getDynamicProperty('locked3') == true && i.getDynamicProperty('claimed3') == false) {
          WooledBountyComplete(i)
          world.getDimension('overworld').runCommandAsync('tellraw @a {\"rawtext\": [{\"text\":\"§aYou completed this bounty.\"}]}')
        }
      })

  }
  function coalBountyInfo(i) {
    let form = new ActionFormData()
      form.title("§fA new start")
      form.body("Obtain your first log.\n\nRewards: 100Xp")
      form.button("Ok")
            form.show(i).then((response) => {
              
            })
  }
  function coalBountyComplete(i) {
    let form = new ActionFormData()
      form.title("§fA new start")
      form.body("Claim: 100Xp")
      form.button("Claim")
            form.show(i).then((response) => {
              if(response.selection == 0){
                console.warn(randomXP)
                world.getDimension('overworld').runCommandAsync('xp 100 @p')
                i.setDynamicProperty('claimed', true)
                i.setDynamicProperty("xpVal", 100)
              }
            })
  }
  function upgradelBountyInfo(i) {
    let form = new ActionFormData()
      form.title("§fGetting an upgrade")
      form.body("Make a wooden pickaxe, then upgrade it to a stone pickaxe, then an iron pickaxe.\nRewards: 4 apples\n60 xp")
      form.button("Ok")
            form.show(i).then((response) => {
              
            })
  }
  function upgradeBountyComplete(i) {
    let form = new ActionFormData()
      form.title("§fGetting an upgrade")
      form.body("Claim: 7 Iron Ingots, 60xp")
      form.button("Claim")
            form.show(i).then((response) => {
              if(response.selection == 0){
                world.getDimension('overworld').runCommandAsync('xp 60 @p')
                world.getDimension('overworld').runCommandAsync('give @p apple 4')
                i.setDynamicProperty('claimed2', true)
                i.setDynamicProperty("xpVal", 200)
              }
            })
      }
  function WooledBountyInfo(i) {
    let form = new ActionFormData()
      form.title("§fWooled")
      form.body("Get yourself 3 wool and make a Bed.\nRewards: 2 Iron Ingots\n100 xp")
      form.button("Ok")
            form.show(i).then((response) => {
              
            })
  }
  function WooledBountyComplete(i) {
    let form = new ActionFormData()
      form.title("§fWooled")
      form.body("Claim: 2 Iron Ingots\n100 xp")
      form.button("Claim")
            form.show(i).then((response) => {
              if(response.selection == 0 && i.getDynamicProperty('claimed3') == false){
                world.getDimension('overworld').runCommandAsync('xp 100 @p')
                world.getDimension('overworld').runCommandAsync('give @p iron_ingot 2')
                i.setDynamicProperty('claimed3', true)
                i.setDynamicProperty("xpVal", 300)
              }
            })
      }
  //item usage
  world.events.beforeItemUse.subscribe(data => {
    let {source: i} = data
    let inv = i.getComponent("inventory").container;
       const itemStack = inv.getItem(i.selectedSlot);
          let newItem = new ItemStack(ItemTypes?.get("pog:bounty_paper_open"));
    if (itemStack?.typeId == "pog:bounty_paper_open") {
      
        bounty_tier_page(i)
    }
    //bounty scroll being opened with no command delay.
    if (itemStack?.typeId == "pog:quest_paper") {
      inv.setItem(i.selectedSlot, newItem);
    }
  }
  )