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

const type = {
    delay: 4,
    damage: lib.damage.baixo,
    damageMultiplier: 1.3
}

mc.Entity.prototype.owner = null
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_metal_hammer.metal_shield"    
        })
        
        lib.animation(player, "animation.ld_eb_metal_hammer.metal_shield", itemStack.typeId, 1, false)
        lib.animation(player, "animation.ld_eb_metal_hammer_fpp.metal_shield", itemStack.typeId, 2, false)

        await mc.system.waitTicks(0.6 * 20)
        /** @type { mc.Entity } */
        const spell = lib.spell(
            "ld_eb:ld_eb_metal_hammer.metal_shield",
            player,
            { z: 0, y: 0.1 },
            { y: player.getRotation().y },
            false,
            false
        )
        spell.owner = player
        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null: player.playSound("ld_eb.ice_armor", { pitch: lib.random(0.6, 0.7) })
        const run = () => {
            player.inputPermissions.movementEnabled = false
            player.addEffect("regeneration", 2*20, {
                amplifier: 1,
                showParticles: false
            })
            lib.rayCast(spell, 5, 1, 
                /** @param  { mc.Entity } target */
                function(target) {
                    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)))) * 0.1,  {
                        cause: "contact",
                        damagingEntity: player
                    })
                    lib.middleImpulse(target, spell, -0.6, 0.001)
                    target.addEffect("slowness", 2*20, {
                        amplifier: 1,
                        showParticles: false
                    })
                },
                [player]
            )
        }
        run()
        const tick = mc.system.runInterval(() => {
            if (!spell.isValid()) {
                !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null: player.playSound("ld_eb.ice_armor", { pitch: lib.random(1.4, 1.6) })
                return mc.system.clearRun(tick)
            }
            run()
        }, 0.1*20)

        await mc.system.waitTicks(0.2 * 20)
        startAnimations(player)

        await mc.system.waitTicks(3 * 20)
        spell.remove()

        
        await mc.system.waitTicks(1 * 20)
        player.inputPermissions.movementEnabled = true
})