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_chain_warden";
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)  
                entity.runCommand(`loot spawn ~~~ loot "ld_hw/the_chain_warden"`)
                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 hook = entity.dimension.spawnEntity('ld_hw:hook_attack', entity.location)
        hook.dimension.getEntities({
            location: entity.location,
            maxDistance: 4,
            excludeFamilies: excludeFamilies,
            excludeTypes: excludeTypes,
            excludeTypes: [entity.typeId]
        }).forEach(target=>{
            mc.system.runTimeout(()=>{
                if (!target.isValid()) return
                target.addTag('ld_hw:chain_hooked')
                target.applyDamage(2*damageMultiplier,{damagingEntity:entity,cause:'contact'})
            },0.92*20)
        })
        mc.system.runTimeout(()=>{
            hook.teleport(entity.location, {rotation: {x: 0, y: entity.getRotation().y}})
            hook.teleport({x:hook.location.x+hook.getViewDirection().x*1, y:hook.location.y, z:hook.location.z+hook.getViewDirection().z*1})
            mc.world.getDimension('overworld').getEntities({tags:['ld_hw:chain_hooked'], location:hook.location, maxDistance: 20}).forEach(target=>{target.applyDamage(2*damageMultiplier,{cause:'contact',damagingEntity:entity});target.teleport(hook.location);target.addEffect('slowness', 3, {amplifier:3})})
            //DEBUG 1 console.warn(`your damage is ${damage}`)
            mc.system.runTimeout(()=>{
                hook.runCommand(`tag @e[r=5] remove ld_hw:chain_hooked`)
            }, 1* 20)
        },1.21*20)
        entity.setProperty("ld_hw:delay", true)
        actionManager.createTimeout(entity, () => {

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


        }, 20 * 3)


    },
    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)
        mc.system.runTimeout(()=>{
            entity.dimension.getEntities({
                location: entity.location,
                maxDistance: 8,
                excludeFamilies: excludeFamilies,
                excludeTypes: excludeTypes,
                excludeTypes: [entity.typeId]
            }).forEach(target=>{
                mc.system.runTimeout(()=>{
                    if (!target.isValid()) return
                    try {
                    target.applyDamage(1*damageMultiplier,{damagingEntity:entity,cause:'contact'})
                    target.applyKnockback(-(entity.location.x - target.location.x), -(entity.location.z - target.location.z),0.5,0.2)
                    } catch (error) {
                        
                    }
                })
            })
        },0.5*20)
        mc.system.runTimeout(()=>{
            entity.dimension.getEntities({
                location: entity.location,
                maxDistance: 8,
                excludeFamilies: excludeFamilies,
                excludeTypes: excludeTypes,
                excludeTypes: [entity.typeId]
            }).forEach(target=>{
                mc.system.runTimeout(()=>{
                    if (!target.isValid()) return
                    try {
                    target.applyDamage(2*damageMultiplier,{damagingEntity:entity,cause:'contact'})
                    target.applyKnockback(-(entity.location.x - target.location.x), -(entity.location.z - target.location.z),1,0.4)
                    } catch (error) {
                        
                    }
                })
            })
        },0.88*20)
        mc.system.runTimeout(()=>{
            entity.dimension.getEntities({
                location: entity.location,
                maxDistance: 8,
                excludeFamilies: excludeFamilies,
                excludeTypes: excludeTypes,
                excludeTypes: [entity.typeId]
            }).forEach(target=>{
                mc.system.runTimeout(()=>{
                    if (!target.isValid()) return
                    try {
                    target.applyDamage(1*damageMultiplier,{damagingEntity:entity,cause:'contact'})
                    target.applyKnockback(-(entity.location.x - target.location.x), -(entity.location.z - target.location.z),3,0.7)
                    } catch (error) {
                        
                    }
                })
            })
        },1.25*20)
        entity.setProperty("ld_hw:delay", true)
        actionManager.createTimeout(entity, () => {
            entity.triggerEvent("ld_hw:remove_attack")
        }, 20 * 2.0)
    },
    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]
                })
                if(targets.length > 0) {targets.forEach(target => {
                    target.addEffect('weakness', 20*20, {amplifier:2})
                    target.addEffect('darkness', 20*20)
                })}
            })
            entity.setProperty("ld_hw:delay", true)
            actionManager.createTimeout(entity, () => {

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


            }, 20 * 1)


        }, 20 * 0.75)


    }

]
