export class PlayButton extends Phaser.GameObjects.Container {
    scene;
    playButton;
    shadow;
    pulseTween;
    buttonWidth = 0;
    buttonHeight = 0;
    constructor(scene) {
        super(scene, scene.scale.width / 2, scene.scale.height / 4);
        this.scene = scene;
        this.playButton = this.scene.add.image(0, 0, "play").setInteractive({ useHandCursor: true });
        this.shadow = this.scene.add.image(0, 0, "play").setTint(0x000000).setAlpha(0.5);
        this.init();
        this.scene.add.existing(this);
        this.once(Phaser.GameObjects.Events.DESTROY, this.cleanup, this);
    }
    init() {
        const scaleFactor = this.scene.mountain?.scaleFactors.height || 1;
        const width = 185;
        const height = 185;
        this.playButton.setPosition(0, 0);
        this.playButton.setVisible(false);
        this.setButtonDisplaySize(width * scaleFactor, height * scaleFactor);
        this.shadow.setPosition(this.playButton.x + 5, this.playButton.y + 5);
        this.shadow.setVisible(false);
        this.add([this.shadow, this.playButton])
            .setAlpha(1)
            .setDepth(3);
        // Handle resizing
        this.scene.scale.on('resize', this.scaleToFit, this);
        this.scene.scale.on('resize', this.positionOnMountain, this);
        this.pulseTween = this.scene.tweens.add({
            targets: this,
            scaleX: 1.2,
            scaleY: 1.2,
            duration: 1300,
            yoyo: true,
            ease: 'Power1',
            repeat: -1
        });
    }
    positionOnMountain() {
        this.setY(this.scene.scale.height / 2);
        this.setX(this.scene.scale.width / 2);
        this.playButton.setVisible(true);
        this.shadow.setVisible(true);
    }
    scaleToFit() {
        // Use the scale factor from the Sky class to maintain consistency
        const scaleFactor = this.scene.mountain?.scaleFactors.height || 1;
        this.setButtonDisplaySize(185 * scaleFactor, 185 * scaleFactor);
    }
    cleanup() {
        this.scene?.scale.off('resize', this.scaleToFit, this);
        this.scene?.scale.off('resize', this.positionOnMountain, this);
        this.scene?.tweens.killTweensOf(this);
    }
    positionOnMountainStopButton() {
        this.scaleToFit();
        const scaleFactorX = this.scene.mountain?.scaleFactors.width || 1;
        const scaleFactorY = this.scene.mountain?.scaleFactors.height || 1;
        const marginX = 18 * scaleFactorX;
        const marginY = 18 * scaleFactorY;
        const targetX = this.scene.scale.width - (this.buttonWidth / 2) - marginX;
        const targetY = this.scene.scale.height - (this.buttonHeight / 2) - marginY;
        // Create a tween to animate the movement from the current position to the target position.
        this.scene.tweens.add({
            targets: this,
            x: targetX,
            y: targetY,
            ease: 'Cubic.easeOut',
            duration: 600,
            // Optional callbacks:
            onStart: () => {
                // code to execute at the start of the tween
            },
            onComplete: () => {
                this.playButton.setTexture("stop");
                this.scaleToFit();
                this.setX(Math.min(this.x, this.scene.scale.width - (this.buttonWidth / 2) - marginX));
                this.setY(Math.min(this.y, this.scene.scale.height - (this.buttonHeight / 2) - marginY));
            }
        });
    }
    onGameStart() {
        // when the game starts, the play button goes to the bottom right corner and becomes 'stop' 
        this.playButton.setInteractive();
        this.playButton.setVisible(true);
        this.shadow.setVisible(true);
        this.positionOnMountainStopButton();
    }
    onGameOver() {
        // when the game is over (player died), the play button goes to the middle of the screen and becomes 'play'
        this.playButton.setTexture("play");
        this.playButton.setInteractive();
        this.playButton.setAlpha(1);
        this.positionOnMountain();
    }
    onFlagsCollected() {
        // when the flags are collected, the play button goes to the middle of the screen and becomes 'play'
        this.playButton.setTexture("play");
        this.playButton.setInteractive();
        this.playButton.setAlpha(1);
        this.scaleToFit();
        this.positionOnMountain();
    }
    show() {
        this.setAlpha(1).setVisible(true).setActive(true);
        this.playButton.setVisible(true);
        this.shadow.setVisible(true);
        this.playButton.setInteractive({ useHandCursor: true });
        this.pulseTween?.resume();
    }
    hide() {
        this.setAlpha(0).setVisible(false).setActive(false);
        this.playButton.setVisible(false);
        this.shadow.setVisible(false);
        this.playButton.disableInteractive();
        this.pulseTween?.pause();
    }
    setButtonDisplaySize(width, height) {
        this.buttonWidth = width;
        this.buttonHeight = height;
        this.playButton.setDisplaySize(width, height);
        this.shadow.setDisplaySize(width, height);
    }
}
