import * as mc from "@minecraft/server"
import * as lib from "../lib"
import { excludeFamilies, excludeTypes, runEntity } from "./utils"
import { ActionManager } from "../lib/actionManager"


const actionManager = new ActionManager();


const type = "ld_hw:the_wraith_keeper";
let damageMultiplier = 2


const tick = mc.system.runInterval(() => {

    runEntity({ typeId: type }, 
        
        (entity) => {

            
            if (entity.getProperty("ld_hw:dying") === true) { 

                actionManager.stopAllActions(entity)  
                return; 


            }

            
            
            const health = entity.getComponent("minecraft:health")

            
            if (health.currentValue === 1 && entity.getProperty("ld_hw:delay") === false) {

                entity.triggerEvent("ld_hw:die")
                entity.setProperty("ld_hw:dying", true)  
                entity.setProperty("ld_hw:delay", true)  
                actionManager.stopAllActions(entity)  
                entity.runCommand(`loot spawn ~~~ loot "ld_hw/the_wraith_keeper"`)
                if(mc.world.getDynamicProperty(`ld_hw.:sound`)==true) {
                    entity.runCommand(`stopsound @a[r=20] ld_hw:fight_song`)
                    entity.runCommand(`playsound ld_hw:effect.hit @a[r=20]`)
                }
                return;

            }

            
            if (entity.getProperty("ld_hw:near_target") === false || entity.getProperty("ld_hw:delay") === true) { return; }

            
            skillIds(entity)


        }
    )
    if(mc.world.getDynamicProperty('ld_hw.:difficulty')==0 && damageMultiplier!=2){
        damageMultiplier = 2
    } else if(mc.world.getDynamicProperty('ld_hw.:difficulty')==1 && damageMultiplier!=2.5){
        damageMultiplier = 2.5
    } else if(mc.world.getDynamicProperty('ld_hw.:difficulty')==2 && damageMultiplier!=3){
        damageMultiplier = 3
    }


}, 20 * 0.1)


function skillIds(entity) {
    let randomSkill = lib.calculate.random(0, 2)
    if (randomSkill === entity.getDynamicProperty("ld_hw:after_random_skill")) { return skillIds(entity) }
    skills[randomSkill](entity)
    entity.setDynamicProperty("ld_hw:after_random_skill", randomSkill)
}
const skills = [

    
    function(entity) {

        
        entity.setProperty("ld_hw:anim", 1)
        actionManager.createTimeout(entity, () => { entity.setProperty("ld_hw:anim", 0) }, 1)

        
        const movement = entity.getComponent("minecraft:movement")
        movement.setCurrentValue(0)

        const targets = entity.dimension.getEntities({
            location: entity.location,
            maxDistance: 8,
            excludeFamilies: excludeFamilies,
            excludeTypes: excludeTypes,
            excludeTypes: [entity.typeId]
        })
        targets.forEach(target => {
            if (target.typeId==='minecraft:player'){target.runCommand('camerashake add @s 0.10 4 positional')}
        })
        actionManager.createTimeout(entity, () => {

            
            targets.forEach(target => {
                if (!target.isValid()) return;
                //target.applyDamage(0.1 * damageMultiplier, { cause: "contact", damagingEntity: entity })
                target.addEffect('slowness', 6*20, {amplifier: 1})
                //if (target.typeId==='minecraft:player'){target.runCommand('camerashake add @s 0.10 3 positional')}
            })
            
            entity.setProperty("ld_hw:delay", true)
            actionManager.createTimeout(entity, () => {

                entity.triggerEvent("ld_hw:remove_attack")
                actionManager.stopAllActions(entity)  


            }, 20 * 2)


        }, 20 * 1)


    },

    
    function(entity) {

        
        entity.setProperty("ld_hw:anim", 2)
        actionManager.createTimeout(entity, () => { entity.setProperty("ld_hw:anim", 0) }, 1)

        
        const movement = entity.getComponent("minecraft:movement")
        movement.setCurrentValue(0)

        
        actionManager.createTimeout(entity, () => {

            const targets = entity.dimension.getEntities({
                location: entity.location,
                maxDistance: 8,
                excludeFamilies: excludeFamilies,
                excludeTypes: excludeTypes,
                excludeTypes: [entity.typeId]
            })

            
            targets.forEach(target => {
                try {
                    target.applyDamage(damageMultiplier*1,{cause:'contact',damagingEntity:entity})
                    if(mc.world.getDynamicProperty('ld_hw.:difficulty')>0){
                    const health = entity.getComponent('minecraft:health')
                    health.setCurrentValue(health.currentValue+Math.round(damageMultiplier/2))
                    }
                    try {
                    target.applyKnockback(entity.getViewDirection().x, entity.getViewDirection().z, 1, 0.6)
                    } catch (error) {
                        
                    }
                } catch (error) {
                    
                }
            })
            
            entity.setProperty("ld_hw:delay", true)
            actionManager.createTimeout(entity, () => {

                entity.triggerEvent("ld_hw:remove_attack")


            }, 20 * 2)


        }, 20 * 0.75)


    },

    
    function(entity) { 
        
        
        entity.setProperty("ld_hw:anim", 3)
        actionManager.createTimeout(entity, () => { entity.setProperty("ld_hw:anim", 0) }, 1)

        
        const movement = entity.getComponent("minecraft:movement")
        movement.setCurrentValue(0)

        
        actionManager.createTimeout(entity, () => {
            actionManager.createTimeout(entity, () => {
                const targets = entity.dimension.getEntities({
                    location: entity.location,
                    maxDistance: 8,
                    excludeFamilies: excludeFamilies,
                    excludeTypes: excludeTypes,
                    excludeTypes: [entity.typeId],
                    closest: 1
                })
                targets.forEach(target => {
                    entity.teleport({
                        x: target.location.x + target.getViewDirection().x * -2,
                        y: target.location.y,
                        z: target.location.z + target.getViewDirection().z * -2,
                    })
                    actionManager.createTimeout(entity, () => {
                        try {
                            target.applyDamage(2 * damageMultiplier, { cause: "contact", damagingEntity: entity })
                            target.applyKnockback(entity.getViewDirection().x, entity.getViewDirection().z, 1, 0.6)
                        } catch (error) {
                            
                        }
                    }, 0.75 * 20)
                })
            })
            entity.setProperty("ld_hw:delay", true)
            actionManager.createTimeout(entity, () => {

                entity.triggerEvent("ld_hw:remove_attack")
                actionManager.stopAllActions(entity)  


            }, 20 * 1)


        }, 20 * 0.63)


    }

]
