class CashBack extends Phaser.Scene {

    constructor ()
    {
        super({ key: 'CashBack', active: true });
    }

    create (data)
    {
        this.parent = data.scene;
        this.x = data.x;
        this.y = data.y;
        this.vDelta = data.vDelta;

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

        this.bmTextCash = this.add.bitmapText(-1000, 228 + this.vDelta, data.fontText, 'CASH');
        this.bmTextCash.letterSpacing = -27;
        this.bmTextCash.setScale(1.0);
        this.txtCashX = 180;

        this.bmTextBack = this.add.bitmapText(1300, 570 + this.vDelta, data.fontText, 'BACK');
        this.bmTextBack.letterSpacing = -32;
        this.bmTextBack.setScale(1.0);
        this.txtBackX = 180;

        this.animWinScale = 1.05;
        this.animWinX = 640 * this.animWinScale;
        this.animWinY = 245;

        this.animWin = new AnimatedAmount(this, this.animWinX, this.animWinY + this.vDelta, 'totalwinNumbers', false);
        this.animWin.setScale(this.animWinScale);
        this.animWin.setSpacing(-70);
        this.animWin.setOrigin(0.5);
        this.animWin.setVisible(false);
        
        this.timer = 100;
        this.scene.sleep();

        this.tweening = false;
        this.tweeningended = false;
        this.sceneCompleted = false;

        this.yoyoCashbackWinActionTween = null;
        this.animWinScaleCorrection = 1.0;
    }
    
    onOrientationChange(offs)
    {
        this.vDelta = offs;

        this.bmTextCash.setPosition(this.bmTextCash.x, 228 + this.vDelta);
        this.bmTextBack.setPosition(this.bmTextBack.x, 570 + this.vDelta);

        if (this.yoyoCashbackWinActionTween == null || !this.yoyoCashbackWinActionTween.isPlaying())
        {
            this.animWin.setPosition(this.animWin.bmText.x, this.animWinY + this.vDelta);
        }
        else if(this.yoyoCashbackWinActionTween.isPlaying())
        {
            this.animWin.setPosition(this.animWinX * this.animWinScaleCorrection, this.animWinY + this.vDelta);

            this.yoyoCashbackWinActionTween = this.createYoyoTween(this.yoyoCashbackWinActionTween);
        }
    }

    start()
    {
        this.reset();
        this.animWin.setSpeed(1);
        this.animWin.reset(this.parent.gameSocket.getCashbackWin());

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

    reset()
    {
        this.animWin.setPosition(this.animWinX, this.animWinY + this.vDelta);
        this.animWin.setScale(this.animWinScale);
        this.animWin.setAlpha(1.0);
        this.animWin.setVisible(false);

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

        this.tweens.killTweensOf(this.sndWin);
        this.tweens.killTweensOf(this.sndWinToCredit);
        this.tweens.killTweensOf(this.sndCashbackWin);

        this.timer = 100;

        this.tweening = false;
        this.tweeningended = false;
        this.sceneCompleted = false;

        this.yoyoCashbackWinActionTween = null;
        this.animWinScaleCorrection = 1.0;
    }

    update(time, delta) 
    {
        if (this.parent.gameState == GAME_STATE_NOT_INITIALIZED || this.sceneCompleted)
        {
            return;
        }

        this.timer -= delta;
        
        if(this.timer < 0)
        {
            if(!this.tweening && !this.tweeningended)
            {
                this.tweening = true;
                this.sndCashbackWin.play();
                this.moveCashAction();
                return;
            }
            
            if(this.tweeningended)
            {
                if(this.sndWinToCredit.isPlaying)
                {
                    return;
                }
                else
                {
                    this.sceneCompleted = true;
                    this.endActions();
                }
            }                
        }

        this.animWin.update(delta, this.parent.gameSocket.getCashbackWin());
    }

    moveCashAction()
    {
        this.tweens.add({

            targets: this.bmTextCash,
            ease: 'Linear',
            duration: 250,
            x: this.txtCashX,

            onCompleteScope: this,
            onComplete: function() 
            {
                this.moveBackAction();
            },
        });
    }

    moveToHideCashAction()
    {
        this.tweens.add({

            targets: this.bmTextCash,
            ease: 'Linear',
            duration: 250,
            x: -1000,
        });
    }

    moveBackAction()
    {
        this.tweens.add({

            targets: this.bmTextBack,
            ease: 'Linear',
            duration: 250,
            x: this.txtBackX,

            onCompleteScope: this,
            onComplete: function() 
            {
                this.positionAnimWinAction();
            },
        });
    }

    moveToHideBackAction()
    {
        this.tweens.add({

            targets: this.bmTextBack,
            ease: 'Linear',
            duration: 250,
            x: 1300,
        });
    }

    positionAnimWinAction()
    {
        this.animWin.reset(this.parent.gameSocket.getCashbackWin());
        this.animWin.setScale(this.animWinScale);
        let r = this.animWin.getTextBounds();

        if(r.global.width > GAME_MAX_DYNAMIC_TEXT_WIDTH)
        {
            this.animWinScaleCorrection = GAME_MAX_DYNAMIC_TEXT_WIDTH / r.global.width;
            this.animWin.setScale(this.animWinScale * this.animWinScaleCorrection);
        }
        
        this.animWin.setPosition(-1000, this.animWinY + this.vDelta);
        this.animWin.setVisible(true);

        this.tweens.add({

            targets: this.animWin.bmText,
            ease: 'Linear',
            duration: 250,
            x: this.animWinX,

            onCompleteScope: this,
            onComplete: function() 
            {
                this.yoyoCashbackWinAction();
                setTimeout(this.onCashbackWait.bind(this), 3000);
            },
        });
    }

    createYoyoTween(tween)
    {
        if(tween != null && tween.isPlaying())
        {
            this.repeat = Math.floor((tween.totalDuration - tween.totalElapsed) / 500);
            
            if(this.repeat < 0)
            {
                this.repeat = 0;
            }

            tween.stop();
        }

        this.animWin.setScale(this.animWinScale * this.animWinScaleCorrection);

        tween = this.tweens.add({

            targets: this.animWin.bmText,
            ease: 'Linear',
            duration: 250,
            scale: (this.animWinScale - 0.1) * this.animWinScaleCorrection,
            repeat: -1,
            yoyo: true,
        });

        return tween;
    }

    yoyoCashbackWinAction()
    {
        this.yoyoCashbackWinActionTween = this.createYoyoTween(this.yoyoCashbackWinActionTween);
    }

    moveToHideAnimWinAction()
    {
        this.tweens.add({

            targets: this.animWin.bmText,
            ease: 'Linear',
            duration: 250,
            x: 275,
            y: 590 + this.vDelta,
            alpha: 0.0,

            onCompleteScope: this,
            onComplete: function() 
            {
                this.tweeningended = true;
            },
        });
    }

    endActions()
    {
        this.tweening = false;
        this.sndWinToCreditEnd.play();
        this.parent.onCashbackCompleted();
    }

    onCashbackWait()
    {
        this.parent.onCashbackWait();
    }

    onCashbackTaken()
    {
        this.parent.balance += Math.floor(this.parent.gameSocket.getCashbackWin()/this.parent.gameSocket.getUnits());//update balance
        this.parent.bar.animateBalance();
        this.sndWinToCredit.play();
        
        this.tweens.add({

            targets: this.sndCashbackWin,
            volume: 0,

            ease: 'Linear',
            duration: 500,

            onCompleteScope: this,
            onComplete: function() 
            {
                this.sndCashbackWin.stop();
                this.sndCashbackWin.setVolume(1.0);
            }                        
        });
        
        this.yoyoCashbackWinActionTween.stop();

        this.moveToHideCashAction();
        this.moveToHideBackAction();
        this.moveToHideAnimWinAction();
    }
}
