"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Core = require("@intelligentgraphics/ig.gfx.core");
var ig_utilities_1 = require("@intelligentgraphics/ig.utilities");
var Util_1 = require("./Util");
/**
 * Creates an animation manager that manages animations and positions for all animatable components in the whole scene
 *
 * @param {PlannerManager} plannerManager
 * @returns {SceneAnimationManager}
 */
exports.createSceneAnimationManager = function (session, plannerManager, _a, animatorOptions) {
    var _b = _a === void 0 ? {} : _a, _c = _b.continueAnimationsAfterUpdate, continueAnimationsAfterUpdate = _c === void 0 ? true : _c, _d = _b.reapplyAnimationProgressAfterUpdate, reapplyAnimationProgressAfterUpdate = _d === void 0 ? true : _d;
    if (continueAnimationsAfterUpdate) {
        reapplyAnimationProgressAfterUpdate = true;
    }
    var eventEmitter = new ig_utilities_1.EventEmitter();
    var getAnimatableComIds = function () {
        return session
            .getFilteredArticles(function (article) {
            return !ig_utilities_1.isNullOrUndefined(article.gfxId) &&
                !ig_utilities_1.isNullOrUndefined(Core.State.API.GetComponent(article.gfxId)) &&
                Core.State.API.GetComponentData(article.gfxId)
                    .hasAnimations === true;
        })
            .map(function (article) { return article.comId; });
    };
    var getRootIds = function () {
        return getAnimatableComIds()
            .map(function (comId) { return session.getArticleGfxId(comId); })
            .filter(ig_utilities_1.isValue);
    };
    var getPosition = function () {
        return Util_1.getPosition(Core.State.API, getRootIds());
    };
    var canAnimate = function () { return getAnimatableComIds().length > 0; };
    var activeStatus = getPosition() === "end" ? "end" : "start";
    var animatable = false;
    var activePlaybackControl;
    var activeAnimationParams;
    var getStatus = function () { return activeStatus; };
    var getActiveOrNextAnimationDirection = function () {
        if (preUpdateState !== undefined &&
            reapplyAnimationProgressAfterUpdate) {
            return preUpdateState.continueForward ? "forwards" : "backwards";
        }
        var position = getPosition();
        return position === "end" ? "backwards" : "forwards";
    };
    var updateStatus = function (newStatus) {
        activeStatus = newStatus;
        eventEmitter.emit("statusChange", newStatus);
    };
    var setPosition = function (position) {
        if (activePlaybackControl !== undefined) {
            activePlaybackControl.stop();
            activePlaybackControl = undefined;
        }
        Core.Animation.setPosition(getRootIds(), position === "end" ? "End" : "Start");
        updateStatus(position);
    };
    var showAnimation = function (params) {
        if (activePlaybackControl !== undefined) {
            return;
        }
        activeAnimationParams = params;
        activePlaybackControl = Core.Animation.showAnimation(getRootIds(), params, animatorOptions);
        updateStatus("running");
        var newStatus = params.forward === !params.invertAnimationAfter ? "end" : "start";
        activePlaybackControl.done
            .then(function () {
            updateStatus(newStatus);
        })
            .finally(function () {
            activePlaybackControl = undefined;
        })
            .catch(ig_utilities_1.noop);
    };
    var showAnimationFromCurrentPosition = function (invertAnimationAfter) {
        var position = getPosition();
        return showAnimation({
            forward: position !== "end",
            invertAnimationAfter: invertAnimationAfter,
        });
    };
    var pauseAnimation = function () {
        if (activePlaybackControl !== undefined) {
            activePlaybackControl.pause();
            updateStatus("paused");
        }
    };
    var continueAnimation = function () {
        if (activePlaybackControl !== undefined) {
            activePlaybackControl.continue();
            updateStatus("running");
        }
    };
    var stopAnimation = function () {
        if (activePlaybackControl !== undefined) {
            activePlaybackControl.stop();
            activePlaybackControl = undefined;
            updateStatus("stopped");
        }
    };
    animatable = canAnimate();
    var preUpdateState;
    plannerManager.hooks.sceneUpdateStart.tap("AnimationManager", function () {
        var e_1, _a;
        preUpdateState = {
            maxProgress: 0,
            continueAfterUpdate: false,
            continueForward: true,
            animationWasPaused: false,
        };
        var animatableComIds = getAnimatableComIds();
        try {
            for (var animatableComIds_1 = tslib_1.__values(animatableComIds), animatableComIds_1_1 = animatableComIds_1.next(); !animatableComIds_1_1.done; animatableComIds_1_1 = animatableComIds_1.next()) {
                var comId = animatableComIds_1_1.value;
                var gfxId = session.getArticleGfxId(comId);
                var _b = Core.State.API.GetComponentData(gfxId), animationProgress = _b.animationProgress, animationPosition = _b.animationPosition;
                if (animationProgress !== undefined) {
                    preUpdateState.maxProgress = Math.max(animationProgress, preUpdateState.maxProgress);
                }
                if (animationPosition !== undefined &&
                    preUpdateState.continueForward) {
                    preUpdateState.continueForward = animationPosition !== "End";
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (animatableComIds_1_1 && !animatableComIds_1_1.done && (_a = animatableComIds_1.return)) _a.call(animatableComIds_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        if (activePlaybackControl !== undefined) {
            preUpdateState.animationWasPaused = activeStatus === "paused";
            preUpdateState.continueAfterUpdate = true;
            stopAnimation();
        }
    });
    plannerManager.hooks.articleUpdateDone.tap("AnimationManager", function (article, update) {
        var maxProgress = preUpdateState.maxProgress;
        if (!continueAnimationsAfterUpdate) {
            if (update.updateType !== "materialOnly") {
                setPosition("start");
            }
            return;
        }
        if (reapplyAnimationProgressAfterUpdate) {
            Core.Animation.setProgress([article.gfxId], maxProgress);
        }
    });
    plannerManager.hooks.sceneUpdateDone.tap("AnimationManager", function () {
        var e_2, _a;
        var _b = preUpdateState, maxProgress = _b.maxProgress, continueAfterUpdate = _b.continueAfterUpdate, continueForward = _b.continueForward, animationWasPaused = _b.animationWasPaused;
        if (continueAnimationsAfterUpdate) {
            try {
                // reapply animation positions here, since they might have been lost in the update
                for (var _c = tslib_1.__values(getAnimatableComIds()), _d = _c.next(); !_d.done; _d = _c.next()) {
                    var comId = _d.value;
                    var gfxId = session.getArticleGfxId(comId);
                    if (gfxId !== undefined) {
                        Core.State.API.SetComponentData(gfxId, "animationPosition", continueForward ? "Start" : "End");
                    }
                }
            }
            catch (e_2_1) { e_2 = { error: e_2_1 }; }
            finally {
                try {
                    if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
                }
                finally { if (e_2) throw e_2.error; }
            }
            if (continueAfterUpdate) {
                showAnimation({
                    forward: continueForward,
                    startFromProgress: maxProgress,
                });
                if (animationWasPaused) {
                    pauseAnimation();
                }
            }
        }
        preUpdateState = undefined;
        var canNowAnimate = canAnimate();
        if (canNowAnimate !== animatable) {
            animatable = canNowAnimate;
            eventEmitter.emit("animateabilityChange", animatable);
        }
    });
    return {
        on: eventEmitter.on.bind(eventEmitter),
        off: eventEmitter.off.bind(eventEmitter),
        once: eventEmitter.once.bind(eventEmitter),
        canAnimate: canAnimate,
        setPosition: setPosition,
        getPosition: getPosition,
        getStatus: getStatus,
        getActiveOrNextAnimationDirection: getActiveOrNextAnimationDirection,
        showAnimation: showAnimation,
        showAnimationFromCurrentPosition: showAnimationFromCurrentPosition,
        pauseAnimation: pauseAnimation,
        continueAnimation: continueAnimation,
        stopAnimation: stopAnimation,
    };
};
