class EnterFreeGames extends Phaser.Scene
{
    create (data)
    {
        this.parent = data.scene;
        this.vDelta = data.vDelta;
        
        this.hand = this.add.image(715, 285 + this.vDelta, 'rightHand');
        this.hand.setOrigin(0);

        let startFGBtnData = {
            'scene': this,
            'key':'startBtn',
            'frameWidth':182,
            'frameHeight':183,
            'x': 1170,
            'y': 431,
            'img': 'pressForFreeGames',
            'img_x': 1169,
            'img_y':433,
            'horizontalOffset': 0,
            'vDelta': this.vDelta,
            'onClick': this.onStartFG,
            'onPressed': null
        }

        this.startFGBtn = this.scene.add("StartFGBtn", SceneButtonEx, true, startFGBtnData);

        this.fgMarkSnd = this.sound.add("free_games_mark");

        this.fgTime = 20000;
        this.fgTimer = this.fgTime;

        if(this.vDelta == 0)
        {
            this.tx = 780;
        }
        else
        {
            this.tx = 120;
        }

        this.tween = null;

        this.scene.sleep();
    }

    onOrientationChange(offs)
    {
        this.vDelta = offs;

        if(this.vDelta == 0)
        {
            this.hand.setPosition(715, 285 + this.vDelta);
            this.startFGBtn.setPosition(1170, 431 + this.vDelta, 1169, 433 + this.vDelta);
            this.startFGBtn.setScale(1.0);
            this.tx = 780;

            if(this.tween != null && this.tween.isPlaying())
            {
                this.tweens.killTweensOf(this.hand);
            }
        }
        else
        {
            this.hand.setPosition(0, 805 + this.vDelta);
            this.startFGBtn.setPosition(620, 951 + this.vDelta, 618, 955 + this.vDelta);
            this.startFGBtn.setScale(2.0);

            this.tx = 120;

            if(this.tween != null && this.tween.isPlaying())
            {
                this.tweens.killTweensOf(this.hand);
            }
        }

        this.startTweening();
    }
    
    update(time, delta) 
    {
        this.fgTimer -= delta;

        if(this.fgTimer < 0)
        {
            this.fgTimer = this.fgTime;
            this.fgMarkSnd.play();
        }
    }
    
    bringToTop()
    {
        this.scene.bringToTop();
        this.startFGBtn.scene.bringToTop();
    }

    wake()
    {
        this.scene.wake();
        this.scene.bringToTop();
        this.startFGBtn.wake();
        this.startFGBtn.img.setVisible(true);

        this.startTweening();

    }

    startTweening()
    {
        this.tween = this.tweens.add({

            targets: this.hand,
            ease: 'Linear',
            duration: 500,

            x: this.tx,

            repeat: -1,
            yoyo: true,
        });
    }

    sleep()
    {
        this.scene.sleep();
        this.startFGBtn.sleep();
    }

    onStartFG()
    {
        this.fgMarkSnd.stop();
        this.tweens.killTweensOf(this.hand);
        this.tween = null;
        
        if(this.vDelta == 0)
        {
            this.hand.setPosition(715, 285 + this.vDelta);
        }
        else
        {
            this.hand.setPosition(0, 805 + this.vDelta);
        }

        this.startFGBtn.sleep();
        this.startFGBtn.img.setVisible(false);
        this.scene.sleep();

        this.parent.onFreeGamesEnter();
    }
}
