import { Object3D } from "three";

import { getParam } from "../../engine/engine_utils.js";
import { Behaviour, GameObject } from "../Component.js";
import { XRFlag } from "./XRFlag.js";

export const debug = getParam("debugavatar");

/**
 * Event arguments for avatar marker creation and destruction events
 */
export type AvatarMarkerEventArgs = {
    /** The AvatarMarker component instance */
    avatarMarker: AvatarMarker;
    /** The GameObject that contains the avatar marker */
    gameObject: Object3D;
}

/**
 * Marks a GameObject as being controlled or owned by a player in networked XR sessions.  
 * This is used internally by the networking system to identify player-controlled objects.  
 *
 * **Note:** This is an internal marker class. For most use cases, use the {@link Avatar} component instead.  
 *
 * @summary Internal marker for player-controlled objects in networked sessions
 * @category XR
 * @category Networking
 * @group Components
 * @see {@link Avatar} for avatar setup and configuration
 */
// We might be updating this system in the future to a centralized API (PlayerView)
// but since currently quite a few core components rely on it, we're keeping it for now
export class AvatarMarker extends Behaviour {

    /**
     * Get an avatar marker by index from the global list of avatar markers.
     * @param index The index in the instances array
     * @returns The AvatarMarker at the specified index, or null if index is out of bounds
     */
    public static getAvatar(index: number): AvatarMarker | null {
        if (index >= 0 && index < AvatarMarker.instances.length)
            return AvatarMarker.instances[index];
        return null;
    }

    /** Global list of all active AvatarMarker instances */
    public static instances: AvatarMarker[] = [];

    /**
     * Subscribe to avatar marker creation events.
     * @param cb Callback function called when a new avatar marker is created
     * @returns The callback function (for removal)
     */
    public static onAvatarMarkerCreated(cb: (args: AvatarMarkerEventArgs) => void): Function {
        AvatarMarker._onNewAvatarMarkerAdded.push(cb);
        return cb;
    }

    /**
     * Subscribe to avatar marker destruction events.
     * @param cb Callback function called when an avatar marker is destroyed
     * @returns The callback function (for removal)
     */
    public static onAvatarMarkerDestroyed(cb: (args: AvatarMarkerEventArgs) => void): Function {
        AvatarMarker._onAvatarMarkerDestroyed.push(cb);
        return cb;
    }

    private static _onNewAvatarMarkerAdded: Array<(args: AvatarMarkerEventArgs) => void> = [];
    private static _onAvatarMarkerDestroyed: Array<(args: AvatarMarkerEventArgs) => void> = [];


    /** The network connection ID of the player who owns this avatar */
    public connectionId!: string;
    /** Reference to the avatar GameObject with optional XR flags */
    public avatar?: Object3D & { flags?: XRFlag[] }

    /** @internal */
    awake() {
        AvatarMarker.instances.push(this);
        if (debug)
            console.log(this);

        for (const cb of AvatarMarker._onNewAvatarMarkerAdded)
            cb({ avatarMarker: this, gameObject: this.gameObject });
    }

    /** @internal */
    onDestroy() {
        AvatarMarker.instances.splice(AvatarMarker.instances.indexOf(this), 1);

        for (const cb of AvatarMarker._onAvatarMarkerDestroyed)
            cb({ avatarMarker: this, gameObject: this.gameObject });
    }

    /**
     * Check if this avatar marker represents the local player.
     * @returns True if this avatar belongs to the local player, false otherwise
     */
    isLocalAvatar() {
        return this.connectionId === this.context.connection.connectionId;
    }
}
