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 };

    constructor(scene: PlayScene) {
        const x = 0;
        const y = 0;
        const width = 2000;
        const height = 744;
        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);
    }

    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() {
        if (!this.scene.isGameRunning) return;
        this.tilePositionX += (this.scene.gameSpeed * this.scene.gameSpeedModifier);
    }

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

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

        // Calculate aspect ratio for the background
        const bgAspectRatio = bgWidth / bgHeight;
        const screenAspectRatio = gameWidth / gameHeight;

        let scaleFactor;
        // Calculate scale factor based on the height or width
        if (screenAspectRatio > bgAspectRatio) {
            scaleFactor = gameHeight / bgHeight;
        } else {
            scaleFactor = gameWidth / bgWidth;
        }

        // Set the background to the new scaled size
        this.setDisplaySize(bgWidth * scaleFactor, bgHeight * scaleFactor);

        this.scaleFactors = { width: scaleFactor, height: scaleFactor };
    }


    // 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 };
    // }
}
