import Vector2 from "../../../core/geom/Vector2.js";
import { AnimatedObjectEmitter } from "./AnimatedObjectEmitter.js";

/**
 * @template T
 */
class ViewEmitter {
    /**
     *
     * @constructor
     */
    constructor() {
        this.source = new Vector2();

        this.objectEmitter = new AnimatedObjectEmitter();

        this.objectEmitter.objectInitializer = function (view) {
            view.visible = true;
        };

        const self = this;
        this.objectEmitter.objectFactory = function (opt) {
            const view = self.viewFactory(opt);
            view.position.copy(self.source);

            return view;
        };

        /**
         *
         * @type {(T)=>View}
         */
        this.viewFactory = null;

        /**
         * Allows modifying timing of the view emitter by slowing or speeding up the time
         * @type {number}
         */
        this.timeScale = 1;

        this.on = this.objectEmitter.on;
    }

    /**
     *
     * @param {AnimationTrack} animationTrack
     * @param {function} updater
     */
    setAnimation(animationTrack, updater) {
        this.objectEmitter.setAnimation(animationTrack, updater);
    }

    /**
     *
     * @param {number} v
     */
    setRushThreshold(v) {
        this.objectEmitter.rushThreshold = v;
    }

    /**
     *
     * @param {number} v in seconds
     */
    setSpawnDelay(v) {
        this.objectEmitter.spawnDelay = v;
    }

    tick(timeDelta) {
        const scaledTimeDelta = timeDelta * this.timeScale;

        this.objectEmitter.tick(scaledTimeDelta);
    }

    /**
     *
     * @param options
     */
    spawn(options) {
        return this.objectEmitter.spawn(options);
    }

    /**
     *
     * @param {View} view
     */
    remove(view) {
        return this.objectEmitter.remove(view);
    }
}

export default ViewEmitter;
