import * as mc from "@minecraft/server"
import { itemStack, startAnimations } from "./item"
import * as lib from "../../lib"

const type = {
    delay: 5,
    damage: lib.damage.moderado,
    damageMultiplier: 1
}

mc.world.afterEvents.itemUse.subscribe(async data => {
    const item = data.itemStack,
        player = data.source
        if (item.typeId !== itemStack.typeId) return
        if (player.getItemCooldown(item.typeId) !== 0) return
        if (player.isSneaking || !player.isOnGround) return
        player.startItemCooldown(item.typeId, type.delay*20)
        player.onScreenDisplay.setActionBar({
            translate: "actionbar.ld_eb:ld_eb_frostbound_glaive.ice_shard_barrage"    
        })
        
        lib.animation(player, "animation.ld_eb_frostbound_glaive.ice_shard_barrage", itemStack.typeId, 1, false)
        lib.animation(player, "animation.ld_eb_frostbound_glaive_fpp.ice_shard_barrage", itemStack.typeId, 2, false)

        await mc.system.waitTicks(0.2 * 20)
        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null: player.playSound("ld_eb.airblow", { pitch: lib.random(0.7, 1.3) })
        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null: player.playSound("ld_eb.ice_smash", { pitch: lib.random(1.3, 1.9) })
        /** @type { mc.Entity } */
        const spell = lib.spell(
            "ld_eb:ld_eb_frostbound_glaive.ice_shard_barrage",
            player, 
            { z: 2, y: 1.8 },
            { y: player.getRotation().y },
            false
        )
        lib.rayCast(player, 4, 3, 
            /** @param  { mc.Player } target */
            function(target) {
                // //console1-console.warn(type.damage + ((type.damageMultiplier - 1) + ((type.damage * (player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) - type.damage))))
                target.applyDamage(type.damage + ((type.damageMultiplier - 1) + ((type.damage * (player.getDynamicProperty(item.typeId + ".lvl") >= 10 ? 3: player.getDynamicProperty(item.typeId + ".lvl") >= 5 ? 2: 1) - type.damage))),  {
                    cause: "contact",
                    damagingEntity: player
                })
                if (target.typeId === "minecraft:player") {
                    target.runCommand("/inputpermission set @s movement disabled");
                    target.ld_eb_init_function("/inputpermission set @s movement enabled");
                    
                    mc.system.runTimeout(() => {
                        target.runCommand("/inputpermission set @s movement enabled");
                        target.ld_eb_remove_function("/inputpermission set @s movement enabled");
                    }, 3 * 20);
                } else {
                    target.addEffect("slowness", 3 * 20, {
                        amplifier: 255,
                        showParticles: false
                    });
                }                
            }
        )

        await mc.system.waitTicks(0.2 * 20)
})