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

function distance(a, b) {
    const dx = a.x - b.x;
    const dy = a.y - b.y;
    const dz = a.z - b.z;
    return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
export const activeBeacons = new Map();

function playBeaconCycle(player, entityLoc, key, entity) {
    try {
        const beaconType = world.getDynamicProperty("beacon_type") ?? "Red";
        const suffix = "_" + beaconType.toLowerCase();
        system.runTimeout(() => player.spawnParticle(`cbcl_rm:beacon_appear_1${suffix}`, entityLoc), 1);
        system.runTimeout(() => player.spawnParticle(`cbcl_rm:beacon_appear_2${suffix}`, entityLoc), 2);
        system.runTimeout(() => player.spawnParticle(`cbcl_rm:beacon_appear_3${suffix}`, entityLoc), 3);
        system.runTimeout(() => player.spawnParticle(`cbcl_rm:beacon_appear_4${suffix}`, entityLoc), 4);

        const stayInterval = system.runInterval(() => {

            try {
                if (!player || !player.location) {
                    system.clearRun(stayInterval);
                    activeBeacons.delete(key);
                    return;
                }
                if (!entity.isValid) {
                    system.clearRun(stayInterval);
                    activeBeacons.delete(key);
                    return;
                }
                const dist = distance(player.location, entityLoc);

                if (dist <= 5) {
                    system.clearRun(stayInterval);

                    system.runTimeout(() => player.spawnParticle(`cbcl_rm:beacon_disappear_1${suffix}`, entityLoc), 1);
                    system.runTimeout(() => player.spawnParticle(`cbcl_rm:beacon_disappear_2${suffix}`, entityLoc), 2);

                    activeBeacons.delete(key);
                    return;
                }

                player.spawnParticle(`cbcl_rm:beacon_stay_1${suffix}`, entityLoc);
                player.spawnParticle(`cbcl_rm:beacon_stay_2${suffix}`, entityLoc);

            } catch (e) {

                system.clearRun(stayInterval);
                activeBeacons.delete(key);
            }
        }, 5);
    } catch (e) {

        activeBeacons.delete(key);
    }
}

export function beaconParticle() {
    const players = world.getPlayers();
    const deathBeaconEnabled = world.getDynamicProperty("death_beacon");

    if (!deathBeaconEnabled) return;

    for (const player of players) {
        if (!player || !player.location) continue;

        const entities = world.getDimension(player.dimension.id).getEntities({
            type: "cbcl_rm:interact_dummy",
            location: player.location,
            maxDistance: 200
        });

        for (const interactEntity of entities) {
            const key = player.id + "_" + interactEntity.id;

            if (!interactEntity || !interactEntity.location) continue;

            const dist = distance(player.location, interactEntity.location);

            if (dist > 5 && !activeBeacons.has(key)) {
                activeBeacons.set(key, true);
                playBeaconCycle(player, interactEntity.location, key, interactEntity);
            }
        }
    }
}
