import { EntityDamageCause, Player, system, world } from "@minecraft/server";
import { Entities } from "./Entities";
import { movePlayerAway } from "../utils/utils";

const Attacks = [
    {
        time: 0.5,
        damage: 3,
    },
    {
        time: 0.75,
        damage: 3,
    },
    {
        time: 1.0,
        damage: 3,
    },
    {
        time: 1.7,
        damage: 3,
    },
];

const difficultyDamage = new Map([
    [
        "easy",
        {
            damage: 4,
        },
    ],
    [
        "normal",
        {
            damage: 6,
        },
    ],
    [
        "hard",
        {
            damage: 8,
        },
    ],
]);

world.afterEvents.dataDrivenEntityTrigger.subscribe((event) => {
    const { entity, eventId } = event;

    if (entity.typeId == Entities.strongman) {
        switch (eventId) {
            case "clown:attack_sequence": {
                sequenceAttack(entity);
                break;
            }
            case "clown:knockup_attack": {
                knockupAttack(entity);
                break;
            }
            case "clown:move_player_away": {
                movePlayerAway(entity);
                break;
            }
        }
    }
});

/**
 *
 * @param {Entity} entity
 */
function sequenceAttack(entity) {
    const viewDirection = entity.getViewDirection();

    const dimension = entity.dimension;

    const attackRange = 3;

    const difficulty = entity.getProperty("clown:difficulty");

    const damage = difficultyDamage.get(difficulty).damage ?? 0;

    for (let attack = 0; attack < Attacks.length; attack++) {
        const { time } = Attacks[attack];

        if (entity) {
            system.runTimeout(() => {
                try {
                    const nearbyEntities =
                        dimension.getEntities({
                            location: {
                                x: entity?.location.x,
                                y: entity?.location.y,
                                z: entity?.location.z,
                            },
                            maxDistance: attackRange,
                        }) ?? [];

                    for (let mob of nearbyEntities) {
                        if (mob != entity) {
                            if (mob instanceof Player) {
                                if (entity.matches({ excludeFamilies: ["passive"] })) {
                                    mob?.applyDamage(damage, {
                                        cause: EntityDamageCause.entityAttack,
                                        damagingEntity: entity,
                                    });
                                }
                            } else {
                                mob?.applyDamage(damage, {
                                    cause: EntityDamageCause.entityAttack,
                                    damagingEntity: entity,
                                });
                            }
                        }
                    }
                } catch (e) {}
            }, time * 20);
        }
    }
}

/**
 *
 * @param {Entity} entity
 */
function knockupAttack(entity) {
    const nearbyEntities = entity.dimension.getEntities({
        location: entity.location,
        excludeTypes: ["minecraft:item", "minecraft:xp_orb"],
        maxDistance: 8,
    });

    const knockupVerticalPower = 1.2;

    const difficulty = entity.getProperty("clown:difficulty");

    const damage = difficultyDamage.get(difficulty).damage ?? 0;

    for (let mob of nearbyEntities) {
        if (mob != entity) {
            if (mob instanceof Player) {
                if (entity.matches({ excludeFamilies: ["passive"] })) {
                    mob.applyDamage(damage, {
                        cause: EntityDamageCause.entityAttack,
                        damagingEntity: entity,
                    });

                    mob.applyKnockback(0, 0, 0, knockupVerticalPower);
                }
            } else {
                mob.applyDamage(damage, {
                    cause: EntityDamageCause.entityAttack,
                    damagingEntity: entity,
                });

                mob.applyImpulse({ x: 0, y: knockupVerticalPower, z: 0 });
            }
        }
    }
}
