import { system, world, ItemStack, EquipmentSlot, EntityComponentTypes, ItemDurabilityComponent } from "@minecraft/server"

import {
    DeadKingScytheLore0,
    DeadKingScytheLore1,
    DeadKingScytheLore2,
    DeadKingScytheLore3_1,
    DeadKingScytheLore3_2
} from './lore.js'

world.beforeEvents.worldInitialize.subscribe(event => {
    event.itemComponentRegistry.registerCustomComponent('darkw_fallenking:dead_scythe_switch', {
        onUse: e => {
            const player = e.source
            const playerHold = player.getComponent('equippable')
            const playerMainhand = playerHold.getEquipment('Mainhand')
            const item = e.itemStack
            const itemLore = item.getLore()[3]
            const PlayerViewVec = player.getViewDirection();

            player.runCommandAsync('/playanimation @s animation.erius_p2.attack4 default 0.2')
            player.addEffect('slowness', 25, {amplifier: 3, showParticles: false})

            system.runTimeout(() => {
                const ranPitch = Math.random() + 0.5
                player.playSound('fallenking.item.scythe.slice', {pitch: ranPitch, volume: 0.5})

                const x = player.location.x
                const y = player.location.y
                const z = player.location.z

                const aura = player.dimension.getEntities({maxDistance: 3, location: {x, y, z}})
                if (!aura) return
                
                for (let i = 1; i < aura.length; i++) {
                    const entryEntity = aura[i]
                    entryEntity.applyDamage(6, {cause: 'entity_attack', damagingEntity: player, damagingProjectile: player})
                    entryEntity.addEffect('wither', 60, { amplifier: 2 })
                }
            }, 11)
        },
        onHitEntity: e => {
            const target = e.hitEntity
            const player = e.attackingEntity
            const PlayerViewVec = player.getViewDirection();
            const item = e.itemStack
            const itemLore = item.getLore()[3]

            target.addEffect('wither', 60, { amplifier: 2 })
        }
    })
})