import * as mc from "@minecraft/server";
import * as selectors from "../utils/selectors";
import * as umvuthana from "./umvuthana";

export function eventBehavior(event) {
    umvuthana.eventBehavior(event);
    if (event.eventId === 'hit') {
        event.entity.runCommand("/playsound random.anvil_land @a[r=16] ~~~ 1 2");
        return;
    }
    if (event.eventId === 'counter') {
        //mc.world.sendMessage("counter");
        mc.system.runTimeout( ()=> {
            //mc.world.sendMessage("dash");
            const loc = event.entity.location;
            event.entity.applyImpulse(event.entity.getViewDirection())
            mc.system.runTimeout( ()=> {
                const entities = event.entity.dimension.getEntities({
                    location: loc,
                    maxDistance: 8,
                    families: ['player']
                });
                if (entities[0] === undefined)
                    return;
                entities[0].applyDamage(7, {
                    cause: "entityAttack",
                    damagingEntity: event.entity
                });
                entities[0].applyKnockback(loc.x, loc.z, 2.5, 0.1);
                //mc.world.sendMessage("slice");
            }, 4);
        }, 7);
        return;
    }
}
export function tick5(entity){
    if (entity.getProperty('hf_mzs:block_counter') > 0)
        entity.setProperty('hf_mzs:block_counter', entity.getProperty('hf_mzs:block_counter')-1);

    entity.runCommand("/execute as @s at @s rotated ~ 0 positioned ^^^5 if entity @p[r=5] run event entity @s player_infront");
    entity.runCommand("/execute as @s at @s rotated ~ 0 positioned ^^^5 unless entity @p[r=5] run event entity @s no_player_infront");
    //mc.world.sendMessage(""+entity.getProperty('hf_mzs:block_counter'));
    /*
    const arc = selectors.getEntitiesArc(entity, ["player"], 8, 180, true);
    if (arc[0] === undefined) {
        //mc.world.sendMessage("not behind");
        entity.setProperty('hf_mzs:front', true)
    }
    else {
        //mc.world.sendMessage("behind");
        entity.setProperty('hf_mzs:front', false)
    }
    */
}