import * as mc from "@minecraft/server"
import * as lib from "../lib"
import { difficultyDamageController } from './index'

let damageMultiplier = 7
const delayVelocityMultiplier = 0.5
let entityType = {
    typeId: "ld_eb:ld_eb_steel_behemoth",
    skills: [
        { delay: delayVelocityMultiplier * 1,damage: damageMultiplier * 1, id: 0, chance: 3, anim: "attack" },
        { delay: delayVelocityMultiplier * 2, damagePS: damageMultiplier * 1, id: 1, chance: 3, anim: "crushing_flail" },
        { delay: delayVelocityMultiplier * 0.1,damage: damageMultiplier * 1, id: 2, chance: 3, anim: "magnetize" },
        { delay: delayVelocityMultiplier * 5, damagePS: damageMultiplier * 1, id: 3, chance: 3, anim: "iron_skin" }
    ]
}

mc.Entity.prototype.ld_eb_inAttack = false

mc.system.afterEvents.scriptEventReceive.subscribe(async data => {
    const entity = data.sourceEntity
    const id = data.id
    if (entity.typeId !== entityType.typeId || entity.ld_eb_inAttack) return

    entity.triggerEvent("ld_eb:cancel_rotation")

    const skillIndex = parseInt(id.replace("ld_eb:skill_", ""))
    if (!isNaN(skillIndex)) skills[skillIndex].start(entity)

    entity.setProperty("ld_eb:proximo_ataque", lib.randomWithChances(entityType.skills))
    entity.triggerEvent("ld_eb:skill_" + entity.getProperty("ld_eb:proximo_ataque") + "_intention")
})

const skills = [
    {
        start: async function (entity) {
            if (ifDead(entity)) return
            entity.ld_eb_inAttack = true
            entity.addEffect("slowness", 9999999, { showParticles: false, amplifier: 255 })
            entity.playAnimation(entityType.skills[this.index].anim)
            const locator = lib.locator(entity, { z: 2 })

            await mc.system.waitTicks(8)
            if (ifDead(entity)) return
            mc.world.playSound("ld_eb.steel_behemont.attack", locator.location, { pitch: 1, volume: 20 })
            await mc.system.waitTicks(2)
            if (ifDead(entity)) return

            lib.getEntities(locator, 10).filter(e => e !== entity).forEach(e => {
                e.applyDamage(entityType.skills[this.index].damage*difficultyDamageController)
                try { e.applyKnockback(entity.getViewDirection().x, entity.getViewDirection().z, 2, 0.2) } catch {}
            })

            await mc.system.waitTicks(entityType.skills[this.index].delay * 20)
            if (ifDead(entity)) return
            entity.removeEffect("slowness")
            entity.ld_eb_inAttack = false
            entity.triggerEvent("ld_eb:remove_cancel_rotation")
        },
        index: 0
    },
    {
        start: async function (entity) {
            if (ifDead(entity)) return
            entity.ld_eb_inAttack = true
            entity.addEffect("slowness", 9999999, { showParticles: false, amplifier: 255 })
            entity.playAnimation(entityType.skills[this.index].anim)
            const locator = lib.locatorEntity(entity, { z: 2 })

            await mc.system.waitTicks(20)
            if (ifDead(entity)) return
            mc.world.playSound("ld_eb.steel_behemont.1", locator.location, { pitch: 1, volume: 20 })
            await mc.system.waitTicks(2)
            if (ifDead(entity)) return

            lib.getEntities(locator, 5).filter(e => e !== entity).forEach(e => {
                e.applyDamage(entityType.skills[this.index].damagePS*difficultyDamageController)
                try { e.applyKnockback(entity.getViewDirection().x, entity.getViewDirection().z, 1, 0.3) } catch {}
            })

            await mc.system.waitTicks(30)
            if (ifDead(entity)) return
            locator.remove()

            await mc.system.waitTicks(entityType.skills[this.index].delay * 20)
            if (ifDead(entity)) return
            entity.removeEffect("slowness")
            entity.ld_eb_inAttack = false
            entity.triggerEvent("ld_eb:remove_cancel_rotation")
        },
        index: 1
    },
    {
        start: async function (entity) {
            if (ifDead(entity)) return
            entity.ld_eb_inAttack = true
            entity.addEffect("slowness", 9999999, { showParticles: false, amplifier: 255 })
            entity.playAnimation(entityType.skills[this.index].anim)
            const locator = lib.locatorEntity(entity, { z: 5 })

            await mc.system.waitTicks(10)
            if (ifDead(entity)) return
            mc.world.playSound("ld_eb.steel_behemont.1", locator.location, { pitch: 1, volume: 20 })

            lib.getEntities(locator, 10).filter(e => e !== entity).forEach(e => {
                e.applyDamage(entityType.skills[this.index].damage*difficultyDamageController)
                try { e.applyKnockback(entity.getViewDirection().x, entity.getViewDirection().z, 2, 0.2) } catch {}
            })

            await mc.system.waitTicks(10)
            if (ifDead(entity)) return
            mc.world.playSound("ld_eb.steel_behemont.2", locator.location, { pitch: 1, volume: 20 })

            lib.getEntities(locator, 12).filter(e => e !== entity).forEach(e => {
                e.addEffect("slowness", 40, { showParticles: false, amplifier: 1 })
                lib.middleImpulse(e, entity, lib.getDistance(e.location, entity.location) / 2, 0.2)
            })

            await mc.system.waitTicks(30)
            if (ifDead(entity)) return
            locator.remove()

            await mc.system.waitTicks(entityType.skills[this.index].delay * 20)
            if (ifDead(entity)) return
            entity.removeEffect("slowness")
            entity.ld_eb_inAttack = false
            entity.triggerEvent("ld_eb:remove_cancel_rotation")
        },
        index: 2
    },
    {
        start: async function (entity) {
            if (ifDead(entity)) return
            entity.ld_eb_inAttack = true
            entity.addEffect("slowness", 9999999, { showParticles: false, amplifier: 255 })
            entity.playAnimation(entityType.skills[this.index].anim)
            const locator = lib.locatorEntity(entity, { z: 2 })

            await mc.system.waitTicks(8)
            if (ifDead(entity)) return
            mc.world.playSound("ld_eb.steel_behemont.3", locator.location, { pitch: 1, volume: 20 })
            entity.addEffect("resistance", 100, { showParticles: false, amplifier: 2 })

            await mc.system.waitTicks(entityType.skills[this.index].delay * 20)
            if (ifDead(entity)) return
            entity.removeEffect("slowness")
            entity.ld_eb_inAttack = false
            entity.triggerEvent("ld_eb:remove_cancel_rotation")
        },
        index: 3
    }
]

skills.forEach((skill, index) => skill.index = index)

mc.world.afterEvents.entityLoad.subscribe(data => {
    const entity = data.entity
    if (entity.typeId === entityType.typeId) {
        entity.removeEffect("slowness")
        entity.triggerEvent("ld_eb:remove_cancel_rotation")
    }
})

function ifDead(entity) {
    const health = entity.getComponent(mc.EntityHealthComponent.componentId)
    return !health || health.currentValue <= 1
}
