class TotalWin extends Phaser.Scene {

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

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

        this.vDelta = data.vDelta;

        this.sndWin = data.sndWin;

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

        this.bmLastWinTxt = this.add.bitmapText(162, 620 + this.vDelta, data.fontText, 'LAST WIN');
        this.bmLastWinTxt.letterSpacing = -17;
        this.bmLastWinTxt.setScale(0.60);

        this.showTime = 1500;//time to show total win
        this.hiddenTime = 13000;//time to hide total win

        this.sceneVisible = true;
        this.timer = 100;//this.showTime;
        this.scene.sleep();

        this.tweening = false;
        this.tweeningended = false;
        this.actionsComplete = false;
        this.forcesleep = false;
        this.tmultpl = 1;//total win conter time multiplier
        this.wbratio = 1;//win to bet ratio
        this.mp = 1;//prob obsolete, replace with wbratio??
        this.totalWinOnNextSpin = false;//show total win on next spin

        this.text = ['AMAZING', 'AWESOME', 'BRAVO', 'GREAT', 'FABULOUS', 'BIG', 'SUPER', 'EXCELLENT', 'MEGA', 'SUP' ];
        this.textStartingPositions = [{x: -1150, y: 243 + this.vDelta}, {x: 1280, y: 243 + this.vDelta}, {x: 55, y: -320 + this.vDelta}, {x: 0, y: -320 + this.vDelta}, {x: -1150, y: 243 + this.vDelta}, {x: -1050, y: 243 + this.vDelta}, {x: -1150, y: 243 + this.vDelta}, {x: -1150, y: 243 + this.vDelta}, {x: -1150, y: 243 + this.vDelta}, {x: -500, y: -320 + this.vDelta},];
        this.textEndingPositions =   [{x: 1280, y: 243 + this.vDelta}, {x: -1280, y: 243 + this.vDelta}, {x: 55, y: -320 + this.vDelta}, {x: 0, y: -320 + this.vDelta}, {x: -1150, y: 243 + this.vDelta}, {x: -1050, y: 243 + this.vDelta}, {x: -1150, y: 243 + this.vDelta}, {x: -1150, y: 243 + this.vDelta}, {x: -1150, y: 243 + this.vDelta}, {x: -500, y: -320 + this.vDelta},];
        this.textPositions =         [{x: 2, y: 243 + this.vDelta}, {x: 2, y: 243 + this.vDelta}, {x: 55, y: 243 + this.vDelta}, {x: 60+10, y: 243 + this.vDelta}, {x: 2, y: 243 + this.vDelta}, {x: 60, y: 243 + this.vDelta}, {x: 10+10, y: 243 + this.vDelta},{x: 2, y: 243 + this.vDelta}, {x: 10+10, y: 243 + this.vDelta}, {x: 13+10, y: 243 + this.vDelta}];
        this.textScale = [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ];
        this.textSpacings = [ -80, -100, -30, -30, -105, -70, -110, -113, -100, -50 ];
        this.bmTxt = this.add.bitmapText(0, 0 + this.vDelta, data.fontText, '');
        this.bmTxt.setVisible(false);
        this.textIndex = 0;

        this.textStartingPositionsDual = [{x: 1900, y: 243 + this.vDelta}, {x: 1500, y: 243 + this.vDelta}, {x: 1280, y: 243 + this.vDelta}, {x: 1180, y: -320 + this.vDelta} ];
        this.textEndingPositionsDual = [{x: 1900, y: 243 + this.vDelta}, {x: 1500, y: 243 + this.vDelta}, {x: 1280, y: 243 + this.vDelta}, {x: 1180, y: -320 + this.vDelta} ];
        this.textEndingPositionsDualShow = [{x: 670, y: 243 + this.vDelta}, {x: 770, y: 243 + this.vDelta}, {x: 745, y: 243 + this.vDelta}, {x: 632, y: 243 + this.vDelta} ];
        this.textScaleDual = [ 1.0, 1.0, 1.0, 1.0 ];
        this.textSpacingsDual = [ -70, -110, -100, -50 ];
        this.bmTxtDual = this.add.bitmapText(0, 0 + this.vDelta, data.fontText, 'WIN');
        this.bmTxtDual.setVisible(false);

        let starsConfig = {
            'scene': this,
            'key':'stars',
            'x': 630,
            'y': 320,
            'vDelta': this.vDelta,
        };

        this.stars = new Stars(starsConfig);

        this.animWinDY = 0;
        this.animWinScale = 1.05;

        this.baseX = 640;
        this.animWinX = this.baseX * this.animWinScale;
        this.animWinY = 260;
        
        this.animWin = new AnimatedAmount(this, this.animWinX, this.animWinY + this.vDelta, "totalwinNumbers", true);
        this.animWin.setScale(this.animWinScale);
        this.animWin.setOrigin(0.5);
        this.animWin.setSpacing(-65);
        this.realTotalWin = 0;
        this.totalWin = 0;

        this.firstTxtActionShowStarted = false;
        this.firstTxtActionShowCompleted = false;
        this.firstTxtActionHideStarted = false;
        this.firstTxtActionHideCompleted = false;

        this.secondTxtActionShowStarted = false;
        this.secondTxtActionShowCompleted = false;
        this.secondTxtActionHideStarted = false;
        this.secondTxtActionHideCompleted = false;

        this.thirdTxtActionShowStarted = false;
        this.thirdTxtActionShowCompleted = false;

        this.onUpdateExtraTextEnabled = false;

        this.ires = -1;
        this.repeat = 3;//default yoyo repeat animation
        this.yoyoTotalWinActionTween = null;
        this.animWinScaleCorrection = 1.0;

        this.actionTween = null;
        this.actionBackTween = null;

        this.actionSpacingTween = null;
        this.actionSpacingBackTween = null;

        this.actionDualTween = null;
        this.actionDualBackTween = null;
    }

    reset()
    {
        this.sceneVisible = true;
        this.timer = 100;
        
        this.realTotalWin = 0;
        this.totalWin = 0;

        this.tmultpl = 1;
        this.wbratio = 1;
        this.mp = 1;
        this.ires = -1;
        this.repeat = 3;

        this.totalWinOnNextSpin = false;

        this.tweening = false;
        this.tweeningended = false;
        this.actionsComplete = false;
        this.forcesleep = false;
        
        this.animWinDY = 0;

        this.animWin.reset(1);
        this.animWin.setPosition(this.animWinX, this.animWinY + this.vDelta);
        this.animWin.setVisible(false);
        this.animWin.setScale(this.animWinScale);
        
        this.textIndex = Math.floor(Math.random() * 6);        
        this.initBmTxt();

        this.bmTxt.setVisible(false);
        this.bmTxtDual.setVisible(false);
        this.bmLastWinTxt.setVisible(false);

        this.firstTxtActionShowStarted = false;
        this.firstTxtActionShowCompleted = false;
        this.firstTxtActionHideStarted = false;
        this.firstTxtActionHideCompleted = false;

        this.secondTxtActionShowStarted = false;
        this.secondTxtActionShowCompleted = false;
        this.secondTxtActionHideStarted = false;
        this.secondTxtActionHideCompleted = false;

        this.thirdTxtActionShowStarted = false;
        this.thirdTxtActionShowCompleted = false;

        this.onUpdateExtraTextEnabled = false;
        
        this.yoyoTotalWinActionTween = null;
        this.animWinScaleCorrection = 1.0;

        this.killTweens();
        this.scene.sleep();
    }

    setCounterSpeed(win)
    {
        this.realTotalWin = win;

        let u = win / this.parent.gameSocket.getUnits();
        let v = this.parent.gameSocket.getBet();
        
        this.wbratio = Math.floor( u /v);

        this.mp = 1;

        if(this.wbratio >= 10 && this.wbratio < 50 )
        {
            this.mp = 10;
        }
        else if(this.wbratio >= 50 && this.wbratio < 100 )
        {
            this.mp = 50;
        }
        else if (this.wbratio >= 100)
        {
            this.mp = 100;
        }

        this.tmultpl = (u + '').replace('.', '').length + 1;

        this.animWin.setSpeed(500 * this.tmultpl);
        this.animWin.setVisible(false);
        
        if(this.mp > 1)//move counter lower to make place for the text
        {
            this.animWinDY = 50;
            this.animWin.setPosition(this.animWin.x, this.animWin.y + this.animWinDY);
        }
    }

    setCounterPosition()
    {
        this.animWin.setPosition(this.animWinX, this.animWinY + this.animWinDY + this.vDelta);
        this.animWin.setOrigin(0.5);
    }

    onOrientationChange(offs, prevofs)
    {
        this.vDelta = offs;

        if(this.parent.gameState == GAME_STATE_WIN_LINES ||
           this.parent.gameState == GAME_STATE_READY ||
           this.parent.gameState == GAME_STATE_SPIN)
        {
            this.setCounterPosition();
        }
        else
        {
            this.animWin.setPosition(this.animWin.bmText.x, this.animWinY + this.animWinDY + this.vDelta);

            if(this.yoyoTotalWinActionTween != null && this.yoyoTotalWinActionTween.isPlaying())
            {
                this.yoyoTotalWinActionTween = this.createYoyoTween(this.yoyoTotalWinActionTween);
            }
        }

        this.bmLastWinTxt.setPosition(162, 620 + this.vDelta);
        this.bmTxt.setPosition(this.bmTxt.x, this.bmTxt.y + prevofs);
        this.bmTxtDual.setPosition(this.bmTxtDual.x, this.bmTxtDual.y + prevofs);

        this.textStartingPositions.forEach(a => { a.y += prevofs});
        this.textEndingPositions.forEach(a => { a.y += prevofs});
        this.textPositions.forEach(a => { a.y += prevofs});

        this.textStartingPositionsDual.forEach(a => { a.y += prevofs});
        this.textEndingPositionsDual.forEach(a => { a.y += prevofs});
        this.textEndingPositionsDualShow.forEach(a => { a.y += prevofs});

        //congratualation texts

        if(this.actionTween != null && this.actionTween.isPlaying())
        {
            this.actionTween = this.updateActionTween(this.actionTween, this.bmTxt, this.textPositions[this.textIndex].x, this.textPositions[this.textIndex].y);
        }

        if(this.actionBackTween != null && this.actionBackTween.isPlaying())
        {
            this.actionBackTween = this.updateActionBackTween(this.actionBackTween, this.bmTxt, this.textEndingPositions[this.textIndex].x, this.textEndingPositions[this.textIndex].y);
        }

        if(this.actionSpacingTween != null && this.actionSpacingTween.isPlaying())
        {
            this.actionSpacingTween = this.updateActionSpacingTween(this.actionSpacingTween, this.bmTxt, this.textPositions[this.textIndex].x, this.textPositions[this.textIndex].y, this.textSpacings[this.textIndex]);
        }

        if(this.actionSpacingBackTween != null && this.actionSpacingBackTween.isPlaying())
        {
            this.actionSpacingBackTween = this.updateActionSpacingBackTween(this.actionSpacingBackTween, this.bmTxt, this.textEndingPositions[this.textIndex].x, this.textEndingPositions[this.textIndex].y, 5);
        }

        if(this.actionDualTween != null && this.actionDualTween.isPlaying())
        {
            this.actionDualTween = this.updateActionDualTween(this.actionDualTween, this.bmTxtDual);
        }

        if(this.actionDualBackTween != null && this.actionDualBackTween.isPlaying())
        {
            this.actionDualBackTween = this.updateActionDualBackTween(this.actionDualBackTween, this.bmTxtDual);
        }
    }

    killTweens()
    {
        this.tweens.killTweensOf(this.animWin.bmText);
        this.tweens.killTweensOf(this.bmLastWinTxt);
        this.tweens.killTweensOf(this.sndWinToCredit);
        this.tweens.killTweensOf(this.bmTxt);
        this.tweens.killTweensOf(this.bmTxtDual);
    }
    
    update(time, delta) 
    {
        if (this.parent.gameState == GAME_STATE_NOT_INITIALIZED ||
            this.parent.gameState == GAME_STATE_CASHBACK_WIN)
        {
            return;
        }

        this.timer -= delta;
        
        if(this.timer < 0 && !this.totalWinOnNextSpin)
        {
            if(!this.tweening && !this.tweeningended)
            {
                if(!this.forcesleep)
                {
                    this.totalWin = this.realTotalWin / this.parent.gameSocket.getUnits();
                }

                this.tweening = true;
                this.actionsComplete = false;

                if(this.mp == 1)
                {
                    this.positionAnimWinAction();
                }
                else
                {
                    this.scaleAnimWinAction();
                }                    
                
                return;
            }                

            if(this.tweeningended && !this.actionsComplete)
            {
                if(this.sndWinToCredit.isPlaying)
                {
                    return;
                }
                else
                {
                    this.endActions();
                    return;
                }
            }                

            if(!this.tweening)
            {
                if(this.sceneVisible)
                {
                    this.tweening = true;
                    this.fadeOutTotalWinAction();
                    this.fadeOutLastWinAction();
                }
                else
                {
                    this.sceneVisible = true;
                    this.scene.bringToTop();
                    this.parent.startBtn.scene.bringToTop();
                    this.parent.betBtn.scene.bringToTop();
                    this.parent.infoBtn.scene.bringToTop();
            
                    this.timer = this.showTime;
                    
                    if(this.mp > 1 && !this.bmTxt.visible)
                    {
                        switch(this.textIndex)
                        {
                            case 0:
                            case 1:
                            case 2:
                            case 3:
                            case 4:{this.bmTxt.setVisible(true);};break;
                            case 5:{this.bmTxt.setVisible(true);this.bmTxtDual.setVisible(true);};break;
                            case 6:{this.bmTxt.setVisible(true);this.bmTxtDual.setVisible(true);};break;
                            case 7:{this.bmTxt.setVisible(true)};break;
                            case 8:{this.bmTxt.setVisible(true);this.bmTxtDual.setVisible(true);};break;
                            case 9:{this.bmTxt.setVisible(true);this.bmTxtDual.setVisible(true);};break;
                        }
                    }

                    this.animWin.setVisible(this.sceneVisible);
                    this.bmLastWinTxt.setVisible(this.sceneVisible);
                }
            }                
        }
        
        this.animWin.update(delta, this.totalWin);
        this.onUpdateExtraText();
    }

    onUpdateExtraText()
    {
        if(!this.onUpdateExtraTextEnabled)
        {
            return;
        }

        if(this.mp > 1)
        {
            if(!this.firstTxtActionShowStarted && this.animWin.value() >= (this.parent.gameSocket.getBet() * /*10*/7))
            {
                this.firstTxtActionShowStarted = true;

                switch(this.textIndex)
                {
                    case 0:
                    case 1:
                    case 2:{this.moveAction();};break;
                    case 3:{this.bmTxt.letterSpacing = 5; this.moveActionWithSpacing();};break;
                    case 4:{this.moveAction();};break;
                    case 5:{this.moveAction();this.moveActionDual();};break;
                };
            }
            else if(this.firstTxtActionShowCompleted && !this.firstTxtActionHideStarted && this.mp >= 50 && this.animWin.value() >= (this.parent.gameSocket.getBet() * /*50*/40))
            {
                this.firstTxtActionHideStarted = true;

                switch(this.textIndex)
                {
                    case 0:
                    case 1:
                    case 2:{this.moveBackAction();};break;
                    case 3:{this.moveBackActionWithSpacing();};break;
                    case 4:{this.moveBackAction();};break;
                    case 5:{this.moveBackAction();this.moveBackActionDual();};break;
                };
            }
            else if(!this.secondTxtActionShowStarted && this.firstTxtActionHideCompleted && this.mp >= 50 && this.animWin.value() >= (this.parent.gameSocket.getBet() * /*50*/40))
            {
                this.secondTxtActionShowStarted = true;

                this.textIndex = 6 + this.animWin.value() % 2;
                this.initBmTxt();

                switch(this.textIndex)
                {
                    case 6:{this.moveAction();this.moveActionDual();};break;
                    case 7:{this.moveAction();};break;
                };
            }
            else if(this.secondTxtActionShowCompleted && !this.secondTxtActionHideStarted && this.mp == 100 && this.animWin.value() >= (this.parent.gameSocket.getBet() * /*100*/70))
            {
                this.secondTxtActionHideStarted = true;

                switch(this.textIndex)
                {
                    case 6:{this.moveBackAction();this.moveBackActionDual();};break;
                    case 7:{this.moveBackAction();};break;
                };
            }
            else if(!this.thirdTxtActionShowStarted && this.secondTxtActionHideCompleted && this.animWin.value() >= (this.parent.gameSocket.getBet() * /*100*/70) )
            {
                this.thirdTxtActionShowStarted = true;

                this.textIndex = 8 + this.animWin.value() % 2;

                this.initBmTxt();

                switch(this.textIndex)
                {
                    case 8:{this.moveAction();this.moveActionDual();};break;
                    case 9:{this.moveAction();this.moveActionDual();};break;
                };
            }
        }
    }

    initBmTxt()
    {
        this.bmTxt.setText(this.text[this.textIndex]);
        this.bmTxt.x = this.textStartingPositions[this.textIndex].x;
        this.bmTxt.y = this.textStartingPositions[this.textIndex].y;
        this.bmTxt.setScale(this.textScale[this.textIndex]);
        this.bmTxt.letterSpacing = this.textSpacings[this.textIndex];

        let index = 0;

        switch (this.textIndex)
        {
            case 5:{index = 0;this.bmTxtDual.setText('WIN')}; break;
            case 6:{index = 1;this.bmTxtDual.setText('WIN')}; break;
            case 8:{index = 2;this.bmTxtDual.setText('WIN')}; break;
            case 9:{index = 3;this.bmTxtDual.setText('ERB')}; break;
        }

        this.bmTxtDual.x = this.textStartingPositionsDual[index].x;
        this.bmTxtDual.y = this.textStartingPositionsDual[index].y;
        this.bmTxtDual.setScale(this.textScaleDual[index]);
        this.bmTxtDual.letterSpacing = this.textSpacingsDual[index];
    }

    forceSetBmTxt()
    {
        if(this.mp == 50)
        {
            if(this.textIndex < 6)
            {
                this.textIndex = 6 +  Math.floor(Math.random() * 2) % 2;
            }                
        }
        else if (this.mp == 100)
        {
            if(this.textIndex < 8)
            {
                this.textIndex = 8 + Math.floor(Math.random() * 2) % 2;
            }                
        }

        if(this.mp > 1)
        {
            this.bmTxt.setText(this.text[this.textIndex]);
            this.bmTxt.x = this.textPositions[this.textIndex].x;
            this.bmTxt.y = this.textPositions[this.textIndex].y;
            this.bmTxt.setScale(this.textScale[this.textIndex]);
            this.bmTxt.letterSpacing = this.textSpacings[this.textIndex];
            this.bmTxt.setOrigin(0);
            this.bmTxt.setVisible(true);
        }    

        let index = 0;
        this.bmTxtDual.setVisible(false);

        switch (this.textIndex)
        {
            case 5:{index = 0;this.bmTxtDual.setText('WIN')}; break;
            case 6:{index = 1;this.bmTxtDual.setText('WIN')}; break;
            case 8:{index = 2;this.bmTxtDual.setText('WIN')}; break;
            case 9:{index = 3;this.bmTxtDual.setText('ERB')}; break;
        }

        if((this.textIndex == 5 || this.textIndex == 6 || this.textIndex == 8 || this.textIndex == 9) && this.mp > 1)
        {
            this.bmTxtDual.x = this.textEndingPositionsDualShow[index].x;
            this.bmTxtDual.y = this.textEndingPositionsDualShow[index].y;
            this.bmTxtDual.setScale(this.textScaleDual[index]);
            this.bmTxtDual.letterSpacing = this.textSpacingsDual[index];
            this.bmTxtDual.setVisible(true);
        }            
    }

    onShowActionComplete()
    {
        switch(this.textIndex)
        {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:{this.firstTxtActionShowCompleted = true;};break;
            case 6:
            case 7:{this.secondTxtActionShowCompleted = true;};break;
            case 8:
            case 9:{this.thirdTxtActionShowCompleted = true;};break;
        }            
    }

    onHideActionComplete()
    {
        switch(this.textIndex)
        {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:{this.firstTxtActionHideCompleted = true;};break;
            case 6:
            case 7:{this.secondTxtActionHideCompleted = true;};break;
            case 8:
            case 9:
        }            
    }

    //action tweens

    updateActionTween(tween, target, targetx, targety)
    {
        tween.stop();

        let duration = tween.totalDuration - tween.totalElapsed;

        tween = this.tweens.add({

            targets: target,
            ease: 'Linear',
            duration: duration,
    
            x: targetx,
            y: targety,

            onCompleteParams:[target],
            onCompleteScope: this,
            onComplete: function() 
            {
                setTimeout(this.onShowActionComplete.bind(this), 500);
            },
        });

        return tween;
    }

    updateActionBackTween(tween, target, targetx, targety)
    {
        tween.stop();

        let duration = tween.totalDuration - tween.totalElapsed;

        tween = this.tweens.add({

            targets: target,
            ease: 'Linear',
            duration: duration,
            
            x: targetx,
            y: targety,

            onCompleteParams:[target],
            onCompleteScope: this,
            onComplete: function() 
            {
                target.setVisible(false);
                target.x = this.textPositions[this.textIndex].x;
                target.y = this.textPositions[this.textIndex].y;
                
                this.onHideActionComplete();
            },
        });

        return tween;
    }

    updateActionSpacingTween(tween, target, targetx, targety, lsp)
    {
        tween.stop();

        let duration = tween.totalDuration - tween.totalElapsed;

        tween = this.tweens.add({

            targets: target,
            ease: 'Linear',
            duration: duration,
    
            x: targetx,
            y: targety,
            letterSpacing: lsp,

            onCompleteParams:[target],
            onCompleteScope: this,
            onComplete: function() 
            {
                setTimeout(this.onShowActionComplete.bind(this), 500);
            },
        });

        return tween;
    }

    updateActionSpacingBackTween(tween, target, targetx, targety, lsp)
    {
        tween.stop();

        let duration = tween.totalDuration - tween.totalElapsed;

        tween = this.tweens.add({

            targets: target,
            ease: 'Linear',
            duration: duration,
            
            x: targetx,
            y: targety,
            letterSpacing: lsp,

            onCompleteParams:[target],
            onCompleteScope: this,
            onComplete: function() 
            {
                target.setVisible(false);
                target.x = this.textPositions[this.textIndex].x;
                target.y = this.textPositions[this.textIndex].y;
                target.letterSpacing = this.textSpacings[this.textIndex];
                
                this.onHideActionComplete();
            },
        });

        return tween;
    }

    updateActionDualTween(tween, target)
    {
        tween.stop();

        let duration = tween.totalDuration - tween.totalElapsed;

        let index = 0;
    
        switch (this.textIndex)
        {
            case 5:index = 0;break;
            case 6:index = 1;break;
            case 8:index = 2; break;
            case 9:index = 3; break;
        }
    
        tween = this.tweens.add({

            targets: target,
            ease: 'Linear',
            duration: duration,
    
            x: this.textEndingPositionsDualShow[index].x,
            y: this.textEndingPositionsDualShow[index].y,
            
            onCompleteParams:[target],
            onCompleteScope: this,
            onComplete: function() 
            {
                setTimeout(this.onShowActionComplete.bind(this), 500);
            },
        });

        return tween;
    }

    updateActionDualBackTween(tween, target)
    {
        tween.stop();

        let duration = tween.totalDuration - tween.totalElapsed;

        let index = 0;

        switch (this.textIndex)
        {
            case 5:index = 0;break;
            case 6:index = 1;break;
            case 8:index = 2; break;
            case 9:index = 3; break; 
        }

        tween = this.tweens.add({

            targets: target,
            ease: 'Linear',
            duration: duration,
            
            x: this.textEndingPositionsDual[index].x,
            y: this.textEndingPositionsDual[index].y,
    
            onCompleteParams:[target, index],
            onCompleteScope: this,
            onComplete: function() 
            {
                target.setVisible(false);

                target.x = this.textEndingPositionsDualShow[index].x;
                target.y = this.textEndingPositionsDualShow[index].y;

                this.onHideActionComplete();
            },
        });

        return tween;
    }

    //actions ...

    moveActionDual()//show extra text
    {
        let index = 0;
        this.bmTxtDual.setText('WIN');

        switch (this.textIndex)
        {
            case 5:index = 0;break;
            case 6:index = 1;break;
            case 8:index = 2; break;
            case 9:{index = 3;this.bmTxtDual.setText('ERB')}; break;
        }

        this.bmTxtDual.x = this.textStartingPositionsDual[index].x;
        this.bmTxtDual.y = this.textStartingPositionsDual[index].y;
        this.bmTxtDual.setScale(this.textScaleDual[index]);
        this.bmTxtDual.letterSpacing = this.textSpacingsDual[index];
        
        this.bmTxtDual.setVisible(true);

        this.actionDualTween = this.tweens.add({

            targets: this.bmTxtDual,
            ease: 'Linear',
            duration: 250,

            x: this.textEndingPositionsDualShow[index].x,
            y: this.textEndingPositionsDualShow[index].y,
            
            onCompleteParams: [index],
            onCompleteScope: this,
            onComplete: function() 
            {                
            },
        });
    }

    moveBackActionDual()//hide extra text
    {
        let index = 0;

        switch (this.textIndex)
        {
            case 5:index = 0;break;
            case 6:index = 1;break;
            case 8:index = 2; break;
            case 9:index = 3; break; 
        }

        this.actionDualBackTween = this.tweens.add({

            targets: this.bmTxtDual,
            ease: 'Linear',
            duration: 250,
            x: this.textEndingPositionsDual[index].x,
            y: this.textEndingPositionsDual[index].y,

            onCompleteParams: [index],
            onCompleteScope: this,
            onComplete: function() 
            {                
                this.bmTxtDual.setVisible(false);
        
                this.bmTxtDual.x = this.textEndingPositionsDualShow[index].x;
                this.bmTxtDual.y = this.textEndingPositionsDualShow[index].y;
            },
        });
    }

    moveAction()
    {
        this.bmTxt.setVisible(true);
        this.actionTween = this.tweens.add({

            targets: this.bmTxt,
            ease: 'Linear',
            duration: 250,
            x: this.textPositions[this.textIndex].x,
            y: this.textPositions[this.textIndex].y,

            onCompleteScope: this,
            onComplete: function() 
            {
                setTimeout(this.onShowActionComplete.bind(this), 500);
            },
        });
    }

    moveBackAction()
    {
        this.actionBackTween = this.tweens.add({

            targets: this.bmTxt,
            ease: 'Linear',
            duration: 250,
            x: this.textEndingPositions[this.textIndex].x,
            y: this.textEndingPositions[this.textIndex].y,            

            onCompleteScope: this,
            onComplete: function() 
            {
                this.bmTxt.setVisible(false);
                this.bmTxt.x = this.textPositions[this.textIndex].x;
                this.bmTxt.y = this.textPositions[this.textIndex].y;
                this.onHideActionComplete();
            },
        });
    }

    moveActionWithSpacing()
    {
        this.bmTxt.setVisible(true);
        this.actionSpacingTween = this.tweens.add({

            targets: this.bmTxt,
            ease: 'Linear',
            duration: 250,
            x: this.textPositions[this.textIndex].x,
            y: this.textPositions[this.textIndex].y,
            letterSpacing: this.textSpacings[this.textIndex],

            onCompleteScope: this,
            onComplete: function() 
            {
                setTimeout(this.onShowActionComplete.bind(this), 500);
            },
        });
    }

    moveBackActionWithSpacing()
    {
        this.actionSpacingBackTween = this.tweens.add({

            targets: this.bmTxt,
            ease: 'Linear',
            duration: 250,
            x: this.textEndingPositions[this.textIndex].x,
            y: this.textEndingPositions[this.textIndex].y,
            letterSpacing: 5,

            onCompleteScope: this,
            onComplete: function() 
            {
                this.bmTxt.setVisible(false);
                this.bmTxt.letterSpacing = this.textSpacings[this.textIndex];
                this.bmTxt.x = this.textPositions[this.textIndex].x;
                this.bmTxt.y = this.textPositions[this.textIndex].y;
                this.onHideActionComplete();
            },
        });
    }
    
    positionAnimWinAction()//center & show total win counter for small wins
    {
        this.animWin.setVisible(true);
        
        if(this.totalWin > 9)//count from 1 to total win
        {
            this.tweens.add({

                targets: this.animWin,
                ease: 'Linear',
                duration: 500 * this.tmultpl,
                timer: 500 * this.tmultpl,

                onCompleteScope: this,
                onComplete: function() 
                {
                    this.tweens.killTweensOf(this.animWin);
                    this.yoyoTotalWinAction();
                },
            });
        }
        else//very small wins (< 9)
        {
            this.animWin.reset(this.totalWin);
            this.yoyoTotalWinAction();
        }            
    }

    scaleAnimWinAction()//center and scale up bigger wins, with alpha changing from 0.2 to 0.95
    {
        this.animWin.reset(this.totalWin);
        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.reset(1);
        this.animWin.setScale(0.2);
        this.animWin.setAlpha(0.2);
        this.animWin.setVisible(true);
        
        this.onUpdateExtraTextEnabled = true;

        this.tweens.add({

            targets: this.animWin.bmText,
            ease: 'Linear',
            duration: 500 * this.tmultpl,

            scale: this.animWinScale * this.animWinScaleCorrection,
            alpha: 1.0,

            onCompleteScope: this,
            onComplete: function() 
            {
                if(this.ires < 0)
                {
                    this.ires = setInterval(this.onUpdateExtraTextCompleted.bind(this), 20);
                }
            },
        });
    }

    onUpdateExtraTextCompleted()
    {
        if((this.mp == 10 && this.firstTxtActionShowCompleted) || (this.mp == 50 && this.secondTxtActionShowCompleted) || (this.mp == 100 && this.thirdTxtActionShowCompleted))
        {
            clearInterval(this.ires);
            this.ires = -1;
            this.onUpdateExtraTextEnabled = false;
            this.animWin.bmText.y = this.animWinY + this.animWinDY  + this.vDelta;
            this.yoyoTotalWinAction();
        }
        else if (this.mp == 1)
        {
            if(this.ires > 0)
            {
                clearInterval(this.ires);
                this.ires = -1;        
            }                
            this.yoyoTotalWinAction();
        }
    }

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

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

        if(this.mp > 1)
        {
           this.stars.setPosition(640, this.animWin.bmText.y);
          
           if(!this.stars.isPlaying())
           {
                this.stars.start();
           }
        }

        tween = this.tweens.add({

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

        return tween;
    }

    onYoyoTweenComplete()
    {
        this.tweens.add({
    
            targets: this.sndWin,
            volume: 0,
    
            ease: 'Linear',
            duration: 500,

            onCompleteScope: this,
            onComplete: function() 
            {
                this.sndWin.stop();
                this.sndWin.setVolume(1.0);    
            }                        
            });
        
        if(this.stars.isPlaying())
        {
            this.stars.stop();
        }            
        
        this.moveTotalWinAction();

        switch(this.textIndex)
        {
            case 0:
            case 1:
            case 2:{this.moveBackAction();};break;
            case 3:{this.moveBackActionWithSpacing();};break;
            case 4:{this.moveBackAction();};break;
            case 5:{this.moveBackAction();this.moveBackActionDual();};break;
            case 6:{this.moveBackAction();this.moveBackActionDual();};break;
            case 7:{this.moveBackAction();};break;
            case 8:{this.moveBackAction();this.moveBackActionDual();};break;
            case 9:{this.moveBackAction();this.moveBackActionDual();};break;
        }
    }

    yoyoTotalWinAction()//animate total win pulsing
    {
        if(this.forcesleep)
        {
            return;
        }
        else
        {
            this.yoyoTotalWinActionTween = this.createYoyoTween(this.yoyoTotalWinActionTween);
        }            
    }

    moveTotalWinAction()//move to hide total win counter, add total win to balance
    {
        this.sndWinToCredit.play();

        this.parent.balance += Math.floor(this.totalWin);//update balance
        this.parent.bar.animateBalance();

        this.tweens.add({

            targets: this.animWin.bmText,
            ease: 'Linear',
            duration: 500,
    
            x: 225,
            y: 570 + this.vDelta,
            alpha: 0.0,

            onCompleteScope: this,
            onComplete: function() 
            {
                this.tweens.add({
    
                    targets: this.sndWinToCredit,
                    volume: 0,
            
                    ease: 'Linear',
                    duration: 500,
    
                    onCompleteScope: this,
                    onComplete: function() 
                    {
                        this.sndWinToCredit.stop();
                        this.sndWinToCredit.setVolume(1.0);
                    }                        
                    });

                this.tweeningended = true;
            },
        });
    }

    fadeOutTotalWinAction()//hide total win counter / every 30 seconds 
    {
        this.tweens.killTweensOf(this.animWin.bmText);

        this.tweens.add({

            targets: this.animWin.bmText,
            ease: 'Linear',
            duration: 200,
            alpha: 0.0,

            onCompleteScope: this,
            onComplete: function() 
            {
                this.sceneVisible = false;
                this.timer = this.hiddenTime;
                this.animWin.setAlpha(1.0);
                this.tweening = false;
                this.bmTxt.setVisible(false);
                this.bmTxtDual.setVisible(false);

                this.animWin.setVisible(this.sceneVisible);
                this.bmLastWinTxt.setVisible(this.sceneVisible);
            },
        });
    }

    fadeOutLastWinAction()//hide last win message, every 30 seconds
    {
        this.tweens.killTweensOf(this.bmLastWinTxt);
        
        this.tweens.add({

            targets: this.bmLastWinTxt,
            ease: 'Linear',
            duration: 200,
            alpha: 0.0,

            onCompleteScope: this,
            onComplete: function() 
            {
                this.bmLastWinTxt.setAlpha(1.0);
            },
        });
    }

    endActions()
    {
        this.tweening = false;
        this.actionsComplete = true;;
        this.sndWinToCreditEnd.play();
        this.timer = this.hiddenTime;
        this.sceneVisible = false;
        
        this.setCounterPosition();

        this.animWin.bmText.setAlpha(1.0);
        this.animWin.setVisible(this.sceneVisible);
        this.animWin.setScale(this.animWinScale * this.animWinScaleCorrection);
        this.bmLastWinTxt.setVisible(this.sceneVisible);

        if(this.forcesleep)
        {
            this.scene.sleep();
        }
        
        this.parent.onWin();
    }

    forceSleep(totalWin)
    {
        this.realTotalWin = totalWin;
        this.totalWin = totalWin / this.parent.gameSocket.getUnits();

        if(this.totalWin > 0)
        {
            if(this.actionsComplete)
            {
                this.scene.sleep();
                return;
            }
            else
            {//display total win during next spin
                
                let yoyoIsPplaying = false;
                
                if(this.yoyoTotalWinActionTween != null)
                {
                    yoyoIsPplaying = this.yoyoTotalWinActionTween.isPlaying();//if true, a special case to not change total win counter when in yoyo state and start button is pressed
                }

                this.killTweens();
                this.setCounterSpeed(totalWin);//
                
                if(!yoyoIsPplaying)
                {
                    this.animWin.reset(this.totalWin);
                    this.animWin.bmText.setAlpha(1.0);
                }

                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.setCounterPosition();

                this.animWin.setVisible(true);
                this.stars.stop();
                this.totalWinOnNextSpin = true;
                this.forcesleep = true;
                this.sndWin.stop();
                this.sndWinToCredit.stop();
                this.sndWinToCreditEnd.play();
                this.forceSetBmTxt();
            }
        }
    }

    forceClose()
    {
        this.killTweens();

        this.tweening = false;
        this.actionsComplete = true;;
        this.timer = this.hiddenTime;
        this.sceneVisible = false;
        this.totalWinOnNextSpin = false;
       
        this.animWin.setPosition(this.animWinX, this.animWinY + this.vDelta);
        this.animWin.bmText.setAlpha(1.0);
        this.animWin.setVisible(this.sceneVisible);
        this.bmLastWinTxt.setVisible(this.sceneVisible);
        this.sndWin.stop();
        this.scene.sleep();
    }
}
