import { EventBus } from "../EventBus";
import Level from "./Level";

class Boot extends Phaser.Scene {
    blurredScene: string | undefined;
    constructor() {
        super({ key: 'boot' });
        console.log("Boot constructor", this);
        this.blurredScene = undefined;

    }

    create() {
        // more setup stuff here
        // ...

        this.sys.game.events.on('pause', () => {
            for (let scene of this.scene.manager.scenes) {
                if (scene.scene.settings.active) {
                    this.blurredScene = scene.scene.key;
                    if ((scene as Level).onGamePause) {
                        (scene as Level).onGamePause();
                        scene.scene.pause();
                    }
                }
            }
        }, this);

        this.sys.game.events.on('blur', () => {
            for (let scene of this.scene.manager.scenes) {
                if (scene.scene.settings.active) {
                    this.blurredScene = scene.scene.key;
                    if ((scene as Level).onGamePause) {
                        (scene as Level).onGamePause();
                        scene.scene.pause();
                    }
                }
            }
        }, this);

        this.sys.game.events.on('focus', () => {
            if (this.blurredScene) {
                this.scene.resume(this.blurredScene);
                let scene = this.scene.manager.getScene(this.blurredScene);
                if ((scene as Level)?.onGameResume) {
                    (scene as Level).onGameResume();
                }
            }
            this.blurredScene = undefined;
        }, this);

        this.sys.game.events.on('resume', () => {
            if (this.blurredScene) {
                this.scene.resume(this.blurredScene);
                let scene = this.scene.manager.getScene(this.blurredScene);
                if ((scene as Level)?.onGameResume) {
                    (scene as Level).onGameResume();
                }
            }
            this.blurredScene = undefined;
        }, this);

        // This will make your game responsive.
        window.onresize = this.onWindowResize.bind(this);
        this.onWindowResize();

        this.scene.start('preloader');

        console.log("EMMITING current-scene-ready in BOOT");
        EventBus.emit('current-scene-ready', this);
    }

    onWindowResize() {
        this.scale.setZoom(this.game.config.zoom);
        const w = Math.ceil(+this.game.config.width);
        const h = Math.ceil(+this.game.config.height);
        this.sys.game.renderer.resize(w, h);
        // this.sys.game.config.width = w;
        // this.sys.game.config.height = h;
        this.scale.setGameSize(w, h);
        this.sys.canvas.style.width = (w * this.game.config.zoom) + 'px';
        this.sys.canvas.style.height = (h * this.game.config.zoom) + 'px';
        // Check which scene is active.
        for (let scene of this.scene.manager.scenes) {
            if (scene.cameras && scene.cameras.main) {
                // Scale the camera
                scene.cameras.main.setViewport(0, 0, w, h);
            }
            if (scene.scene.settings.active) {
                if ((scene as Level).resizeField) {
                    // Scale/position stuff in the scene itself with this method, that the scene must implement.
                    (scene as Level).resizeField(w, h);
                }
            }
        }
        if (this.blurredScene) {
            let scene = this.scene.manager.getScene(this.blurredScene);
            scene?.cameras.main.setViewport(0, 0, w, h);
            if ((scene as Level)?.resizeField) {
                (scene as Level).resizeField(w, h);
            }
        }
    }

}

export default Boot;
