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

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

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

        this.lastWinMsg = this.add.bitmapText(0, 0, "totalwin", "LAST WIN");
        this.lastWinMsg.setScale(0.65).setLetterSpacing(-17).setVisible(false);

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

        this.winVisible = true;
        this.timer = 100;//this.showTime;

        this.playInProgress = false;
        this.playCompleted = false;

        this.forcesleep = false;
        this.counterSpeed = 1;
        this.winToBet = 1;
        this.showWinOnNextSpin = false;

        this.msg = ["AMAZING", "AWESOME", "BRAVO", "GREAT", "FABULOUS", "BIG", "SUPER", "EXCELLENT", "MEGA", "SUP", "BONUS" ];
        this.msgStartingPositions = [{x: -1150, y: 903}, {x: 1280, y: 903}, {x: 95, y: 0}, {x: 0, y: 0}, {x: -1150, y: 903}, {x: -1050, y: 903}, {x: -1150, y: 903}, {x: -1150, y: 903}, {x: -1150, y: 903}, {x: -500, y: 0}, {x: -1150, y: 705}];
        this.msgEndingPositions =   [{x: 1280, y: 903}, {x: -1280, y: 903}, {x: 95, y: 0}, {x: 0, y: 0}, {x: -1150, y: 903}, {x: -1050, y: 903}, {x: -1150, y: 903}, {x: -1150, y: 903}, {x: -1150, y: 903}, {x: -500, y: 0}, {x: -1150, y: 705}];
        this.msgPositions =         [{x: 12, y: 903}, {x: 2, y: 903}, {x: 95, y: 903}, {x: 100, y: 903}, {x: -12, y: 903}, {x: 90, y: 903}, {x: 20, y: 903},{x: 2, y: 903}, {x: 50, y: 903}, {x: 100, y: 903}, {x: 160, y: 705}];
        this.msgScale = [ 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1 ];
        this.msgSpacings = [ -3, -10, 25, 15, -25, 0, -45, -43, -30, -20, -6 ];
        this.winMsg = this.add.bitmapText(0, 0, "totalwin", "");
        this.winMsg.setVisible(false);

        this.msgStartingPositionsRight = [{x: 1900, y: 903}, {x: 1500, y: 903}, {x: 1280, y: 903}, {x: 1180, y: 0}, {x: 1180, y: 903} ];
        this.msgEndingPositionsRight = [{x: 1900, y: 903}, {x: 1500, y: 903}, {x: 1280, y: 903}, {x: 1180, y: 0}, {x: 1180, y: 903} ];
        this.msgPositionsRight = [{x: 570, y: 903}, {x: 650, y: 903}, {x: 650, y: 903}, {x: 532, y: 903}, {x: 330, y: 903} ];
        this.msgScaleRight = [ 1.1, 1.1, 1.1, 1.1, 1.1 ];
        this.msgSpacingsRight = [ 0, -20, -10, -20, 0 ];
        this.winMsgR = this.add.bitmapText(0, 0, "totalwin", "WIN");
        this.winMsgR.setVisible(false);

        let starsConfig = {
            "scene": this,
            "key":"stars",
            "x": 540,
            "y": 1000
        };

        this.stars = new Stars(starsConfig);

        this.winCounterExtraY = 0;
        this.winCounterScale = 1.10;

        this.winCounterX = 540;
        this.winCounterY = 1080;
        
        this.winCounter = new AnimatedAmount(this, this.winCounterX, this.winCounterY, "totalwin", false);
        this.winCounter.setScale(this.winCounterScale).setOrigin(0.5).setSpacing(-30);
        this.winCounter.eventHandler = this.updateWinMsgCompleted.bind(this);

        this.win = 0;
        this.bigWinThreshold = 1;

        this.firstMsgDisplayStarted = false;
        this.firstMsgDisplayCompleted = false;
        this.firstMsgHideStarted = false;
        this.firstMsgHideCompleted = false;

        this.secondMsgDisplayStarted = false;
        this.secondMsgDisplayCompleted = false;
        this.secondMsgHideStarted = false;
        this.secondMsgHideCompleted = false;

        this.thirdMsgDisplayStarted = false;
        this.thirdMsgDisplayCompleted = false;

        this.bonusWnMsgDisplayStarted = false;

        this.updateWinMsg = false;

        this.isBonusWin = false;
        
        this.repeat = 1;//default yoyo repeat animation

        this.winCounterScaleCorrection = 1.0;

        this.resetPending = true;

        this.duration = 250;

        this.sleep();
    }

    reset ()
    {
        this.winVisible = true;
        this.timer = 100;
        
        this.win = 0;

        this.counterSpeed = 1;
        this.winToBet = 1;
        this.bigWinThreshold = 1;

        this.repeat = 1;

        this.showWinOnNextSpin = false;

        this.playInProgress = false;
        this.playCompleted = false;

        this.forcesleep = false;
        
        this.winCounterExtraY = 0;

        this.winCounter.reset(1).setPosition(this.winCounterX, this.winCounterY).setVisible(false).setScale(this.winCounterScale);
        
        this.winMsgPos = Math.floor(Math.random() * 6);
        this.initWinMsg();

        this.winMsg.setVisible(false);
        this.winMsgR.setVisible(false);
        this.lastWinMsg.setVisible(false).setPosition(250, 1200);

        this.firstMsgDisplayStarted = false;
        this.firstMsgDisplayCompleted = false;
        this.firstMsgHideStarted = false;
        this.firstMsgHideCompleted = false;

        this.secondMsgDisplayStarted = false;
        this.secondMsgDisplayCompleted = false;
        this.secondMsgHideStarted = false;
        this.secondMsgHideCompleted = false;

        this.thirdMsgDisplayStarted = false;
        this.thirdMsgDisplayCompleted = false;
        this.bonusWnMsgDisplayStarted = false;

        this.updateWinMsg = false;

        this.isBonusWin = false;

        this.winCounterScaleCorrection = 1.0;

        this.killTweens();
        this.sleep();

        this.resetPending = false;
    }

    sleep ()
    {
        if (this.scene.isSleeping())
        {
            return;
        }

        this.scene.sleep();
    }

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

    setWinCounter (win)
    {
        this.win = win;

        this.winToBet = this.getWinToBet();

        if (this.winToBet > 1)
        {
            this.winCounterExtraY = 50;
        }

        this.winCounter.reset(this.win);
        this.winCounter.fireEvent(false);
    }

    setWin (win, forcedCashoutWin, bonusWin)
    {
        this.win = win;

        this.isBonusWin = bonusWin;

        if (this.isBonusWin)
        {
            this.winMsgPos = 10;
            this.initWinMsg();

            this.winToBet = 10;//force to show bonus message
        }
        else
        {
            this.winToBet = this.getWinToBet();
        }

        if (this.winToBet < 100)
        {
            this.counterSpeed = ((this.win / this.parentScene.gameSocket.getUnits() + "").replace(".", "").length + 1) * 400;
            this.duration = 200;
        }
        else
        {
            this.counterSpeed = ((this.win / this.parentScene.gameSocket.getUnits() + "").replace(".", "").length + 1) * 500;
            this.duration = 250;
        }

        this.winCounter.setSpeed(this.counterSpeed);
        this.winCounter.setVisible(false);
        
        if (this.winToBet > 1)//move counter lower to make place for the text
        {
            this.winCounterExtraY = 50;
            this.winCounter.setPosition(this.winCounter.x, this.winCounter.y + this.winCounterExtraY);
        }
    }

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

    getWinToBet ()
    {
        let u = this.win;
        let v = this.parentScene.gameSocket.getBet();
        
        let w = Math.floor( u / v);

        this.bigWinThreshold = w;

        let wb = 1;

        if (w >= 10 && w < 50 )
        {
            wb = 10;
        }
        else if (w >= 50 && w < 100 )
        {
            wb = 50;
        }
        else if (w >= 100)
        {
            wb = 100;
        }

        return wb;
    }

    killTweens ()
    {
        this.tweens.killTweensOf(this.winCounter);
        this.tweens.killTweensOf(this.winCounter.bmText);
        this.tweens.killTweensOf(this.lastWinMsg);
        this.tweens.killTweensOf(this.sndWinToCredit);
        this.tweens.killTweensOf(this.winMsg);
        this.tweens.killTweensOf(this.winMsgR);
    }
    
    update (time, delta) 
    {
        if (this.parentScene.gameState == GAME_STATE_NOT_INITIALIZED)
        {
            return;
        }

        this.timer -= delta;
        
        if (this.timer < 0 && !this.showWinOnNextSpin)
        {
            if (!this.playInProgress && !this.playCompleted)
            {
                this.playInProgress = true;

                if (this.winToBet == 1)
                {
                    this.showWinCounter();
                }
                else
                {
                    this.scaleWinCounter();
                }                    
                
                return;
            }                

            if (!this.playInProgress && this.playCompleted)
            {
                if (this.winVisible)
                {
                    this.playInProgress = true;
                    
                    this.fadeOutWin();
                    this.fadeOutWinMsg();
                }
                else
                {
                    this.winVisible = true;
                    this.scene.bringToTop();
            
                    this.timer = this.showTime;
                    
                    if ((this.winToBet > 1 || this.isBonusWin) && !this.winMsg.visible)
                    {
                        switch (this.winMsgPos)
                        {
                            case 0:
                            case 1:
                            case 2:
                            case 3:
                            case 4:{this.winMsg.setVisible(true);};break;
                            case 5:{this.winMsg.setVisible(true);this.winMsgR.setVisible(true);};break;
                            case 6:{this.winMsg.setVisible(true);this.winMsgR.setVisible(true);};break;
                            case 7:{this.winMsg.setVisible(true)};break;
                            case 8:{this.winMsg.setVisible(true);this.winMsgR.setVisible(true);};break;
                            case 9:{this.winMsg.setVisible(true);this.winMsgR.setVisible(true);};break;
                            case 10:{this.winMsg.setVisible(true);this.winMsgR.setVisible(true);};break;
                        }
                    }

                    this.winCounter.setVisible(this.winVisible);
                    this.lastWinMsg.setVisible(this.winVisible);
                }
            }                
        }
        
        this.winCounter.update(delta, this.win);
        this.updateWinMsgs();
    }

    updateWinMsgs ()
    {
        if (!this.updateWinMsg)
        {
            return;
        }
        
        if (this.isBonusWin)
        {
            if (!this.bonusWnMsgDisplayStarted)
            {
                this.bonusWnMsgDisplayStarted = true;

                this.displayMsg();this.displayRightMsg();
            }
        }
        else if (this.winToBet > 1)
        {
            if (!this.firstMsgDisplayStarted && this.winCounter.value() >= (this.parentScene.gameSocket.getBet() * 7))
            {
                this.firstMsgDisplayStarted = true;

                switch (this.winMsgPos)
                {
                    case 0:
                    case 1:
                    case 2:{this.displayMsg();};break;
                    case 3:{this.winMsg.letterSpacing = 65; this.displayMsgWithSpacing();};break;
                    case 4:{this.displayMsg();};break;
                    case 5:{this.displayMsg();this.displayRightMsg();};break;
                };
            }
            else if (this.firstMsgDisplayCompleted && !this.firstMsgHideStarted && this.winToBet >= 50 && this.winCounter.value() >= (this.parentScene.gameSocket.getBet() * 40))
            {
                this.firstMsgHideStarted = true;

                switch (this.winMsgPos)
                {
                    case 0:
                    case 1:
                    case 2:{this.hideMsg();};break;
                    case 3:{this.hideMsgWithSpacing();};break;
                    case 4:{this.hideMsg();};break;
                    case 5:{this.hideMsg();this.hideRightMsg();};break;
                };
            }
            else if (!this.secondMsgDisplayStarted && this.firstMsgHideCompleted && this.winToBet >= 50 && this.winCounter.value() >= (this.parentScene.gameSocket.getBet() * 40))
            {
                this.secondMsgDisplayStarted = true;

                this.winMsgPos = 6 + this.winCounter.value() % 2;
                this.initWinMsg();

                switch (this.winMsgPos)
                {
                    case 6:{this.displayMsg();this.displayRightMsg();};break;
                    case 7:{this.displayMsg();};break;
                };
            }
            else if (this.secondMsgDisplayCompleted && !this.secondMsgHideStarted && this.winToBet == 100 && this.winCounter.value() >= (this.parentScene.gameSocket.getBet() * 40))
            {
                this.secondMsgHideStarted = true;

                switch (this.winMsgPos)
                {
                    case 6:{this.hideMsg();this.hideRightMsg();};break;
                    case 7:{this.hideMsg();};break;
                };
            }
            else if (!this.thirdMsgDisplayStarted && this.secondMsgHideCompleted && this.winCounter.value() >= (this.parentScene.gameSocket.getBet() * 70) )
            {
                this.thirdMsgDisplayStarted = true;

                this.winMsgPos = 8 + this.winCounter.value() % 2;

                this.initWinMsg();

                switch (this.winMsgPos)
                {
                    case 8:{this.displayMsg();this.displayRightMsg();};break;
                    case 9:{this.displayMsg();this.displayRightMsg();};break;
                };
            }
        }
    }

    initWinMsg ()
    {
        this.winMsg.setText(this.msg[this.winMsgPos]);
        this.winMsg.x = this.msgStartingPositions[this.winMsgPos].x;
        this.winMsg.y = this.msgStartingPositions[this.winMsgPos].y;
        this.winMsg.setScale(this.msgScale[this.winMsgPos]);
        this.winMsg.letterSpacing = this.msgSpacings[this.winMsgPos];

        let index = 0;

        switch (this.winMsgPos)
        {
            case 5:{index = 0;this.winMsgR.setText("WIN")}; break;
            case 6:{index = 1;this.winMsgR.setText("WIN")}; break;
            case 8:{index = 2;this.winMsgR.setText("WIN")}; break;
            case 9:{index = 3;this.winMsgR.setText("ERB")}; break;
            case 10:{index = 4;this.winMsgR.setText("WIN")}; break;
        }

        this.winMsgR.x = this.msgStartingPositionsRight[index].x;
        this.winMsgR.y = this.msgStartingPositionsRight[index].y;
        this.winMsgR.setScale(this.msgScaleRight[index]);
        this.winMsgR.letterSpacing = this.msgSpacingsRight[index];
    }

    setWinMsg ()
    {
        if (this.winToBet == 50)
        {
            if (this.winMsgPos < 6)
            {
                this.winMsgPos = 6 +  Math.floor(Math.random() * 2) % 2;
            }                
        }
        else if (this.winToBet == 100)
        {
            if (this.winMsgPos < 8)
            {
                this.winMsgPos = 8 + Math.floor(Math.random() * 2) % 2;
            }                
        }

        if (this.winToBet > 1)
        {
            this.winMsg.setText(this.msg[this.winMsgPos]);
            this.winMsg.x = this.msgPositions[this.winMsgPos].x;
            this.winMsg.y = this.msgPositions[this.winMsgPos].y;
            this.winMsg.setScale(this.msgScale[this.winMsgPos]);
            this.winMsg.letterSpacing = this.msgSpacings[this.winMsgPos];
            this.winMsg.setOrigin(0);
            this.winMsg.setVisible(true);
        }    

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

        switch (this.winMsgPos)
        {
            case 5:{index = 0;this.winMsgR.setText("WIN")}; break;
            case 6:{index = 1;this.winMsgR.setText("WIN")}; break;
            case 8:{index = 2;this.winMsgR.setText("WIN")}; break;
            case 9:{index = 3;this.winMsgR.setText("ERB")}; break;
            case 10:{index = 4;this.winMsgR.setText("WIN")}; break;
        }

        if ((this.winMsgPos == 5 || this.winMsgPos == 6 || this.winMsgPos == 8 || this.winMsgPos == 9 || this.winMsgPos == 10) && this.winToBet > 1)
        {
            this.winMsgR.x = this.msgPositionsRight[index].x;
            this.winMsgR.y = this.msgPositionsRight[index].y;
            this.winMsgR.setScale(this.msgScaleRight[index]);
            this.winMsgR.letterSpacing = this.msgSpacingsRight[index];
            this.winMsgR.setVisible(true);
        }            
    }

    displayMsgCompleted ()
    {
        switch (this.winMsgPos)
        {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:{this.firstMsgDisplayCompleted = true;};break;
            case 6:
            case 7:{this.secondMsgDisplayCompleted = true;};break;
            case 8:
            case 9:{this.thirdMsgDisplayCompleted = true;};break;
        }            
    }

    hideMsgCompleted ()
    {
        switch (this.winMsgPos)
        {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:{this.firstMsgHideCompleted = true;};break;
            case 6:
            case 7:{this.secondMsgHideCompleted = true;};break;
            case 8:
            case 9:
            case 10:
        }            
    }

    //actions ...

    displayRightMsg ()//show extra text
    {
        let index = 0;

        this.winMsgR.setText("WIN");

        switch (this.winMsgPos)
        {
            case 5:index = 0;break;
            case 6:index = 1;break;
            case 8:index = 2; break;
            case 9:{index = 3;this.winMsgR.setText("ERB")}; break;
            case 10:index = 4;break;
        }

        this.winMsgR.x = this.msgStartingPositionsRight[index].x;
        this.winMsgR.y = this.msgStartingPositionsRight[index].y;
        this.winMsgR.setScale(this.msgScaleRight[index]);
        this.winMsgR.letterSpacing = this.msgSpacingsRight[index];
        
        this.winMsgR.setVisible(true);

        this.tweens.add({

            targets: this.winMsgR,
            ease: "Linear",
            duration: this.duration,

            x: this.msgPositionsRight[index].x,
            y: this.msgPositionsRight[index].y,
        });
    }

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

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

        this.tweens.add({

            targets: this.winMsgR,
            ease: "Linear",
            duration: this.duration,
            x: this.msgEndingPositionsRight[index].x,
            y: this.msgEndingPositionsRight[index].y,

            onCompleteParams: [index],
            callbackScope: this,
            onComplete: function () 
            {                
                this.winMsgR.setVisible(false);
        
                this.winMsgR.x = this.msgPositionsRight[index].x;
                this.winMsgR.y = this.msgPositionsRight[index].y;
            },
        });
    }

    displayMsg ()
    {
        if (this.playCompleted)//collected win with collect button
        {
            return;
        }

        this.winMsg.setVisible(true);

        this.tweens.add({

            targets: this.winMsg,
            ease: "Linear",
            duration: this.duration,
            x: this.msgPositions[this.winMsgPos].x,
            y: this.msgPositions[this.winMsgPos].y,
            
            callbackScope: this,
            onComplete: function () 
            {
                this.time.delayedCall(500, this.displayMsgCompleted, [], this);
            },
        });
    }

    hideMsg ()
    {
        this.tweens.add({

            targets: this.winMsg,
            ease: "Linear",
            duration: this.duration,
            x: this.msgEndingPositions[this.winMsgPos].x,
            y: this.msgEndingPositions[this.winMsgPos].y,            

            callbackScope: this,
            onComplete: function () 
            {
                this.winMsg.setVisible(false);
                this.winMsg.x = this.msgPositions[this.winMsgPos].x;
                this.winMsg.y = this.msgPositions[this.winMsgPos].y;
                this.hideMsgCompleted();
            },
        });
    }

    displayMsgWithSpacing ()
    {
        if (this.playCompleted)//collected win with collect button
        {
            return;
        }
        
        this.winMsg.setVisible(true);

        this.tweens.add({

            targets: this.winMsg,
            ease: "Linear",
            duration: this.duration,
            x: this.msgPositions[this.winMsgPos].x,
            y: this.msgPositions[this.winMsgPos].y,
            letterSpacing: this.msgSpacings[this.winMsgPos],

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

    hideMsgWithSpacing ()
    {
        this.tweens.add({
            targets: this.winMsg,
            ease: "Linear",
            duration: this.duration,
            x: this.msgEndingPositions[this.winMsgPos].x,
            y: this.msgEndingPositions[this.winMsgPos].y,
            letterSpacing: 65,

            callbackScope: this,
            onComplete: function () 
            {
                this.winMsg.setVisible(false);
                this.winMsg.letterSpacing = this.msgSpacings[this.winMsgPos];
                this.winMsg.x = this.msgPositions[this.winMsgPos].x;
                this.winMsg.y = this.msgPositions[this.winMsgPos].y;
                
                this.hideMsgCompleted();
            },
        });
    }
    
    showWinCounter()//center & show total win counter for small wins
    {
        this.winCounter.reset(this.win);
        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.reset(1);
        
        this.winCounter.setVisible(true);        
        
        if (this.win > 9)//count from 1 to total win
        {
            this.tweens.add({

                targets: this.winCounter,
                ease: "Linear",
                duration: this.counterSpeed,
                timer: this.counterSpeed,

                callbackScope: this,
                onComplete: function () 
                {
                    this.pulseWinCounter();
                },
            });
        }
        else//very small wins (< 9)
        {
            this.winCounter.reset(this.win);
            this.pulseWinCounter();
        }

        if (this.bwrt != 0 && this.bigWinThreshold >= this.bwrt)
        {
            this.parentScene.emitter.wake();
            this.repeat = 10;
        }
    }

    scaleWinCounter ()//center and scale up bigger wins
    {
        this.winCounter.reset(this.win);
        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.reset(1).setScale(0.2).setAlpha(0.2).setPosition(this.winCounterX, this.winCounterY).setVisible(true);

        //when very big win happen, vertical correction taking into account bitmap font height (180px in this case) to center correctly win counter

        let realWinCounterY = this.winCounterY + this.winCounterExtraY -  (180 / 2) * (1 - this.winCounterScaleCorrection);

        this.updateWinMsg = true;

        this.tweens.add({

            targets: this.winCounter.bmText,
            ease: "Linear",
            duration: this.counterSpeed,

            scale: this.winCounterScale * this.winCounterScaleCorrection,
            alpha: 1.0,
            y: realWinCounterY,
        });

        if (this.bwrt != 0 && this.bigWinThreshold >= this.bwrt)
        {
            this.parentScene.emitter.wake();
            this.repeat = 10;
        }
    }

    updateWinMsgCompleted ()
    {
        if (this.winToBet == 10 || this.winToBet == 50 || this.winToBet == 100)
        {
            this.updateWinMsg = false;
            
            this.pulseWinCounter();
        }
    }

    pulseWinCounter ()
    {
        if (this.forcesleep)
        {
            return;
        }
        else
        {
            this.pulseCounter();
        }            
    }

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

        if (this.winToBet > 1)
        {
            this.stars.start();
        }

        this.tweens.add({

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

    pulseCounterCompleted ()
    {
        this.tweens.add({
    
            targets: this.parentScene.sndWin,
            volume: 0,
    
            ease: "Linear",
            duration: 500,

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

        if (this.bwrt != 0 && this.bigWinThreshold >= this.bwrt)
        {
            this.parentScene.emitter.sleep();
        }

        this.collectWin();

        switch (this.winMsgPos)
        {
            case 0:
            case 1:
            case 2:{this.hideMsg();};break;
            case 3:{this.hideMsgWithSpacing();};break;
            case 4:{this.hideMsg();};break;
            case 5:{this.hideMsg();this.hideRightMsg();};break;
            case 6:{this.hideMsg();this.hideRightMsg();};break;
            case 7:{this.hideMsg();};break;
            case 8:{this.hideMsg();this.hideRightMsg();};break;
            case 9:{this.hideMsg();this.hideRightMsg();};break;
            case 10:{this.hideMsg();this.hideRightMsg();};break;
        }
    }

    collectWin ()//move to hide total win counter, add total win to balance
    {
        this.sndWinToCredit.play();
        
        this.parentScene.balanceUpdate2(this.win);//update balance

        this.tweens.add({

            targets: this.winCounter.bmText,
            ease: "Linear",
            duration: 500,
    
            x: 425,
            y: 1520,
            alpha: 0.0,

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

                        this.endScene();
                    }                        
                });
            },
        });
    }

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

        this.tweens.add({

            targets: this.winCounter.bmText,
            ease: "Linear",
            
            duration: 200,
            alpha: 0.0,

            callbackScope: this,
            onComplete: function () 
            {
                this.winVisible = false;
                this.timer = this.hiddenTime;
                this.playInProgress = false;
                this.winMsg.setVisible(false);
                this.winMsgR.setVisible(false);

                this.winCounter.setAlpha(1.0).setVisible(this.winVisible);
                this.lastWinMsg.setVisible(this.winVisible);
            },
        });
    }

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

            targets: this.lastWinMsg,
            ease: "Linear",
            
            duration: 200,
            alpha: 0.0,

            callbackScope: this,
            onComplete: function () 
            {
                this.lastWinMsg.setAlpha(1.0);
            },
        });
    }

    end ()
    {
        this.playInProgress = false;
        this.playCompleted = true;
        this.sndWinToCreditEnd.play();
        this.timer = this.hiddenTime;
        this.winVisible = false;
        this.resetPending = true;
        
        this.winCounter.setPosition(this.winCounterX, this.winCounterY + this.winCounterExtraY -  (180 / 2) * (1 - this.winCounterScaleCorrection));

        this.winCounter.setAlpha(1.0).setVisible(this.winVisible).setScale(this.winCounterScale * this.winCounterScaleCorrection);

        this.lastWinMsg.setVisible(this.winVisible);
        this.lastWinMsg.setPosition(this.lastWinMsg.x, this.lastWinMsg.y + this.winCounterExtraY);

        if (this.forcesleep)
        {
            this.sleep();
        }
    }

    endScene ()
    {
        this.end();        
        this.parentScene.collectWin();
    }

    forceSleep (win)
    {
        if (win == 0 )
        {
            return;
        }

        this.win = win;

        if (this.win > 0)
        {
            if (this.playCompleted)
            {
                this.sleep();
                return;
            }
            else
            {//display total win during next spin
                
                this.killTweens();
                this.setWin(win);//
                
                this.winCounter.reset(this.win).setAlpha(1.0).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(this.winCounterX, this.winCounterY + this.winCounterExtraY -  (180 / 2) * (1 - this.winCounterScaleCorrection));
                this.winCounter.setVisible(true);

                this.stars.stop();

                if (this.bwrt != 0 && this.bigWinThreshold >= this.bwrt)
                {
                    this.parentScene.emitter.sleep();
                }

                this.showWinOnNextSpin = true;
                this.forcesleep = true;
                this.parentScene.sndWin.stop();
                this.sndWinToCredit.stop();
                this.sndWinToCreditEnd.play();
                this.setWinMsg();

                this.resetPending = true;
            }
        }
    }

    forceClose (willSleep)
    {
        this.killTweens();

        this.playInProgress = false;
        this.playCompleted = true;
        this.timer = this.hiddenTime;
        this.winVisible = false;
        this.showWinOnNextSpin = false;

        this.resetPending = true;
       
        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(this.winCounterX, this.winCounterY + this.winCounterExtraY -  (180 / 2) * (1 - this.winCounterScaleCorrection));
        this.winCounter.setAlpha(1.0).setVisible(this.winVisible);

        this.setWinMsg();

        this.winMsg.setVisible(false);
        this.winMsgR.setVisible(false);

        this.lastWinMsg.setVisible(this.winVisible).setPosition(this.lastWinMsg.x, this.lastWinMsg.y + this.winCounterExtraY);

        this.stars.stop();

        if (this.bwrt != 0 && this.bigWinThreshold >= this.bwrt)
        {
            this.parentScene.emitter.sleep();
        }

        this.parentScene.sndWin.stop();
        
        if (willSleep)
        {
            this.sleep();
        }
    }
}
