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_glacier_colossus",
    skills: [
        {
            delay: delayVelocityMultiplier * 2,
           damage: damageMultiplier * 1,
            id: 0,
            chance: 3,
            anim: "attack"
        },
        {
            delay: delayVelocityMultiplier * 2,
           damage: damageMultiplier * 1,
            id: 1,
            chance: 3,
            anim: "frozen_punch"
        },
        {
            delay: delayVelocityMultiplier * 1,
           damage: damageMultiplier * 1,
            id: 2,
            chance: 3,
            anim: "icequake"
        },
        {
            delay: delayVelocityMultiplier * 3,
           damage: damageMultiplier * 1,
            id: 3,
            chance: 3,
            anim: "shield_of_frost"
        }
    ]
}
mc.Entity.prototype.ld_eb_inAttack = false
mc.system.afterEvents.scriptEventReceive.subscribe(async data => {
    const entity = data.sourceEntity,
        id = data.id
    if (entity.typeId !== entityType.typeId) return
    entity.triggerEvent("ld_eb:cancel_rotation")
    if (entity.ld_eb_inAttack) return
    switch (id) {
        case "ld_eb:skill_0": skills[0].start(entity); break
        case "ld_eb:skill_1": skills[1].start(entity); break
        case "ld_eb:skill_2": skills[2].start(entity); break
        case "ld_eb:skill_3": skills[3].start(entity); break
    }
    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.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : mc.world.playSound("ld_eb.glacier_colossus.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.locator(entity, { z: 2 })
        await mc.system.waitTicks(8)
        if (ifDead(entity)) return;
        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : mc.world.playSound("ld_eb.glacier_colossus.1", 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, 3, 0.6) } 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: 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.locator(entity, { z: 0 })
        await mc.system.waitTicks(8)
        if (ifDead(entity)) return;
        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : mc.world.playSound("ld_eb.glacier_colossus.2", locator.location, { pitch: 1, volume: 20 })
        await mc.system.waitTicks(2)
        if (ifDead(entity)) return;
        lib.getEntities(locator, 10).filter(e => e !== entity).forEach(async e => {
            const spike = e.dimension.spawnEntity("ld_eb:ld_eb_glacier_colossus_spike", e.location)
            await mc.system.waitTicks(2)
            if (ifDead(entity)) return;
            e.applyDamage(entityType.skills[this.index].damage*difficultyDamageController)
            lib.middleImpulse(e, entity, -3, 1)
            await mc.system.waitTicks(20)
            spike.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.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : mc.world.playSound("ld_eb.glacier_colossus.3", locator.location, { pitch: 1, volume: 20 })
        entity.addEffect("resistance", 5 * 20, { 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
}]

mc.world.afterEvents.entityLoad.subscribe(data => {
    const entity = data.entity
    if (entity.typeId !== entityType.typeId) return
    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
}
