import Loading from "../Loading/Loading";
import IScene from "../components/IScene";
import Scene from "../views/Scene";
import ComponentBase from "../Components/ComponentBase";

export default class SceneCtrl {
    public get parent(): eui.Group {
        return this._parent;
    }
    private _parent: eui.Group;
    private _currentScene: Scene;

    static _instance: SceneCtrl;
    static get instance() {
        return SceneCtrl._instance || (SceneCtrl._instance = new SceneCtrl())
    }
    init(parent: eui.Group) {
        this._parent = parent;
    }

    change<T extends Scene>(cls: any, data?: any): T {
        let scene: Scene = new cls(data);

        if (this._currentScene) {
            let preScene = this._currentScene;
            scene.visible = false;
            scene.onLoaded = () => {
                preScene.destroy();
                scene.visible = true;
            }
        }

        this._currentScene = scene;
        this._parent.addChild(scene);

        return scene as T;
    }
}