import { world, Items, ItemStack, MinecraftEntityTypes, DynamicPropertiesDefinition, SoundOptions, 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 iCompShowTick = new DynamicPropertiesDefinition()


    eventData.propertyRegistry.registerEntityTypeDynamicProperties(iCompShowTick, MinecraftEntityTypes["player"])
  })

  //UI
  function bounty_tier_page(i) {
    let form = new ActionFormData()
      form.title("§fBounties")
      form.body("Welcome to the bounty screen. Select an exisitng bounty, or a newly unlocked bounty and follow the instructions. Each bounty will have a difficulty indicated by 'I, II, III, IV, V'.")
      if(i.hasTag("cow_bounty_open")){
      form.button("Cow Slayer I §dOpen")
        } else if(i.hasTag("cow_bounty_busy")) {
      form.button("Cow Slayer I §cSearching Bounty...")
     } else if(i.hasTag("cow_bounty_completed") && !i.hasTag("cow_bounty_busy")) {
      form.button("Cow Slayer I §aBounty Found!")
     }

      if(i.hasTag("pig_bounty_open")){
        form.button("Pig Slayer I §dOpen")
          } else if(i.hasTag("pig_bounty_busy")) {
            form.button("Pig Slayer I §cSearching Bounty")
       } else if(i.hasTag("pig_bounty_completed")) {
        form.button("Pig Slayer I §aBounty Found")
       }

       if(i.hasTag("sheep_bounty_open")){
        form.button("Sheep Slayer II §dOpen")
          } else if(i.hasTag("sheep_bounty_busy")) {
            form.button("Sheep Slayer II §cSearching Bounty")
       } else if(i.hasTag("sheep_bounty_completed")) {
        form.button("Sheep Slayer II §aBounty Found")
       }
            form.show(i).then((response) => {
              if(response.selection == 0){
                if(i.hasTag("cow_bounty_open")){
                cow_slayer(i)
                } else if(i.hasTag("cow_bounty_busy")) {
                  cow_slayer_about(i)
              } else if(i.hasTag("cow_bounty_completed")){
                if(i.hasTag("cow_claimed_I")){
                cow_slayer_claim(i)
              }}
            }
            if(response.selection == 1){
              if(i.hasTag("pig_bounty_open")){
              pig_slayer(i)
              } else if(i.hasTag("pig_bounty_busy")) {
                pig_slayer_about(i)
            } else if(i.hasTag("pig_bounty_completed")){
              if(i.hasTag("pig_claimed_I")){
              pig_slayer_claim(i)
            }}
          }
          if(response.selection == 2){
            if(i.hasTag("sheep_bounty_open")){
            sheep_slayer(i)
            } else if(i.hasTag("sheep_bounty_busy")) {
              sheep_slayer_about(i)
          } else if(i.hasTag("sheep_bounty_completed")){
            if(i.hasTag("sheep_claimed_II")){
            sheep_slayer_claim(i)
          }}
        }
          })
  }

  function cow_slayer(i) {
    let form = new ActionFormData()
      form.title("Cow Slayer I")
      form.body("Hunt down 3 cows.\nRewards: 20xp, 22 coal")
      form.button("Start Hunt")
      form.button("Not Now")
            form.show(i).then((response) => {
              if(response.selection == 0){
                i.removeTag("cow_bounty_open")
                i.addTag("cow_bounty_started")
              }
              if(response.selection == 1){
              }
            })
  }
  function cow_slayer_about(i) {
    let form = new ActionFormData()
      form.title("Cow Slayer I")
      form.body("Hunt down 3 cows.\nRewards: 20xp, 22 coal")
      form.button("Got It!")
            form.show(i).then((response) => {
              if(response.selection == 0){

              }
            })
  }
   function cow_slayer_claim(i) {
    let form = new ActionFormData()
      form.title("Cow Slayer I")
      form.body("You hunted down 3 cows so you can claim your rewards:\nRewards: 20xp, 22 coal")
      form.button("Got It!")
            form.show(i).then((response) => {
              if(response.selection == 0){
                if(i.hasTag("cow_claimed_I")){
                  i.removeTag("cow_claimed_I")
                i.runCommandAsync("give @s coal 22")
              }}
           })
      
  }

  function pig_slayer(i) {
    let form = new ActionFormData()
      form.title("Pig Slayer I")
      form.body("Hunt down 3 Pigs.\nRewards: 20xp, 4 Wild Carrots")
      form.button("Start Hunt")
      form.button("Not Now")
            form.show(i).then((response) => {
              if(response.selection == 0){
                i.removeTag("pig_bounty_open")
                i.addTag("pig_bounty_started")
              }
              if(response.selection == 1){
              }
            })
  }
  function pig_slayer_about(i) {
    let form = new ActionFormData()
      form.title("Pig Slayer I")
      form.body("Hunt down 3 Pigs.\nRewards: 20xp, 4 Wild Carrots")
      form.button("Got It!")
            form.show(i).then((response) => {
              if(response.selection == 0){

              }
            })
  }
   function pig_slayer_claim(i) {
    let form = new ActionFormData()
      form.title("Pig Slayer II")
      form.body("You hunted down 3 Pigs so you can claim your rewards:\nRewards: 20xp, 4 Wild Carrots")
      form.button("Got It!")
            form.show(i).then((response) => {
              if(response.selection == 0){
                if(i.hasTag("pig_claimed_I")){
                  i.removeTag("pig_claimed_I")
                i.runCommandAsync("give @s pog:wild_carrot_food 4")
              }}
            })
  }

  function sheep_slayer(i) {
    let form = new ActionFormData()
      form.title("sheep Slayer I")
      form.body("Hunt down 6 Sheeps.\nRewards: 20xp, 3 Iron Ingots")
      form.button("Start Hunt")
      form.button("Not Now")
            form.show(i).then((response) => {
              if(response.selection == 0){
                i.removeTag("sheep_bounty_open")
                i.addTag("sheep_bounty_started")
              }
              if(response.selection == 1){
              }
            })
  }
  function sheep_slayer_about(i) {
    let form = new ActionFormData()
      form.title("sheep Slayer I")
      form.body("Hunt down 6 Sheeps.\nRewards: 20xp, 3 Iron Ingots")
      form.button("Got It!")
            form.show(i).then((response) => {
              if(response.selection == 0){

              }
            })
  }
   function sheep_slayer_claim(i) {
    let form = new ActionFormData()
      form.title("sheep Slayer II")
      form.body("You hunted down 3 sheeps so you can claim your rewards:\nRewards: 20xp, 3 Iron Ingots")
      form.button("Got It!")
            form.show(i).then((response) => {
              if(response.selection == 0){
                if(i.hasTag("sheep_claimed_I")){
                  i.removeTag("sheep_claimed_I")
                i.runCommandAsync("give @s iron_ingot 3")
              }}
            })
  }




  //item usage
  world.events.beforeItemUse.subscribe(data => {
    let { source: i, item } = data
      let inv = i.getComponent("inventory").container;
          let newItem = new ItemStack(ItemTypes?.get("pog:quest_scroll_opened"));

    if (item.typeId == "pog:quest_scroll_opened") {

        bounty_tier_page(i)
    }
    //bounty scroll being opened with no command delay.
    if (item.typeId == "pog:quest_scroll_closed") {
      i.addTag("cow_bounty_open")
      inv.setItem(i.selectedSlot, newItem);
    }
  }
  )