import * as mc from "@minecraft/server"

import { ActionFormData, MessageFormData, ModalFormData } from "@minecraft/server-ui"

 
export function getScore(objective, target, useZero = false) {
    try {
        const oB = mc.world.scoreboard.getObjective(objective)
        if (typeof target == 'string') return oB.getScore(oB.getParticipants().find(pT => pT.displayName == target))
        return oB.getScore(target.scoreboard)
    } catch {
        return useZero ? 0 : NaN
    }
}
    
    const msg0 = new MessageFormData()
    msg0.title("Upgrade Statistics | Inventory")
    msg0.body("Level 1 -> Level 2 = XP: 2L \n Level 2 -> Level 3 = XP: 4L \n Level 3 -> Level 4 = XP: 6L \n Level 4 -> Level 5 = XP: 7L \n Level 5 -> Level 6 = XP: 8L \n Level 6 -> Level 7 = XP: 10L \n Level 7 -> Level 8 = XP: 12L \n Level 8 -> Level 9 = XP: 13L \n Level 9 -> Level 10 = XP: 14L")
    msg0.button1("§aConfirm")
    msg0.button2("§cCancel")
    
    const msg1 = new MessageFormData()
    msg1.title("Upgrade Statistics | Immunity")
    msg1.body("Level 1 -> Level 2 = XP: 40L (hunger) \n Level 2 -> Level 3 = XP: 42L (slowness) \n Level 3 -> Level 4 = XP: 44L (mining fatigue) \n Level 4 -> Level 5 = XP: 46L (poison) \n Level 5 -> Level 6 = XP: 48L (weakness) \n Level 6 -> Level 7 = XP: 50L (nausea) \n Level 7 -> Level 8 = XP: 52L (blindness) \n Level 8 -> Level 9 = XP: 54L (wither) \n Level 9 -> Level 10 = XP: 56L (darkness)")
    msg1.button1("§aConfirm")
    msg1.button2("§cCancel")
    
     const msg2 = new MessageFormData()
    msg2.title("Upgrade Statistics | Effects capacity")
    msg2.body("Level 1 -> Level 2 = XP: 2L (speed,conduit_power...) \n Level 2 -> Level 3 = XP: 4L (jump_boost,absorption) \n Level 3 -> Level 4 = XP: 6L (strength,night_vision) \n Level 4 -> Level 5 = XP: 7L (resistance,haste) \n Level 5 -> Level 6 = XP: 8L (fire_resistance) \n Level 6 -> Level 7 = XP: 10L (water_breathing,saturation)\n Level 7 -> Level 8 = XP: 12L (slow_falling)\n Level 8 -> Level 9 = XP: 13L (health_boost) \n Level 9 -> Level 10 = XP: 14L (invisibility)")
    msg2.button1("§aConfirm")
    msg2.button2("§cCancel")
    
    const msg3 = new MessageFormData()
    msg3.title("Upgrade Statistics | Protection")
    msg3.body("Level 1 -> Level 2 = XP: 6L (boots) \n Level 2 -> Level 3 = XP: 8L (helmet) \n Level 3 -> Level 4 = XP: 10L (leggings) \n Level 4 -> Level 5 = XP: 12L (chestplate) \n Level 5 -> Level 6 = XP: 14L (offhand slot)")
    msg3.button1("§aConfirm")
    msg3.button2("§cCancel")

mc.world.events.beforeItemUse.subscribe(eventData => {

    if (eventData.item.typeId == "blayy:statistic_book") {

        const player = eventData.source
    const inventory = getScore("inventory_score", player)
    const protection = getScore("protection_score", player)
    const effects = getScore("effects_score", player)
    const immunity = getScore("immunity_score", player)
    const action = new ActionFormData()

    action.title("Upgrade Statistics")
    action.body("§lClick to Upgrade")
    if (inventory == 10) {
    action.button("Inventory | §2LEVEL 10 (MAX) ", "textures/blocks/chest_front")
    }else{
      action.button("Inventory | §2LEVEL " + inventory, "textures/blocks/chest_front")
}
    if (protection == 6) {
    action.button("Protection | §2LEVEL 6 (MAX)", "textures/items/iron_chestplate")
    }else{
    action.button("Protection | §2LEVEL " + protection, "textures/items/iron_chestplate")
  }
    if (effects == 10) {
    action.button("Effects | §2LEVEL 10 (MAX)", "textures/ui/absorption_effect")
    }else{
    action.button("Effects | §2LEVEL " + effects, "textures/ui/absorption_effect")
    }
    if (immunity == 10) {
    action.button("Immunity | §2LEVEL 10 (MAX)", "textures/ui/trade_icon")
    }else{
      action.button("Immunity | §2LEVEL " + immunity, "textures/ui/trade_icon")
}
        action.show(player).then(response => {
          if (response.selection == 0) {
             msg0.show(player).then(sla => {
               if (sla.selection == 1) {
                 player.runCommandAsync(`function invv`)
               }
             })
          }
          if (response.selection == 3) {
             msg1.show(player).then(slaa => {
               if (slaa.selection == 1) {
                 player.runCommandAsync(`function imune`)
               }
             })
          }
          if (response.selection == 2) {
             msg2.show(player).then(slaaa => {
               if (slaaa.selection == 1) {
                 player.runCommandAsync(`function effects12`)
               }
             })
          }
          if (response.selection == 1) {
             msg3.show(player).then(slaaaa => {
               if (slaaaa.selection == 1) {
                 player.runCommandAsync(`function protecao1`)
               }
             })
          }
                
        })
    }
})
