import * as mc from "@minecraft/server"
import "./before/fire_staff/index"
import "./before/air_staff/index"
import "./before/spirit_staff/index"
import "./before/electric_staff/index"
import "./before/water_staff/index"
import "./before/earth_glove/index"
import "./cetro_do_vazio/index"
import "./glaive_congelada/index"
import "./martelo_de_metal/index"
import "./punhos_derretidos/index"
import * as lib from "../lib"
import "./amors"
import "./settings"
import "./guide_book"
import "./elemental_table"

export const entitiesTypes = [
    {
        entities: new Set([
            "minecraft:cave_spider",
            "minecraft:drowned",
            "minecraft:endermite",
            "minecraft:husk",
            "minecraft:magma_cube",
            "minecraft:silverfish",
            "minecraft:skeleton",
            "minecraft:slime",
            "minecraft:spider",
            "minecraft:stray",
            "minecraft:zombie",
            "minecraft:zombie_pigman",
            "minecraft:zombie_villager",
            "minecraft:zombie_villager_v2"
        ]),
        xp: 1
    },
    {
        entities: new Set([
            "minecraft:blaze",
            "minecraft:creeper",
            "minecraft:enderman",
            "minecraft:ghast",
            "minecraft:guardian",
            "minecraft:hoglin",
            "minecraft:phantom",
            "minecraft:shulker",
            "minecraft:vex",
            "minecraft:zoglin"
        ]),
        xp: 3
    },
    {
        entities: new Set([
            "minecraft:evocation_illager",
            "minecraft:piglin",
            "minecraft:piglin_brute",
            "minecraft:pillager",
            "minecraft:ravager",
            "minecraft:vindicator",
            "minecraft:witch",
            "minecraft:wither_skeleton"
        ]),
        xp: 4
    },
    {
        entities: new Set([
            "minecraft:elder_guardian",

            "ld_eb:ld_eb_glacier_colossus",
            "ld_eb:ld_eb_magma_warden",
            "ld_eb:ld_eb_steel_behemoth",
            "ld_eb:void_monster",
            "ld_eb:air_elemental",
            "ld_eb:earth_elemental",
            "ld_eb:electricity_elemental",
            "ld_eb:fire_elemental",
            "ld_eb:spirit_elemental",
            "ld_eb:water_elemental",
        ]),
        xp: 50
    },
    {
        entities: new Set([
            "minecraft:ender_dragon",
            "minecraft:wither",
            "minecraft:warden"
        ]),
        xp: 150
    }
]

mc.system.afterEvents.scriptEventReceive.subscribe(data => {
    const id = data.id,
        player = data.sourceEntity
    if (id === "ld_be:reset") {
        player.getDynamicPropertyIds().filter(id => id.startsWith("ld_eb:weapons") && id.endsWith("init")).forEach(id => {
            player.setDynamicProperty(id, undefined)
        })
    }
})

mc.world.afterEvents.entityLoad.subscribe(data => {
    const entity = data.entity
    entity.dimension.getEntities({
        location: entity.location,
        maxDistance: 1,
        families: ["ld_eb:none"],
        closest: 1
    }).forEach(e => {
        switch (e.typeId) {
            case "ld_eb:ld_eb_frostbound_glaive.glacial_cataclysm": {
                e.remove()
                break
            }
            case "ld_eb:none_entity": {
                const skillType = e.getDynamicProperty("ld_eb:skill_type")
                if (skillType === "ld_eb:ld_eb_frostbound_glaive.glacial_cataclysm") {
                    const radius = e.getDynamicProperty("ld_eb:radius") ?? 0
                    try {
                        e.runCommand(
                            `fill ~${radius + 1} ~${radius + 1} ~${radius + 1} ~-${radius + 1} ~-${radius + 1} ~-${radius + 1} air replace barrier`
                        )
                    } catch (err) {
                        //console1-console.warn(`Falha ao executar o comando de preenchimento: ${err}`)
                    }
                    e.remove()
                }
                break
            }
        }
    })
})


mc.world.afterEvents.playerSpawn.subscribe(data => {
    const player = data.player
    if (player.getDynamicProperty("ld_eb:is_in_ult")) {
        lib.finalUlt(player)
    }
})