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.5,
        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.stilt_walker) {
        switch (eventId) {
            case "clown:attack_sequence": {
                sequenceAttack(entity);
                break;
            }
            case "clown:scream_attack": {
                screamAttack(entity);
                break;
            }
            case "clown:move_player_away": {
                movePlayerAway(entity, 1.7);
                break;
            }
        }
    }
});

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

    const dimension = entity.dimension;

    const attackRange = 4;

    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 screamAttack(entity) {
    const nearbyEntities = entity.dimension.getEntities({
        location: entity.location,
        excludeTypes: ["minecraft:item", "minecraft:xp_orb"],
        maxDistance: 8,
    });

    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,
                    });
                }
            } else {
                mob.applyDamage(damage, {
                    cause: EntityDamageCause.entityAttack,
                    damagingEntity: entity,
                });
            }
        }
    }
}
