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_eb_air_clone"    
    })
    /** @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
    })
    const targetwoy = player.dimension.getEntities({maxDistance:10,location:player.location,excludeTypes:['minecraft:item','minecraft:xp_orb']})
    players.forEach(p => p.playSound("ld_eb.air_soundtrack", { pitch: 1 }))

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

    await mc.system.waitTicks(0.3 * 20)

    player.playAnimation("animation.ld_eb_air_staff.ult")

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

    await mc.system.waitTicks(1.9 * 20)
    players.forEach(p => lib.cameraToPlayer(p, player, { z: 4, x: 0, y: 5 }, { y: 4 }))
    players.forEach(p => lib.cameraToPlayer(p, player, { z: 6, x: 0, y: 7 }, { y: 6 }, 5))
    players.forEach(p => p.playSound("ld_eb.tornado", { pitch: lib.random(3, 3.1), volume: 20 }))

    await mc.system.waitTicks(1.5 * 20)
    players.forEach(p => lib.cameraToPlayer(p, player, { z: 6, x: -6, y: 6 }, { y: 3, z: 2 }))
    players.forEach(p => lib.cameraToPlayer(p, player, { z: 5, x: -5, y: 6 }, { y: 3, z: 3 }, 5))

    await mc.system.waitTicks(2.3 * 20)
    players.forEach(p => lib.shakeBlend(p, { power: 2, holdTime: 0.2, outTime: 0.8 }))
    players.forEach(p => p.playSound("ld_eb.airblow", { pitch: lib.random(0.3, 0.4), volume: 20 }))
    players.forEach(p => p.playSound("ld_eb.tornado", { pitch: lib.random(8, 8.1), volume: 20 }))
    players.forEach(p => p.playSound("ld_eb.rockpunch", { pitch: lib.random(4, 4.2), volume: 20 }))
    lib.despawnBox(locator)
    let entities = []
    lib.rayCast(locator, 10, 1,
        /** @param  { mc.Entity } target */
        function (target) {
            entities.push(target)
            lib.middleImpulse(target, spell, -10, 0.4)
            target.addEffect("slowness", 2 * 20, {
                amplifier: 1,
                showParticles: false
            })
        }
    )

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

    await mc.system.waitTicks(0.8 * 20)
    player.removeEffect("slowness")
    players.forEach(p => lib.finalUlt(p, player))
    targetwoy.forEach(target=>{
        if(target==player) return
        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
        })
        try { target.applyKnockback(0, 0, 0, 8) } catch {}
        target.addEffect("slowness", 2 * 20, {
            amplifier: 1,
            showParticles: false
        })
    })
    spell.remove()
    locator.remove()
    startAnimations(player)
})