class SceneBonus extends Phaser.Scene
{
	constructor ()
    {
        super({key: "SceneBonus", active: true});
    }
    
    init (data)
    {
        this.parentScene = data.scene;
        this.selectBonus = data.selectBonus;
        this.cancelBonus = data.cancelBonus;

        this.orientationInit = false;
    }

    preload ()
    {
        this.load.image("rectPlate", "assets/rectplate.png?ver=" + VERSION);
		this.load.image("bonusRect", "assets/bonusrect.png?ver=" + VERSION);
        
        this.load.spritesheet("buyBonusBtn", "assets/buy.png?ver=" + VERSION, { frameWidth: 246, frameHeight: 104 });
        this.load.spritesheet("closeBtn", "assets/close.png?ver=" + VERSION, { frameWidth: 219, frameHeight: 225 });
    }

	create ()
	{
        this.container = this.add.container(0, 0).setVisible(false);
        
        this.bonusPlate = this.add.image(4, -32, "plate").setInteractive();

        this.container.add(this.bonusPlate);
        
        this.rectPlate = this.add.image(0, 78, "rectPlate");
        this.container.add(this.rectPlate);

        this.fgBonus = this.add.image(-35, -177, "bonusRect");
        this.container.add(this.fgBonus);

        this.txtBonus = this.add.text(0, 82, "", { fontFamily: "Arial", fontSize: 56, fontStyle: "bold", color: "#f0f0f0"}).setOrigin(0.5);
        
        this.container.add(this.txtBonus);

        let buyBonusBtnData = {
            "scene": this,
            "key":"buyBonusBtn",
            "onClick": this.buyBonus,
            "onPressed": null,
            "onHold": null
        }

        this.buyBonusBtn = this.scene.add("BuyBonusBtn", SceneButton, true, buyBonusBtnData);
        this.buyBonusBtn.setPosition(-3, 180);
        this.buyBonusBtn.sleep();
        this.container.add(this.buyBonusBtn.button);

        let closeBtnData = {
            "scene": this,
            "key":"closeBtn",
            "onClick": this.cancel,
            "onPressed": null,
            "onHold": null
        }

        this.closeBtn = this.scene.add("CloseBtn", SceneButton, true, closeBtnData);
        this.closeBtn.setPosition(400, -300);

        this.container.add(this.closeBtn.button);

		this.scene.sleep();
	}

	onOrientationChange ()
	{
        let status = this.scene.getStatus(this.scene.key);

        if (status < Phaser.Scenes.RUNNING)
        {
            return;
        }

        if (this.tweens.isTweening(this.container))
        {
            this.tweens.killTweensOf(this.container)
            this.container.setAlpha(1.0);
        }

        if (this.parentScene.isLandscapeMode())
        {
            this.container.setPosition(950, 610);
        }
        else
        {
            this.container.setPosition(536, 945);
        }

        if (!this.orientationInit)
        {
            this.orientationInit = true;
        }
	}

    buyBonus ()
    {
        if (this.tweens.isTweening(this.container))
        {
            return;
        }

        if (this.selectBonus != null)
        {
            this.selectBonus(BET_TYPE_BONUS);
        }
    }

    cancel ()
    {
        if (this.tweens.isTweening(this.container))
        {
            return;
        }

        if (this.cancelBonus != null)
        {
            this.cancelBonus();
        }
    }

	sleep ()
    {
        let status = this.scene.getStatus(this.scene.key);

        if (status < Phaser.Scenes.RUNNING)
        {
            return;
        }

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

        this.container.setVisible(false);

        this.buyBonusBtn.sleep();
        this.closeBtn.sleep();

        this.scene.sleep();
    }

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

        if (!this.orientationInit)
        {
            this.onOrientationChange();
        }

        if (this.currencyPrecision == undefined)
        {
            this.currencyPrecision = this.parentScene.currencyPrecision;
        }
        
        this.container.setVisible(true);

        this.closeBtn.wake();
        
        this.txtBonus.setText(formatValue(this.parentScene.gameSocket.getBet(BET_TYPE_BONUS), this.currencyPrecision));

        if (this.balance < this.parentScene.gameSocket.getBet(BET_TYPE_BONUS))
        {
            this.buyBonusBtn.disable();
        }

        this.bringToTop();

        this.container.setScale(0.1);
        this.container.setAlpha(0.1);

        this.tweens.add({

            targets: this.container,
            ease: "Linear",
            duration: 250,

            scale: 1.0,
            alpha: 1.0,

            callbackScope: this,
            onComplete: function () 
            {                
                this.buyBonusBtn.wake();
            },
        });
    }

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

        this.buyBonusBtn.scene.bringToTop();
        this.closeBtn.scene.bringToTop();
    }
}
