import { assert } from "../../../../../core/assert.js";
import { array_remove_first } from "../../../../../core/collection/array/array_remove_first.js";
import { SGAnimationPlayback } from "./SGAnimationPlayback.js";

export class SGMeshAnimationController {
    /**
     *
     * @type {SGAnimationPlayback[]}
     */
    active = []

    /**
     *
     * @type {EntityNodeAnimationClip[]}
     */
    bound = []

    /**
     *
     * @param {string} name
     * @return {EntityNodeAnimationClip|undefined}
     */
    getBoundByName(name) {
        assert.isString(name, 'name');

        return this.bound.find(b => b.name === name);
    }

    /**
     *
     * @param {string} name
     * @return {SGAnimationPlayback|undefined}
     */
    getActiveByName(name) {
        assert.isString(name, 'name');

        return this.active.find(a => a.clip_name === name);
    }


    /**
     *
     * @param {string} name
     * @param {boolean} loop
     * @param {number} time_offset
     * @returns {SGAnimationPlayback}
     */
    start(name, loop = false, time_offset = 0) {
        assert.isString(name, 'name');
        assert.isBoolean(loop, 'loop');
        assert.isNumber(time_offset, 'time_offset');

        const playback = new SGAnimationPlayback();

        playback.clip_name = name;
        playback.loop = loop;
        playback.time = time_offset;

        this.active.push(playback);

        return playback;
    }

    /**
     *
     * @param {SGAnimationPlayback} playback
     * @return {boolean}
     */
    stop(playback) {

        assert.defined(playback, 'playback');
        assert.notNull(playback, 'playback');

        return array_remove_first(this.active, playback)
    }
}

