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

let damageMultiplier = 7
const delayVelocityMultiplier = 0.5

const entityType = {
    typeId: "ld_eb:ld_eb_magma_warden",
    skills: [
        { delay: delayVelocityMultiplier * 2,damage: damageMultiplier * 1, id: 0, chance: 3, anim: "attack" },
        { delay: delayVelocityMultiplier * 2,damage: damageMultiplier * 1, id: 1, chance: 3, anim: "lava_spout" },
        { delay: delayVelocityMultiplier * 3, damagePS: damageMultiplier * 1, id: 2, chance: 3, anim: "lava_shield" },
        { delay: delayVelocityMultiplier * 3,damage: damageMultiplier * 1, id: 3, chance: 3, anim: "flame_punch" }
    ]
}

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 = Number(id.split("_").pop())
    const skill = skills[skillIndex]
    if (skill) skill.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
            playSound("attack", locator)

            await mc.system.waitTicks(2); if (ifDead(entity)) return
            damageEntities(locator, entity, 1, 0.3, this.index)

            await finishAttack(entity, this.index)
        }, 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.locator(entity, { z: 2 })

            await mc.system.waitTicks(10); if (ifDead(entity)) return
            playSound("1", locator)

            await mc.system.waitTicks(2); if (ifDead(entity)) return
            damageEntities(locator, entity, 1.3, 1, this.index)

            await finishAttack(entity, this.index)
        }, 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: 2 })

            await mc.system.waitTicks(8); if (ifDead(entity)) return
            playSound("2", locator)
            entity.addEffect("resistance", 5 * 20, { showParticles: false, amplifier: 2 })

            await finishAttack(entity, this.index)
        }, 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.locator(entity, { z: 2 })

            await mc.system.waitTicks(8); if (ifDead(entity)) return
            playSound("3", locator)

            await mc.system.waitTicks(2); if (ifDead(entity)) return
            damageEntities(locator, entity, 2, 1, this.index)

            await finishAttack(entity, this.index)
        }, index: 3
    }
]

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

function playSound(name, location) {
    if (!mc.world.getDynamicProperty("ld_eb:efeitos_sonoros")) return
    mc.world.playSound(`ld_eb.magma_warden.${name}`, location.location, { pitch: 1, volume: 20 })
}

function damageEntities(locator, entity, horizontal, vertical, index) {
    lib.getEntities(locator, 10).filter(e => e !== entity).forEach(e => {
        const skill = entityType.skills[index]
        if (skill.damage) e.applyDamage(skill.damage)
        if (horizontal && vertical) {
            try { e.applyKnockback(entity.getViewDirection().x, entity.getViewDirection().z, horizontal, vertical) } catch {}
        }
    })
}

async function finishAttack(entity, index) {
    await mc.system.waitTicks(Math.max(0.1, entityType.skills[index].delay) * 20)
    if (ifDead(entity)) return
    entity.removeEffect("slowness")
    entity.ld_eb_inAttack = false
    entity.triggerEvent("ld_eb:remove_cancel_rotation")
}

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")
    }
})
