class JackpotWin extends Phaser.Scene {

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

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

        this.vDelta = data.vDelta;

        this.index = 0;//0 grand, 1 maxi, 2 mini
        this.jptypes = ['GRAND', 'MAXI', 'MINI', 'MULTI'];
        this.jpxs = [70, 165, 165, 70];//left x of jackpot type  text
        this.spacings = [-35, 0, 25, 5];

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

        this.bmTextJackpot = this.add.bitmapText(-1300, 320 + this.vDelta, data.fontText, 'JACKPOT');
        this.bmTextJackpot.setScale(1.0);
        this.bmTextJackpot.letterSpacing = -77;
        this.txtJackpotX = -5;

        this.bmTextWinner = this.add.bitmapText(1300, 550 + this.vDelta, data.fontText, 'WINNER');
        this.bmTextWinner.setScale(1.0);
        this.bmTextWinner.letterSpacing = -49;
        this.txtWinnerX = 25;
        
        this.bmJackpotType = this.add.bitmapText(this.jpxs[this.index], -370 + this.vDelta, data.fontText, this.jptypes[this.index]);
        this.bmJackpotType.letterSpacing = this.spacings[this.index];
        this.bmJackpotType.setScale(1.0);
        this.bmJackpotType.setVisible(false);

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

        this.stars = new Stars(starsConfig);

        this.animWinScale = 1.15;
        this.animWinX = 560 * this.animWinScale;
        this.animWinY = 410;
        this.animWin = new AnimatedAmount(this, this.animWinX, this.animWinY + this.vDelta, 'totalwinNumbers', false);
        this.animWin.setScale(0.2);
        this.animWin.setAlpha(0.2);
        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.tmultpl = 1;
        this.animWinScaleCorrection = 1.0;
        this.yoyoJackpotWinActionTween = null;
        this.moveJackpotTypeActionTween = null;
    }

    onOrientationChange(offs)
    {
        this.vDelta = offs;

        this.bmTextJackpot.setPosition(this.bmTextJackpot.x, 320 + this.vDelta);
        this.bmTextWinner.setPosition(this.bmTextWinner.x, 550 + this.vDelta);

        if(this.parent.gameState == GAME_STATE_JACKPOT_WIN)
        {
            if (this.moveJackpotTypeActionTween == null || !this.moveJackpotTypeActionTween.isPlaying())
            {
                this.bmJackpotType.setPosition(this.bmJackpotType.x, 318 + this.vDelta);
            }
            else if(this.moveJackpotTypeActionTween.isPlaying())
            {
                this.bmJackpotType.y =  (this.vDelta == 0)?this.bmJackpotType.y - 800:this.bmJackpotType.y + 800; 
                this.moveJackpotTypeActionTween = this.createMoveJackpotTypeTween(this.moveJackpotTypeActionTween);
            }

            if (this.yoyoJackpotWinActionTween == null || !this.yoyoJackpotWinActionTween.isPlaying())
            {
                this.animWin.setPosition(this.animWin.bmText.x, this.animWinY + this.vDelta);
            }
            else if(this.yoyoJackpotWinActionTween.isPlaying())
            {
                this.yoyoJackpotWinActionTween = this.createYoyoTween(this.yoyoJackpotWinActionTween);
            }
        }
        else
        {
            this.bmJackpotType.setPosition(this.bmJackpotType.x, -370 + this.vDelta);
            this.animWin.setPosition(this.animWinX, this.animWinY + this.vDelta);
        }                
    }        

    start()
    {
        if( this.parent.gameSocket.getJackpotWin() == 0)
        {
            return;
        }

        let jpwins = this.parent.gameSocket.getJackpotWins();

        if(jpwins.length == 0)
        {
            return;
        }

        let nmwins = 0;

        for(let i = 0; i < jpwins.length; ++i)
        {
            if(jpwins[i] > 0)
            {
                ++nmwins;
                this.index = i;
            }
        }

        if(nmwins > 1)
        {
            this.index = 3;//select multi
        }

        this.bmJackpotType.setText(this.jptypes[this.index]);
        this.bmJackpotType.x = this.jpxs[this.index];
        this.bmJackpotType.letterSpacing = this.spacings[this.index];

        let u = this.parent.gameSocket.getJackpotWin();
        this.tmultpl = (u + '').replace('.', '').length + 1;
        this.animWin.setSpeed(500 * this.tmultpl);

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

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

        this.bmJackpotType.setVisible(false);

        this.tweens.killTweensOf(this.animWin.bmText);
        this.tweens.killTweensOf(this.bmTextJackpot);
        this.tweens.killTweensOf(this.bmTextWinner);
        this.tweens.killTweensOf(this.bmJackpotType);
        this.tweens.killTweensOf(this.sndJackpotWin);
                        
        this.timer = 100;
        this.scene.sleep();

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

        this.yoyoJackpotWinActionTween = null;
        this.moveJackpotTypeActionTween = 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.sndJackpotWin.play();
                this.moveJackpotAction();
                this.moveWinnerAction();
                return;
            }
            
            if(this.tweeningended)
            {
                if(this.sndWinToCredit.isPlaying)
                {
                    return;
                }
                else
                {
                    this.sceneCompleted = true;
                    this.endActions();
                }
            }                
        }

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

    moveJackpotAction()
    {
        this.tweens.add({

            targets: this.bmTextJackpot,
            ease: 'Linear',
            duration: 500,
            x: this.txtJackpotX,
        });
    }

    moveToHideJackpotAction()
    {
        this.tweens.add({

            targets: this.bmTextJackpot,
            ease: 'Linear',
            duration: 500,
            x: -1300,
        });
    }

    moveWinnerAction()
    {
        this.tweens.add({

            targets: this.bmTextWinner,
            ease: 'Linear',
            duration: 500,
            x: this.txtWinnerX,

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

    moveToHideWinnerAction()
    {
        this.tweens.add({

            targets: this.bmTextWinner,
            ease: 'Linear',
            duration: 500,
            x: 1300,

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

    moveToHideActions()
    {
        this.moveToHideJackpotAction();
        this.moveToHideWinnerAction();
    }
    
    createMoveJackpotTypeTween(tween)
    {
        let duration = 500;

        if(tween != null && tween.isPlaying())
        {
            duration = tween.totalDuration - tween.totalElapsed;
            
            if(duration < 0)
            {
                duration = 0;
            }
    
            tween.stop();
        }
    
        tween = this.tweens.add({

            targets: this.bmJackpotType,
            ease: 'Linear',
            duration: duration, 
            y: 318 + this.vDelta,
        });

        return tween;
    }

    moveJackpotTypeAction()
    {
        this.bmJackpotType.setVisible(true);
        this.moveJackpotTypeActionTween = this.createMoveJackpotTypeTween(this.moveJackpotTypeActionTween);
    }
    
    moveToHideJackpotTypeAction()
    {
        this.tweens.add({

            targets: this.bmJackpotType,
            ease: 'Linear',
            duration: 300,
            y: -370 + this.vDelta,

            onCompleteScope: this,
            onComplete: function() 
            {
                this.bmJackpotType.setVisible(false);        
            }    
        });
    }

    scaleAnimWinAction()
    {
        this.animWin.reset(this.parent.gameSocket.getJackpotWin());
        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.animWin.setOrigin(0.5);

        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() 
            {
                this.yoyoJackpotWinAction();
                setTimeout(this.onJackpotWait.bind(this), 3000);
            },
        });
    }

    createYoyoTween(tween)
    {
        if(tween != null && tween.isPlaying())
        {
            tween.stop();
        }

        this.animWin.setPosition(this.animWinX, this.animWinY + this.vDelta);

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

        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: -1,
            yoyo: true,
        });

        return tween;
    }

    yoyoJackpotWinAction()
    {
        this.yoyoJackpotWinActionTween = this.createYoyoTween(this.yoyoJackpotWinActionTween);
    }

    moveToHideAnimWinAction()
    {
        this.tweens.add({

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

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

    endActions()
    {
        this.sndWinToCreditEnd.play();
        this.reset();
        this.parent.onJackpotCompleted();
    }
    
    onJackpotWait()
    {
        this.parent.onJackpotWait();
    }

    onJackpotTaken()
    {
        this.parent.balance += Math.floor(this.parent.gameSocket.getJackpotWin()/this.parent.gameSocket.getUnits());//update balance
        this.parent.bar.animateBalance();
        this.sndWinToCredit.play();
        
        this.yoyoJackpotWinActionTween.stop();                
        this.stars.stop();
        
        this.tweens.add({

            targets: this.sndJackpotWin,
            volume: 0,

            ease: 'Linear',
            duration: 500,

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

        this.moveToHideAnimWinAction();
        this.moveToHideJackpotTypeAction();
    }
}
