import { EventBus } from "../EventBus";
import PlayScene from "../scenes/PlayScene";

export class Sky extends Phaser.GameObjects.TileSprite {
    scene: PlayScene;
    scaleFactors: { width: number, height: number } = { width: 0, height: 0 };
    zoomFactor: number = 1;

    constructor(scene: PlayScene) {
        const x = 0;
        const y = 0;
        const width = 2000;
        const height = 743;
        super(scene, x, y, width, height, "sky");
        this.scene = scene;
        scene.add.existing(this);
        // scene.physics.add.existing(this);

        this.init();
        this.scene.events.on(Phaser.Scenes.Events.UPDATE, this.update, this);
        this.once(Phaser.GameObjects.Events.DESTROY, this.cleanup, this);
    }

    init() {
        this
            .setOrigin(0, 0)
            .setDepth(1);

        this.scaleToFit();
        EventBus.emit('sky-ready', this);
        // Handle resizing
        // this.scene.scale.on('resize', this.scaleToFit, this);
    }

    update(_time: number, delta: number = 1000 / 60) {
        if (!this.scene.isGameRunning) return;
        const frameScale = delta / (1000 / 60);
        this.tilePositionX += (this.scene.gameSpeed * this.scene.gameSpeedModifier * frameScale);
    }

    scaleToFit() {
        const gameWidth = this.scene.scale.width;
        const gameHeight = this.scene.scale.height;

        const bgWidth = 2000; // Actual width of the background image
        const bgHeight = 743;  // Actual height of the background image

        const scaleFactor = Math.max(gameWidth / bgWidth, gameHeight / bgHeight) * this.zoomFactor;

        this.setSize(gameWidth, gameHeight);
        this.setDisplaySize(gameWidth, gameHeight);
        this.setTileScale(scaleFactor, scaleFactor);
        this.scaleFactors = { width: scaleFactor, height: scaleFactor };
    }

    cleanup() {
        this.scene?.events.off(Phaser.Scenes.Events.UPDATE, this.update, this);
    }


    // scaleToFitFORNONE() {
    //     const screenWidth = window.innerWidth;
    //     const screenHeight = window.innerHeight;

    //     const widthScalingFactor = .8;
    //     const heightScalingFactor = .6;

    //     const gameWidthScaled = screenWidth * widthScalingFactor;
    //     const gameHeightScaled = screenHeight * heightScalingFactor;

    //     const skyWidthScaled = gameWidthScaled;
    //     const skyHeightScaled = gameHeightScaled;

    //     this.width = skyWidthScaled;
    //     this.height = skyHeightScaled;

    //     const bgWidth = 2000;
    //     const bgHeight = 744;
    //     let scaleFactor;
    //     const bgAspectRatio = bgWidth / bgHeight;
    //     const screenAspectRatio = screenWidth / screenHeight; 

    //     this.setDisplaySize(bgWidth, bgHeight);

    //     // Set the size of the game to the new scaled size
    //     this.scene.scale.setGameSize(gameWidthScaled - 9, gameHeightScaled - 9);

    //     this.scaleFactors = { width: widthScalingFactor, height: heightScalingFactor };
    // }
}
