import { system, world } from "@minecraft/server"

// Erius Phase 1

world.afterEvents.entityHitEntity.subscribe(mob => {
    // Grab Entities
    const target = mob.hitEntity
    const attacker = mob.damagingEntity

    const ts0 = target.runCommand('/testfor @s[hasitem={item=minecraft:shield, location=slot.weapon.offhand}]').successCount
    const ts1 = target.runCommand('/testfor @s[hasitem={item=minecraft:shield, location=slot.weapon.mainhand}]').successCount

    if (attacker.typeId === 'darkw_fallenking:erius_p1') {
        const erian = mob.damagingEntity
        const attackMode = erian.getProperty('darkw_fallenking:attack_mode')


        switch (attackMode) {
            // Normal Attack
            case 1: {
                if ((ts0 === 0 || ts1 === 0) && !target.isSneaking) {
                    target.addEffect('wither', 40, { amplifier: 2 })
                }
                break
            }
            // Caveman Attack
            case 2: {
                if ((ts0 === 0 || ts1 === 0) && !target.isSneaking) {
                    target.setOnFire(2)
                }
                break
            }
            // Slam Attack
            case 3: {
                const ErianViewVec = erian.getViewDirection();
                target.applyKnockback(ErianViewVec.x, ErianViewVec.z, 3.5, 0.45)
                break
            }
            // Spin Attack
            case 4: {
                const ErianViewVec = erian.getViewDirection();
                target.applyKnockback(ErianViewVec.x, ErianViewVec.z, 2.75, 0.45)
                if ((ts0 === 0 || ts1 === 0) && !target.isSneaking) {
                    target.addEffect('wither', 2, { amplifier: 2 })
                }
                break
            }
        }
    }
})






// Erius Phase 2

world.afterEvents.entityHitEntity.subscribe(mob => {
    // Grab Entities
    const target = mob.hitEntity
    const attacker = mob.damagingEntity

    const ts0 = target.runCommand('/testfor @s[hasitem={item=minecraft:shield, location=slot.weapon.offhand}]').successCount
    const ts1 = target.runCommand('/testfor @s[hasitem={item=minecraft:shield, location=slot.weapon.mainhand}]').successCount

    if (attacker.typeId === 'darkw_fallenking:erius_p2') {
        const erian = mob.damagingEntity
        const attackMode = erian.getProperty('darkw_fallenking:attack_mode')
        switch (attackMode) {
            // Normal Attack
            case 1: {
                const ErianViewVec = erian.getViewDirection();
                target.applyKnockback(ErianViewVec.x, ErianViewVec.z, 2.75, 0.45)
                break
            }
            // Caveman Attack
            case 2: {
                const ErianViewVec = erian.getViewDirection();
                target.applyKnockback(ErianViewVec.x, ErianViewVec.z, 1.75, 0.65)
                break
            }
            // Slam Attack
            case 3: {
                const ErianViewVec = erian.getViewDirection();
                target.applyKnockback(ErianViewVec.x, ErianViewVec.z, 1.75, 0.95)
                break
            }
            // Spin Attack
            case 4: {
                const ErianViewVec = erian.getViewDirection();
                target.applyKnockback(ErianViewVec.x, ErianViewVec.z, 1.75, 0.65)
                if ((ts0 === 0 || ts1 === 0) && !target.isSneaking) {
                    target.runCommandAsync('effect @s nausea 2 5')
                }
            }
        }
    }
})

// OG Erius

world.afterEvents.entityHitEntity.subscribe(mob => {
    // Grab Entities
    const target = mob.hitEntity
    const attacker = mob.damagingEntity

    if (attacker.typeId === 'darkw_fallenking:erius_og') {
        const erian = mob.damagingEntity
        const victim = mob.hitEntity

        if ((ts0 === 0 || ts1 === 0) && !target.isSneaking) {
            victim.addEffect('wither', 100, { amplifier: 3 })
        }

        const ErianViewVec = erian.getViewDirection();
        target.applyKnockback(ErianViewVec.x, ErianViewVec.z, 0.95, 0.45)
    }
})

system.afterEvents.scriptEventReceive.subscribe(event => {
    const id = event.id
    const entity = event.sourceEntity

    if (id === 'darkw_fallenking:deathprep') {
        entity.clearVelocity()
    }
})