import { world, system } from "@minecraft/server";
import {
    namespace,
    TwinWolfAttack1Damage,

    TwinWolfAttack2Damage,
    TwinWolfAttack2KnockbackStrength
} from './config';

system.afterEvents.scriptEventReceive.subscribe((event) => {
    const entity = event.sourceEntity;

    if (event.id === `${namespace}:twinWolfAttack_1`) {
        attack1(entity);
    } else if (event.id === `${namespace}:twinWolfAttack_2`) {
        attack2(entity);
    }
});

function attack1(entity) {
    if (!entity.isValid()) return;
    entity.triggerEvent("bunnystudios_rpg:attack_1");

    system.runTimeout(() => {
        if (!entity.isValid()) return;
        entity.runCommandAsync(`execute as @s at @s positioned ^^^5 run camerashake add @a[r=10] 0.1 1.5 positional`);
    }, 9);

    system.runTimeout(() => {
        if (!entity.isValid()) return;
        const pos = entity.location;
        const viewDirection = entity.getViewDirection();
        const damageCenter = {
            x: pos.x + (viewDirection.x * 1),
            y: pos.y + 1,
            z: pos.z + (viewDirection.z * 1)
        };
        const nearbyEntities = entity.dimension.getEntities({
            location: damageCenter,
            maxDistance: 4,
            excludeTypes: [entity.typeId, "item"],
            excludeFamilies: ["inanimate"]
        });

        for (const target of nearbyEntities) {
            target.applyDamage(TwinWolfAttack1Damage, { cause: 'entityAttack', damagingEntity: entity });
        }
    }, 11);
}

function attack2(entity) {
    if (!entity.isValid()) return;
    entity.triggerEvent("bunnystudios_rpg:attack_2");

    system.runTimeout(() => {
        if (!entity.isValid()) return;
        let hits = 0;

        function hitEnemies() {
            if (!entity.isValid()) return;
            const pos = entity.location;
            const angle = entity.getRotation().y * (Math.PI / 180);

            hitArea(entity, pos, { start: 1, width: 10, length: 8 }, angle);
            hitArea(entity, pos, { start: 9, width: 22, length: 7 }, angle);

            hits++;
            if (hits < 9) {
                system.runTimeout(hitEnemies, 10);
            }
        }

        hitEnemies();
    }, 30);
}

function hitArea(entity, pos, area, angle) {
    if (!entity.isValid()) return;
    const centerX = pos.x - Math.sin(angle) * (area.start + area.length / 2);
    const centerZ = pos.z + Math.cos(angle) * (area.start + area.length / 2);

    const targets = entity.dimension.getEntities({
        location: { x: centerX, y: pos.y, z: centerZ },
        maxDistance: Math.max(area.width, area.length),
        excludeTypes: [entity.typeId, "item"],
        excludeFamilies: ["inanimate"]
    });

    const sin = Math.sin(angle);
    const cos = Math.cos(angle);

    targets.forEach(target => {
        if (target.id === entity.id) return;

        const targetPos = target.location;
        const dx = targetPos.x - pos.x;
        const dz = targetPos.z - pos.z;

        const rotX = dx * cos + dz * sin;
        const rotZ = -dx * sin + dz * cos;

        if (rotZ >= area.start &&
            rotZ <= area.start + area.length &&
            Math.abs(rotX) <= area.width / 2) {

            target.applyDamage(TwinWolfAttack2Damage, { cause: 'entityAttack', damagingEntity: entity });

            const dist = Math.sqrt(dx * dx + dz * dz);
            if (dist > 0) {
                target.applyKnockback(
                    dx / dist,
                    dz / dist,
                    TwinWolfAttack2KnockbackStrength * 3,
                    TwinWolfAttack2KnockbackStrength * 0.5
                );
            }
        }
    });
}