class CashBackWin extends Phaser.Scene
{
    constructor ()
    {
        super({ key: "CashBackWin", active: true });
    }

    create (data)
    {
        this.parentScene = data.scene;

        this.sndCashbackWin = data.sndCashbackWin;
        this.sndWinToCredit = data.sndWinToCredit;
        this.sndWinToCreditEnd = data.sndWinToCreditEnd;

        this.bmTextCash = this.add.bitmapText(-1200, 310, "winFont", "CASH").setScale(1.1).setLetterSpacing(0);

        this.bmTextBack = this.add.bitmapText(-1200, 650, "winFont", "BACK").setScale(1.1).setLetterSpacing(-5);

        this.winCounterScale = 1.2;
        this.winCounterX = 540;
        this.winCounterY = 470;

        this.winCounter = new AnimatedAmount(this, this.winCounterX, this.winCounterY, "winFont", false);
        this.winCounter.setScale(this.winCounterScale).setSpacing(-50).setOrigin(0.5).setVisible(false);

        this.winCounterScaleCorrection = 1.0;
        
        this.scene.sleep();

        this.playInProgress = false;

        this.camera = this.cameras.main;
        
        this.camera.setOrigin(0);
    }
    
    onOrientationChange ()
    {
        if (this.parentScene.isLandscapeMode())
        {
            this.camera.setSize(CAMERA_WIDTH, CAMERA_HEIGHT);
            this.camera.setPosition(CAMERA_LANDSCAPE_X, CAMERA_LANDSCAPE_Y);
            this.camera.setScroll(CAMERA_SCROLL_LANDSCAPE_X, CAMERA_SCROLL_LANDSCAPE_Y);
        }
        else
        {
            this.camera.setSize(CAMERA_WIDTH, CAMERA_HEIGHT * 2 - 110);        
            this.camera.setPosition(CAMERA_PORTRAIT_X, CAMERA_PORTRAIT_Y);
            this.camera.setScroll(CAMERA_SCROLL_PORTRAIT_X, CAMERA_SCROLL_PORTRAIT_Y + 20);
        }
    }

    reset ()
    {
        this.winCounter.setPosition(this.winCounterX, this.winCounterY).setScale(this.winCounterScale).setAlpha(1.0).setVisible(false);

        this.tweens.killTweensOf(this.winCounter.bmText);
        this.tweens.killTweensOf(this.bmTextCash);
        this.tweens.killTweensOf(this.bmTextBack);

        this.tweens.killTweensOf(this.sndWinToCredit);

        this.winCounterScaleCorrection = 1.0;
    }

    start ()
    {
        this.cashbackWin = this.parentScene.gameSocket.getCashbackWin()

        this.winCounter.setSpeed(1).reset(this.cashbackWin);

        this.playInProgress = false;

        this.scene.wake();
        this.scene.bringToTop();
    }

    stop ()
    {
        this.reset();
        
        this.parentScene.collectCashbackComplete();
    }

    update (time, delta) 
    {
        if (!this.playInProgress)
        {
            this.playInProgress = true;
            this.sndCashbackWin.play();
            this.showCashText();
        }
            
        this.winCounter.update(delta, this.cashbackWin);
    }

    setPrecision (v)
    {
        this.winCounter.setPrecision(v);
    }

    showCashText ()
    {
        this.tweens.add({

            targets: this.bmTextCash,
            ease: "Linear",
            duration: 250,
            x: 70,

            callbackScope: this,
            onComplete: function() 
            {
                this.showBackText();
            },
        });
    }

    hideCashText ()
    {
        this.tweens.add({

            targets: this.bmTextCash,
            ease: "Linear",
            duration: 250,
            x: -1200,
        });
    }

    showBackText ()
    {
        this.tweens.add({

            targets: this.bmTextBack,
            ease: "Linear",
            duration: 250,
            x: 70,

            callbackScope: this,
            onComplete: function() 
            {
                this.showWinCounter();
            },
        });
    }

    hideBackText ()
    {
        this.tweens.add({

            targets: this.bmTextBack,
            ease: "Linear",
            duration: 250,
            x: 1350,
        });
    }

    showWinCounter ()
    {
        this.winCounter.setScale(this.winCounterScale);
        let r = this.winCounter.getTextBounds();

        if(r.global.width > GAME_MAX_DYNAMIC_TEXT_WIDTH_PORTRAIT)
        {
            this.winCounterScaleCorrection = GAME_MAX_DYNAMIC_TEXT_WIDTH_PORTRAIT / r.global.width;
            this.winCounter.setScale(this.winCounterScale * this.winCounterScaleCorrection);
        }

        this.winCounter.setPosition(-1000, this.winCounterY -  (218 / 2) * (1 - this.winCounterScaleCorrection)).setVisible(true);

        this.tweens.add({

            targets: this.winCounter.bmText,
            ease: "Linear",
            duration: 250,
            x: this.winCounterX,

            callbackScope: this,
            onComplete: function() 
            {
                this.pulseWinCounter();

                this.time.delayedCall(3000, this.parentScene.collectCashbackWait, [], this.parentScene);
            },
        });
    }

    pulseWinCounter ()
    {
        this.winCounter.setScale(this.winCounterScale * this.winCounterScaleCorrection);

        this.tweens.add({

            targets: this.winCounter.bmText,
            ease: "Linear",
            duration: 250,
            scale: (this.winCounterScale - 0.1) * this.winCounterScaleCorrection,
            repeat: -1,
            yoyo: true,
        });
    }

    hideWinCounter ()
    {
        this.tweens.add({

            targets: this.winCounter.bmText,
            ease: "Linear",
            duration: 250,
            x: 225,
            y: 920,
            alpha: 0.0,

            callbackScope: this,
            onComplete: function() 
            {
                this.time.delayedCall(500, this.stop(), [], this);
            },
        });
    }

    collectCashback ()
    {
        this.parentScene.balanceUpdate2(this.cashbackWin);//update balance

        this.sndWinToCredit.play();        
        
        this.tweens.add({

            targets: this.sndCashbackWin,
            volume: 0,

            ease: "Linear",
            duration: 500,

            callbackScope: this,
            onComplete: function() 
            {
                this.sndCashbackWin.stop();
                this.sndCashbackWin.setVolume(1.0);
                this.sndWinToCreditEnd.play();

                this.scene.sleep();
            }                        
        });
        
        this.hideCashText();
        this.hideBackText();
        this.hideWinCounter();
    }
}
