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

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

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
    if (player.getDynamicProperty("ld_eb:is_in_ult")) return
    if (player.getDynamicProperty(item.typeId + ".lvl") < 5) {
        player.onScreenDisplay.setActionBar(
            "§cYou must be at least level 5 to use this skill."
        )
        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null: player.playSound("note.bass")
        return
    }
    if (player.getDynamicProperty(item.typeId + ".ult") < 10) {
            player.onScreenDisplay.setActionBar(
                "§cYour power is not strong enough... Kill hostile mobs to earn Ultimate points!"
            )
            !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("note.bass")
            return
        }
    player.setDynamicProperty(item.typeId + ".ult", 0)
    player.startItemCooldown(item.typeId, type.delay * 20)
    player.onScreenDisplay.setActionBar({
        translate: "actionbar.ld_eb:ld_be_ducks_feet"    
    })
    /** @type { mc.Entity } */
    const locator = lib.locatorEntity(player, { z: 0 }, false, false)
    locator.owner = player
    const size = 7
    lib.spawnBox(locator, size)
    const players = locator.dimension.getEntities({
        type: "minecraft:player",
        location: locator.location,
        maxDistance: size
    })
    players.forEach(p => p.playSound("ld_eb.water_soundtrack", { pitch: 1 }))

    players.forEach(p => lib.startUlt(p, player))

    await mc.system.waitTicks(0.1 * 20)

    player.playAnimation("animation.ld_eb_water_staff.ducks_feet")

    players.forEach(p => lib.cameraToPlayer(p, player, { z: 2, x: 0, y: 1 }, { y: 1.8 }))
    /** @type { mc.Entity } */
    const spell = lib.spell(
        "ld_eb:ld_be_ducks_feet",
        player,
        { z: 0, y: 0 },
        { y: player.getRotation().y },
        true,
        false
    )
    
    await mc.system.waitTicks(1.1 * 20)
    players.forEach(p => lib.cameraToPlayer(p, player, { z: 10, x: 0, y: 6 }, { y: 1, z: 3 }))
    players.forEach(p => lib.shakeBlend(p, { power: 0.3, holdTime: 0.1, outTime: 0.6 }))
    players.forEach(p => p.playSound("ld_eb.ice_armor", { pitch: lib.random(1, 1.2) }))

    await mc.system.waitTicks(1.7 * 20)
    players.forEach(p => p.playSound("ld_eb.tornado", { pitch: lib.random(7, 7.2) }))

    await mc.system.waitTicks(0.2 * 20)
    players.forEach(p => lib.cameraToPlayer(p, player, { z: 8, x: 0, y: 5 }, { y: 3 }))
    await mc.system.waitTicks(1)
    players.forEach(p => lib.cameraToPlayer(p, player, { z: 8, x: 0, y: 8 }, { y: 3 }, 5))

    await mc.system.waitTicks(0.3 * 20)
    players.forEach(p => p.playSound("ambient.underwater.exit", { pitch: 0.5, volume: 20 }))

    await mc.system.waitTicks(1.7 * 20)
    players.forEach(p => lib.cameraToPlayer(p, player, { z: 15, x: -15, y: 6 }, { y: 1.8 }))
    await mc.system.waitTicks(1)
    players.forEach(p => lib.cameraToPlayer(p, player, { z: 10, x: -10, y: 8 }, { y: 0, z: 2 }, 5))

    await mc.system.waitTicks(0.7 * 20)
    players.forEach(p => p.playSound("ld_eb.ice_armor", { pitch: lib.random(0.8, 0.9), volume: 20 }))

    await mc.system.waitTicks(0.8 * 20)
    players.forEach(p => lib.shakeBlend(p, { power: 2, holdTime: 0.1, outTime: 0.6 }))
    players.forEach(p => p.playSound("ld_eb.rockpunch", { pitch: lib.random(2, 2.2), volume: 20 }))
    players.forEach(p => p.playSound("ambient.underwater.exit", { pitch: 1, volume: 20 }))

    await mc.system.waitTicks(0.3 * 20)
    players.forEach(p => lib.fade(p, "white", [0.3, 0.5, 0.8]))

    await mc.system.waitTicks(0.3 * 20)
    lib.despawnBox(locator)
    lib.rayCast(locator, 10, 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))), {
                cause: "contact",
                damagingEntity: player
            })
            lib.middleImpulse(target, spell, -10, 0.004)
            target.addEffect("slowness", 2 * 20, {
                amplifier: 1,
                showParticles: false
            })
        }
    )
    players.forEach(p => lib.finalUlt(p, player))
    spell.remove()
    locator.remove()
    startAnimations(player)
})