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

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

mc.Entity.prototype.ld_eb_location_after = 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_void_scepter.shadow_grip"
    })
    lib.animation(player, "animation.ld_eb_void_scepter_fpp.shadow_grip", itemStack.typeId, 1, false)
    lib.animation(player, "animation.ld_eb_void_scepter.shadow_grip", itemStack.typeId, 2, false)

    await mc.system.waitTicks(0.7 * 20)
    !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null: player.playSound("ld_eb.airblow", { pitch: lib.random(0.6, 0.7) })
    lib.rayCast(player, 12, 0,
        /** @param  { mc.Player } target */
        async function (target) {
            /** @type { mc.Entity } */
            const spell = lib.spell(
                "ld_eb:ld_eb_void_scepter.shadow_grip",
                target,
                { z: lib.random(1, 2) === 1 ? 1 : -1, y: 0.1, x: lib.random(1, 2) === 1 ? 1 : -1 },
                { y: target.getRotation().y },
                true,
                false
            )
            spell.teleport(spell.location, {
                facingLocation: target.location
            })
            spell.owner = player
            const tick = mc.system.runInterval(async () => {
                if (!spell.isValid()||!target.isValid()) {
                    return mc.system.clearRun(tick)
                }
                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
                })
                target.ld_eb_location_after === null ? target.ld_eb_location_after = target.location : null
                target.teleport(target.ld_eb_location_after)
            }, 0.1 * 20)
            await mc.system.waitTicks(0.7 * 20)
            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.6), {
                cause: "contact",
                damagingEntity: player
            })
            target.ld_eb_location_after = null

            await mc.system.waitTicks(2 * 20)
            spell.remove()
        }
    )

    await mc.system.waitTicks(2 * 20)
    startAnimations(player)
})