class Bar extends Phaser.Scene {

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

    create (data)
    {
        this.parent = data.scene;
        this.vDelta = data.vDelta;
        this.bgrbar = this.add.image(640, 689 + this.vDelta, data.key);
        this.winbar = this.add.image(376, 708 + this.vDelta, "winbar").setVisible(false);

        this.logoxl = data.logoxl;
        this.logoxp = data.logoxp;
        this.logoy = data.logoy;
        this.scale_ = 1.0;//data.scale;
        this.logo = this.add.image(this.logoxl, this.logoy, "logo");
        this.logo.setScale(this.scale_);


        this.betX = 1170;
        this.betY = 678;
        this.Bet = this.add.bitmapText(this.betX, this.betY + this.vDelta, data.fontBet, '');
        this.Bet.setOrigin(0.5);

        this.abX = 200;
        this.abY = 678;
        this.animBalance = new AnimatedAmount(this, this.abX, this.abY + this.vDelta, data.fontBet, true);
        this.animBalance.setOrigin(0.5);

        this.bmTxtCoin = this.add.bitmapText(560, 660 + this.vDelta, 'barLetters', 'COIN');
        this.bmTxtCoin.setOrigin(0.5);
        this.bmTxtCoin.setScale(0.35);
        this.bmTxtCoin.letterSpacing = -10;

        this.bmTxtUnits = this.add.bitmapText(660, 635 + this.vDelta, 'barNumbers', '');
        this.bmTxtUnits.setOrigin(0.5);
        this.bmTxtUnits.setScale(0.5);
        this.bmTxtUnits.letterSpacing = -10;

        this.bmTxtCurrency = this.add.bitmapText(720, 635 + this.vDelta, 'barLetters', '');
        this.bmTxtCurrency.setOrigin(0.5);
        this.bmTxtCurrency.setScale(0.5);
        this.bmTxtCurrency.letterSpacing = -10;

        this.betChangeTime = 100;
        this.betChangeTimer = -1;

        this.balanceChangeTime = 100;
        this.balanceChangeTimer = -1;

        this.features = [];
        this.featureX = 1000;
        this.featureY = 690;
        this.featureDelta = 65;

        this.currencyXCorrection = 0;

        this.scene.sleep();
    }
    
    addFeature()
    {
        let img = this.add.image(this.featureX, this.featureY + this.vDelta, "feature");
        
        img.setScale(0.3);
        
        this.features.push(img);

        this.featureX -= this.featureDelta;
    }

    removeFeature()
    {
        let img = this.features.pop();

        if(img != null) 
        {
            img.destroy();
            this.featureX += this.featureDelta;
        }                
    }

    onOrientationChange(v)
    {
        this.vDelta = v;

        this.bgrbar.setPosition(640, 689 + this.vDelta);
        this.winbar.setPosition(376, 708 + this.vDelta);
        this.Bet.setPosition(this.Bet.x, this.betY +  this.vDelta);
        this.animBalance.setPosition(this.abX, this.abY + this.vDelta);

        if(this.vDelta == 0)
        {
            this.bmTxtUnits.setPosition(660, 635);
            this.bmTxtUnits.setScale(0.5);
            this.bmTxtCurrency.setPosition(720 + this.currencyXCorrection, 635);
            this.bmTxtCurrency.setScale(0.5);
            this.bmTxtCoin.setPosition(560, 660);
            this.bmTxtCoin.setScale(0.35);

            this.logo.setPosition(this.logoxl, this.logoy);
            this.logo.setScale(1.0);
        }
        else
        {
            this.bmTxtUnits.setPosition(650, 1192 + this.vDelta);
            this.bmTxtUnits.setScale(1.0);
            this.bmTxtCurrency.setPosition(790, 1192 + this.vDelta);
            this.bmTxtCurrency.setScale(1.0);
            this.bmTxtCoin.setPosition(480, 1240 + this.vDelta);
            this.bmTxtCoin.setScale(0.7);

            this.logo.setPosition(this.logoxp, this.logoy + 1382, 1.9);
            this.logo.setScale(1.9);            
        }
    }

    setUnits()
    {
        let u = this.parent.gameSocket.getUnits() / 100;

		if (Number.isInteger(u))
		{
			this.bmTxtUnits.setText(u);
		}
		else
		{
			this.bmTxtUnits.setText(u.toFixed(2));
		}

		this.bmTxtCurrency.setText(this.parent.gameSocket.getCurrency());

        if(this.parent.gameSocket.getCurrency().length == 4)
        {
            this.currencyXCorrection = 20;

            if(this.vDelta == 0)
            {
                this.bmTxtCurrency.x = 720 + this.currencyXCorrection;
            }                
        }

        this.Bet.setText(this.parent.gameSocket.getBet());
    }

    hideInfo()
    {
        if(this.vDelta == 0)
        {
            this.bmTxtCoin.setVisible(false);
            this.bmTxtCurrency.setVisible(false);
            this.bmTxtUnits.setVisible(false);
        }

        this.winbar.setVisible(true);
    }

    showInfo()
    {
        if(this.vDelta == 0)
        {
            this.bmTxtCoin.setVisible(true);
            this.bmTxtCurrency.setVisible(true);
            this.bmTxtUnits.setVisible(true);
        }
                    
        this.winbar.setVisible(false);
    }

    onPaytableOpen()
    {
        this.bmTxtCoin.setVisible(false);
        this.bmTxtCurrency.setVisible(false);
        this.bmTxtUnits.setVisible(false);
        this.logo.setVisible(false);
    }

    onPaytableClosed()
    {
        this.bmTxtCoin.setVisible(true);
        this.bmTxtCurrency.setVisible(true);
        this.bmTxtUnits.setVisible(true);
        this.logo.setVisible(true);        
    }

    showLogo()
    {
        this.logo.setVisible(true);

        if(this.vDelta == 0)
        {
            this.logo.setPosition(this.logoxl, this.logoy);
            this.logo.setScale(1.0);
        }
        else
        {
            this.logo.setPosition(this.logoxp, this.logoy + 1382, 1.9);
            this.logo.setScale(1.9);

            this.bmTxtUnits.setPosition(650, 1192 + this.vDelta);
            this.bmTxtUnits.setScale(1.0);
            this.bmTxtCurrency.setPosition(790, 1192 + this.vDelta);
            this.bmTxtCurrency.setScale(1.0);
            this.bmTxtCoin.setPosition(480, 1240 + this.vDelta);
            this.bmTxtCoin.setScale(0.7);

            this.bmTxtCoin.setVisible(true);
            this.bmTxtCurrency.setVisible(true);
            this.bmTxtUnits.setVisible(true);
        }
    }

    hideLogo()
    {
        this.logo.setVisible(false);
    }

    updateBet()
    {
        this.Bet.setText(this.parent.gameSocket.getBet());
        this.Bet.setScale(1.2);
        this.betChangeTimer = this.betChangeTime;
    }

    updateBalance(v)
    {
        if(v == this.animBalance.value())
        {
            return;
        }

        this.animBalance.reset(v);
        this.animBalance.setScale(0.95);
        
        this.balanceChangeTimer = this.balanceChangeTime;
    }

    update(time, delta) 
    {
        if (this.parent.gameState == GAME_STATE_NOT_INITIALIZED)
        {
            return;
        }
        this.updateBetTimer(delta);
        this.updateBalanceTimer(delta);
        this.animBalance.update(delta, this.parent.balance);
    }

    updateBetTimer(delta)
    {
        if(this.betChangeTimer >= 0)
        {
            this.betChangeTimer -= delta;
            if(this.betChangeTimer < 0)
            {
                this.Bet.setScale(1.0);
            }
        }
    }

    updateBalanceTimer(delta)
    {
        if(this.balanceChangeTimer >= 0)
        {
            this.balanceChangeTimer -= delta;
            if( this.balanceChangeTimer < 0)
            {
                this.animBalance.setScale(1.0);
            }
        }
    }

    animateBalance()
    {
        this.tweens.timeline({

            targets: this.animBalance.bmText,
            ease: 'Linear',
            duration: 1000,
    
            tweens: [{
                scaleX: 1.1,
                scaleY: 1.1,
            },
            ],
            onCompleteScope: this,
            onComplete: function() 
            {
                this.animBalance.setScale (1.0);
            },
        });
    }

    reset()
    {
        this.tweens.killTweensOf(this.animBalance.bmText);
        this.animBalance.setScale (1.0);
        this.animBalance.setPosition(this.abX, this.abY + this.vDelta);
    }
}
