class GameOver extends Phaser.Scene {
    constructor() {
        super('GameOver');
    }

    init(data) {
        this.won = data.won || false;
        this.score = data.score || 0;
        this.total = data.total || 0;
        this.timeVal = data.time || 0;
        this.best = data.best || 0;
        this.newBest = data.newBest || false;
    }

    create() {
        this.cameras.main.setBackgroundColor(0x0F0F23);

        if (this.won) {
            this.add.text(480, 160, 'MISSION COMPLETE!', {
                fontFamily: 'Arial Black',
                fontSize: '42px',
                color: '#00ff9d',
                stroke: '#000000',
                strokeThickness: 8
            }).setOrigin(0.5);

            this.add.text(480, 230, 'All ' + this.total + ' cards collected!', {
                fontFamily: 'Arial',
                fontSize: '20px',
                color: '#ffd700'
            }).setOrigin(0.5);

            this.add.text(480, 270, 'Time: ' + this.formatTime(this.timeVal), {
                fontFamily: 'Arial Black',
                fontSize: '24px',
                color: '#ffffff',
                stroke: '#000000',
                strokeThickness: 4
            }).setOrigin(0.5);

            if (this.newBest) {
                this.add.text(480, 300, 'NEW BEST!', {
                    fontFamily: 'Arial Black',
                    fontSize: '18px',
                    color: '#00ff9d',
                    stroke: '#000000',
                    strokeThickness: 3
                }).setOrigin(0.5);
            }

            if (this.best > 0) {
                this.add.text(480, 325, 'FSYS Agent: ' + this.formatTime(this.best), {
                    fontFamily: 'Arial',
                    fontSize: '14px',
                    color: '#b9cbbc'
                }).setOrigin(0.5);
            }

            this.add.text(480, 350, 'The food system is restored.', {
                fontFamily: 'Arial',
                fontSize: '14px',
                color: '#849587'
            }).setOrigin(0.5);
        } else {
            this.add.text(480, 200, 'MISSION FAILED', {
                fontFamily: 'Arial Black',
                fontSize: '42px',
                color: '#ffb4ab',
                stroke: '#000000',
                strokeThickness: 8
            }).setOrigin(0.5);

            this.add.text(480, 270, 'Cards: ' + this.score + ' / ' + this.total, {
                fontFamily: 'Arial',
                fontSize: '20px',
                color: '#b9cbbc'
            }).setOrigin(0.5);

            this.add.text(480, 295, 'Time: ' + this.formatTime(this.timeVal), {
                fontFamily: 'Arial',
                fontSize: '18px',
                color: '#ffffff',
                stroke: '#000000',
                strokeThickness: 3
            }).setOrigin(0.5);

            if (this.best > 0 && this.best < Infinity) {
                this.add.text(480, 320, 'FSYS Agent: ' + this.formatTime(this.best), {
                    fontFamily: 'Arial',
                    fontSize: '14px',
                    color: '#b9cbbc'
                }).setOrigin(0.5);
            }

            this.add.text(480, 350, 'Don\'t give up!', {
                fontFamily: 'Arial',
                fontSize: '14px',
                color: '#849587'
            }).setOrigin(0.5);
        }

        var retryBtn = this.add.text(480, 420, 'PLAY AGAIN', {
            fontFamily: 'Arial Black',
            fontSize: '24px',
            color: '#52ffff',
            stroke: '#000000',
            strokeThickness: 4,
            padding: { x: 20, y: 10 }
        }).setOrigin(0.5).setInteractive({ useHandCursor: true });

        retryBtn.on('pointerover', function() { retryBtn.setColor('#00ff9d'); });
        retryBtn.on('pointerout', function() { retryBtn.setColor('#52ffff'); });
        retryBtn.on('pointerdown', function() {
            this.scene.start('GameScene');
        }, this);

        var border = this.add.rectangle(480, 420, retryBtn.width + 20, retryBtn.height + 10)
            .setStrokeStyle(2, 0x52ffff, 0.6);

        this.tweens.add({
            targets: border,
            alpha: 0.3,
            duration: 1000,
            yoyo: true,
            repeat: -1
        });

        this.add.text(480, 550, 'FSYS Agent 2050', {
            fontFamily: 'Arial',
            fontSize: '12px',
            color: '#3b4a3f'
        }).setOrigin(0.5);
    }

    formatTime(seconds) {
        var m = Math.floor(seconds / 60);
        var s = Math.floor(seconds % 60);
        return (m < 10 ? '0' : '') + m + ':' + (s < 10 ? '0' : '') + s;
    }
}
