import LabelView from "../view/common/LabelView.js";

/**
 * Used for monitoring progress of the engine startup
 */
export class EngineBootstrapper {

    /**
     *
     * @type {Node}
     */
    rootNode = null;
        

    /**
     *
     * @param {Engine} engine
     * @returns {Promise}
     */
    boot(engine) {

        const v = new LabelView('Engine initialization');
        v.css({
            color: 'white'
        });
        v.link();

        this.rootNode.appendChild(v.el);

        // NOTE: the engine used to be started here, this was moved out
        const started = Promise.resolve();

        const vExecutionEngineState = new LabelView('');
        const vAssetManagerState = new LabelView('');

        v.addChild(vExecutionEngineState);
        v.addChild(vAssetManagerState);

        const update_stats = () => {
            const executor = engine.executor;
            const assetManager = engine.assetManager;

            vExecutionEngineState.updateText(`Executor: ${executor.queueReady.length} ready, ${executor.queueUnresolved.length} unresolved`);
            vAssetManagerState.updateText(`Assets: ${assetManager.assets.size} loaded, ${assetManager.request_map.size} pending`);
        };

        const update_loop = () => {
            update_stats();

            if (v.isLinked) {
                requestIdleCallback(update_loop)
            }
        };

        update_loop();

        started.then(() => {

            this.rootNode.removeChild(v.el);

            v.unlink();
        });


        return started;
    }
}
