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

world.afterEvents.entityHurt.subscribe(event => {
    const a = event.damageSource.damagingEntity
    const b = event.hurtEntity

    if (!a) return
    if (!b) return

    // Deathblade
    if (a.typeId === 'darkw_dotv:deathblade') {
        const deathblade = event.damageSource.damagingEntity
        const player = event.hurtEntity
        const ShieldTest = player.runCommand('/testfor @s[hasitem={item=minecraft:shield, location=slot.weapon.offhand}]').successCount

        // Get current attack state
        if (!deathblade) return
        const attackstate = deathblade.getComponent('variant').value
        if (!attackstate) return
        if (attackstate) {

            switch (attackstate) {


                case 0:
                    break


                // Smack
                case 1:
                    if (ShieldTest === 0 && !player.isSneaking) {
                        player.addEffect('wither', 20, { amplifier: 1 })
                    }
                    break


                // Grab
                case 2:
                    if (ShieldTest === 0 && !player.isSneaking) {
                        player.addEffect('wither', 20, { amplifier: 1 })
                    }
                    break


                // Slam
                case 3:
                    const DeathbladeViewVec = deathblade.getViewDirection();
                    player.applyKnockback({ x: deathblade.getViewDirection().x * 1.85, z: deathblade.getViewDirection().z * 1.85 }, 0.45)
                    deathblade.runCommand('particle minecraft:knockback_roar_particle ~ ~ ~')
                    break


                // Horns
                case 4:
                    player.applyKnockback({ x: 0, z: 0 }, 0.65)
                    if (ShieldTest === 0 && !player.isSneaking) {
                        player.setOnFire(5)
                    }
                    break
            }
        }
        else return
    }
    // Brawlatar
    if (a.typeId === 'darkw_dotv:brawlatar') {
        const brawlatar = event.damageSource.damagingEntity
        const player = event.hurtEntity
        const BrawlatarViewVec = brawlatar.getViewDirection();

        // Get current attack state
        const attackstate = brawlatar.getComponent('variant').value
        if (!attackstate) return
        if (attackstate) {
            switch (attackstate) {

                case 0:
                    break


                // Smack
                case 1:
                    player.applyKnockback({ x: brawlatar.getViewDirection().x * 1.85, z: brawlatar.getViewDirection().z * 1.85 }, 0.3)
                    break


                // Grab
                case 2:
                    player.applyKnockback({ x: brawlatar.getViewDirection().x * 1.85, z: brawlatar.getViewDirection().z * 1.85 }, 0.3)
                    break


                // Slam
                case 3:
                    player.applyKnockback({ x: brawlatar.getViewDirection().x * 1.85, z: brawlatar.getViewDirection().z * 1.85 }, 0.3)
                    break


                // Puke
                case 4:

                    player.addEffect('nausea', 200, { amplifier: 2 })
                    player.addEffect('wither', 200, { amplifier: 1 })
                    player.applyKnockback({ x: brawlatar.getViewDirection().x * 1.85, z: brawlatar.getViewDirection().z * 1.85 }, 0.3)
                    break
            }
        }
        else return


    }
    // Erian
    if (a.typeId === 'darkw_dotv:erian') {
        const erian = event.damageSource.damagingEntity
        const player = event.hurtEntity

        if (erian.getProperty('darkw_dotv:is_enraged')) {
            player.addEffect('wither', 200, { amplifier: 1 })
        }
    }
})