import * as mc from "@minecraft/server"
import * as ui from "@minecraft/server-ui"
import { maxLevel } from "./items/settings.js"

// const position = {
//     x: 5,
//     y: 1,
//     z: 9
// }
// mc.system.runInterval(() => {
//     const dimension = mc.world.getDimension("overworld")
//     const entities = dimension.getEntities({
//         type: "minecraft:husk",
//         location: position,
//         minDistance: 25
//     })
//     entities.forEach((entity, index) => {
//         entity.teleport(position)
//     })
// })

// mc.world.afterEvents.entityDie.subscribe(data => {
//     const entity = data.deadEntity
//     entity.dimension.getEntities({
//         type: "minecraft:husk"
//     }).forEach(entity => {
//         entity.remove()
//     })
//     entity.dimension.spawnEntity("minecraft:husk", position)
// })

// import("./items/")


let array = [
    import("./items/cetro_do_vazio/item.js"),
    import("./items/glaive_congelada/item.js"),
    import("./items/martelo_de_metal/item.js"),
    import("./items/punhos_derretidos/item.js"),
    import("./items/before/air_staff/item.js"),
    import("./items/before/earth_glove/item.js"),
    import("./items/before/electric_staff/item.js"),
    import("./items/before/fire_staff/item.js"),
    import("./items/before/spirit_staff/item.js"),
    import("./items/before/water_staff/item.js"),
]

// Esperar todos os módulos serem carregados
Promise.all(array).then((itens) => {
    const newArray = []
    itens.forEach(a => {
        const item = a.itemStack
        ////console1-console.warn(item.typeId)
        newArray.push(item.typeId)
    })
    array = newArray
})
const weapons = [
    "ld_eb:air_staff",
    "ld_eb:earth_glove",
    "ld_eb:electricity_staff",
    "ld_eb:fire_staff",
    "ld_eb:spirit_staff",
    "ld_eb:water_staff",
    "ld_eb:weapons.glaive_congelada.normal",
    "ld_eb:weapons.punhos_derretidos.normal",
    "ld_eb:weapons.martelo_de_metal.normal",
    "ld_eb:weapons.cetro_do_vazio.normal"
]
mc.system.afterEvents.scriptEventReceive.subscribe(data => {
    const id = data.id
    const player = data.sourceEntity
    if (id === "ld_eb:levelmax") {
        weapons.forEach(element => {
            //console1-console.warn(element)
            player.setDynamicProperty(element + ".init", true)
            player.setDynamicProperty(element + ".lvl", 10)
            player.setDynamicProperty(element + ".xp", 0)
            player.setDynamicProperty(element + ".ult", 10)
        })
    } else if (id === "ld_eb:levelmin") {
        array.forEach(element => {
            // //console1-console.warn(element)
            player.setDynamicProperty(element + ".init", true)
            player.setDynamicProperty(element + ".lvl", 0)
            player.setDynamicProperty(element + ".xp", 0)
            player.setDynamicProperty(element + ".xpm", 10)
        })
    }
})

mc.system.afterEvents.scriptEventReceive.subscribe(data => {
    const id = data.id,
        player = data.sourceEntity
    if (id === "ld_eb:max_ult") {
        array.forEach(element => {
            // //console1-console.warn(element)
            player.setDynamicProperty(element + ".ult", 10)
        })
    } else if (id === "ld_eb:min_ult") {
        array.forEach(element => {
            // //console1-console.warn(element)
            player.setDynamicProperty(element + ".ult", 0)
        })
    }
})

// mc.world.afterEvents.entityHurt.subscribe(data => {
//     //console1-console.warn(data.damage)
// })

// test book system

mc.world.afterEvents.entityHitEntity.subscribe(async data => {
    const entity = data.hitEntity
    if (entity.typeId !== "ld_eb:book_teste") return
    entity.setProperty("ld_eb:page", entity.getProperty("ld_eb:page") >= 11 ? 1: entity.getProperty("ld_eb:page") + 1)
    entity.playAnimation("animation.ld_eb_book_teste")
    // await mc.system.waitTicks(0.4 * 20)
})