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:decimal_skeleton";
let damageMultiplier = 3


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)  
                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!=3){
        damageMultiplier = 3
    } else if(mc.world.getDynamicProperty('ld_hw.:difficulty')==2 && damageMultiplier!=4){
        damageMultiplier = 4
    }


}, 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[0](entity)
    entity.setDynamicProperty("ld_hw:after_random_skill", randomSkill)
}
const skills = [
    function(entity) {

        
        entity.setProperty("ld_hw:anim", 1)
        actionManager.createTimeout(entity, () => { 
            try {
                entity.setProperty("ld_hw:anim", 0)
            } catch (error) {
                
            }
        }, 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]
        })
        actionManager.createTimeout(entity, () => {
            try {
                targets.forEach(target => {
                    if (!target.isValid()) return
                    if (target.typeId==='minecraft:player'){target.runCommand('camerashake add @s 0.10 3 positional')}
                })
                try {
                    if(mc.world.getDynamicProperty('ld_hw.:block_damage')){entity.triggerEvent('ld_hw:explode')}
                    else {entity.triggerEvent('ld_hw:explode_no_breaks')}
                } catch (error) {
                    
                }
                entity.setProperty("ld_hw:delay", true)
                actionManager.createTimeout(entity, () => {
    
                    try {
                        entity.triggerEvent("ld_hw:remove_attack")
                        actionManager.stopAllActions(entity)  
                    } catch (error) {
                        
                    }
    
    
                }, 20 * 2)
            } catch (error) {
                
            }


        }, 20 * 0.5)


    }
]