class Paytable extends Phaser.Scene {

    constructor ()
    {
        super({ key: 'Paytable', active: true });
    }
    
    create (data)
    {
        this.parent = data.scene;
        this.vDelta = data.vDelta;
        this.paytable = [];
        this.bmTexts = [];

        this.y = 360;

        this.bgrpaytable = this.add.image(640, this.y + this.vDelta, 'bgrpaytable');

        let backToGameBtnData = {
            'scene': this,
            'key':'backToGameClick',
            'frameWidth':350,
            'frameHeight':66,
            'x': 680,
            'y': 680,
            'onClick': this.onPaytableClosed,
            'onPressed':null
        };
        
        if(!this.sys.game.device.os.desktop)
        {
            backToGameBtnData.key = 'backToGameTap';
        }

        this.backToGameBtn = this.scene.add("BacktoGameBtn", SceneButton, true, backToGameBtnData);

        this.backToGameBtn.setScale(0.6);

        this.backToGameBtn.scene.sleep();
        
        this.scene.setVisible(false);

        this.index = -1;
        this.highlightPrevent = false;
    }
    
    update(time, delta)
    {
        if (this.parent.hasOwnProperty('reelmanager') && this.parent.gameState == GAME_STATE_PAYTABLE)
        {
            this.parent.reelmanager.update(time,delta);
        }
    }

    onOrientationChange(offs, prevofs)
    {
        this.vDelta = offs;

        this.bgrpaytable.setPosition(this.bgrpaytable.x, this.y + this.vDelta);

        if(this.bmTexts.length > 0)
        {
            for(let i = 0; i < this.bmTexts.length; ++i)
            {
                this.bmTexts[i].setPosition(this.positions[i].x, this.positions[i].y + offs);
                if(this.index != -1 && this.index != i)
                {
                    this.bmTexts[this.index].setScale(1.0);
                }    
            }

            if(this.index != -1 )
            {
                this.highlightWin(this.index);
            }
        }            

        if (this.scale.orientation === Phaser.Scale.PORTRAIT)
        {
            this.backToGameBtn.scene.sleep();
            this.input.off('pointerdown', this.onPaytableClosedWithSnd, this);
        }
    }
    
    getPaytable()
    {
        this.paytable = this.parent.gameSocket.getPaytable();
        this.bpl = this.parent.gameSocket.getBetPerLine();

        this.createPaytable();
    }

    open()
    {
        this.scene.setVisible(true);
        this.scene.bringToTop();
        this.input.on('pointerdown', this.onPaytableClosedWithSnd, this);        
    }
    
    showBackButton()
    {
        this.backToGameBtn.scene.wake();
        this.backToGameBtn.scene.bringToTop();
    }

    onPaytableClosed()
    {
        this.input.off('pointerdown', this.onPaytableClosedWithSnd, this);
        this.backToGameBtn.scene.sleep();
        this.scene.setVisible(false);
        this.parent.onPaytableClosed();
    }

    onPaytableClosedWithSnd()
    {
        this.sound.play("click");
        this.input.off('pointerdown', this.onPaytableClosedWithSnd, this);
        this.backToGameBtn.scene.sleep();
        this.scene.setVisible(false);
        this.parent.onPaytableClosed();
    }

    highlightWinSymbols(symbol, len)
    {
        switch(symbol)
        {
            case CHERRY:
            case LEMON:
            case ORANGE:
            case PLUM:{this.highlightWin(len - 3);};break;
            case BELL:{this.highlightWin(3 + len - 3);};break;
            case GRAPE:
            case MELON:{this.highlightWin(6 + len - 3);};break;
            case STAR:{this.highlightWin(9 + len - 3);};break;
            case SEVEN:{this.highlightWin(12 + len - 2);};break;
            case BREAK:break;
            case SCATTER:{this.highlightWin(16 + len - 3);};break;
            case MYSTERY:{this.highlightWin(19);this.highlightWin(20);};break;
        }
    }

    highlightWin(index)
    {
        if(this.highlightPrevent)
        {
            return;
        }

        if(this.index >= 0 && this.index != 19 && this.index != 20)
        {
            this.bmTexts[this.index].setFont('fontPaytable');
            this.bmTexts[this.index].setScale(1.0);
            this.bmTexts[this.index].setPosition(this.positions[this.index].x, this.positions[this.index].y + this.vDelta);
        }

        this.bmTexts[index].setFont('fontPaytableH');

        if(this.bmTexts[index].scale == 1.0)
        {
            this.bmTexts[index].setScale(1.2);

            this.bmTexts[index].y = this.positions[index].y + this.vDelta + 2;
        }    

        if(!this.bmTexts[index].visible)
        {
            this.bmTexts[index].setVisible(true);
        }

        this.index = index;
    }

    clearWins()
    {
        for(let i = 0; i < this.bmTexts.length; ++i)
        {
            this.bmTexts[i].setPosition(this.positions[i].x, this.positions[i].y + this.vDelta);
            this.bmTexts[i].setFont('fontPaytable');
            this.bmTexts[i].setScale(1.0);
        }

        this.index = -1;
        this.highlightPrevent = false;
    }

    createPaytable()
    {
        this.positions = [];

        this.positions[0] = {x:920, y:588 + 39};//cherry, lemon. orange, plum
        this.positions[1] = {x:920, y:544 + 39};
        this.positions[2] = {x:920, y:498 + 39};

        this.positions[3] = {x:1085, y:343 + 39};//bell
        this.positions[4] = {x:1085, y:298 + 39};
        this.positions[5] = {x:1085, y:253 + 39};

        this.positions[6] = {x:333, y:594 + 39};//grape, melon
        this.positions[7] = {x:333, y:548 + 39};
        this.positions[8] = {x:333, y:505 + 39};

        this.positions[9] = {x:199, y:405 + 39};//star
        this.positions[10] = {x:199, y:360 + 39};
        this.positions[11] = {x:199, y:316 + 39};

        this.positions[12] = {x:199, y:206 + 39} ;//seven
        this.positions[13] = {x:199, y:161 + 39};
        this.positions[14] = {x:199, y:116 + 39};
        this.positions[15] = {x:199, y:71 + 39};

        this.positions[16] = {x:1085, y:163 + 39};//scatter
        this.positions[17] = {x:1085, y:118 + 39};
        this.positions[18] = {x:1085, y:74 + 39};

        this.positions[19] = {x:575, y:383 + 39} ;//mystery min
        this.positions[20] = {x:747, y:383 + 39};//mystery max

       
        this.bmTexts.push(this.add.bitmapText(this.positions[0].x, this.positions[0].y + this.vDelta, 'fontPaytable', this.paytable[0][3] * this.bpl).setOrigin(0.5));//cherry, lemon, orange, plum
        this.bmTexts.push(this.add.bitmapText(this.positions[1].x, this.positions[1].y + this.vDelta, 'fontPaytable', this.paytable[0][4] * this.bpl).setOrigin(0.5));
        this.bmTexts.push(this.add.bitmapText(this.positions[2].x, this.positions[2].y + this.vDelta, 'fontPaytable', this.paytable[0][5] * this.bpl).setOrigin(0.5));

        this.bmTexts.push(this.add.bitmapText(this.positions[3].x, this.positions[3].y + this.vDelta, 'fontPaytable', this.paytable[4][3] * this.bpl).setOrigin(0.5));//bell
        this.bmTexts.push(this.add.bitmapText(this.positions[4].x, this.positions[4].y + this.vDelta, 'fontPaytable', this.paytable[4][4] * this.bpl).setOrigin(0.5));
        this.bmTexts.push(this.add.bitmapText(this.positions[5].x, this.positions[5].y + this.vDelta, 'fontPaytable', this.paytable[4][5] * this.bpl).setOrigin(0.5));

        this.bmTexts.push(this.add.bitmapText(this.positions[6].x, this.positions[6].y + this.vDelta, 'fontPaytable', this.paytable[5][3] * this.bpl).setOrigin(0.5));//grape, melon
        this.bmTexts.push(this.add.bitmapText(this.positions[7].x, this.positions[7].y + this.vDelta, 'fontPaytable', this.paytable[5][4] * this.bpl).setOrigin(0.5));
        this.bmTexts.push(this.add.bitmapText(this.positions[8].x, this.positions[8].y + this.vDelta, 'fontPaytable', this.paytable[5][5] * this.bpl).setOrigin(0.5));

        this.bmTexts.push(this.add.bitmapText(this.positions[9].x, this.positions[9].y + this.vDelta, 'fontPaytable', this.paytable[7][3] * this.bpl).setOrigin(0.5));//star
        this.bmTexts.push(this.add.bitmapText(this.positions[10].x, this.positions[10].y + this.vDelta, 'fontPaytable', this.paytable[7][4] * this.bpl).setOrigin(0.5));
        this.bmTexts.push(this.add.bitmapText(this.positions[11].x, this.positions[11].y + this.vDelta, 'fontPaytable', this.paytable[7][5] * this.bpl).setOrigin(0.5));

        this.bmTexts.push(this.add.bitmapText(this.positions[12].x, this.positions[12].y + this.vDelta, 'fontPaytable', this.paytable[8][2] * this.bpl).setOrigin(0.5));//seven
        this.bmTexts.push(this.add.bitmapText(this.positions[13].x, this.positions[13].y + this.vDelta, 'fontPaytable', this.paytable[8][3] * this.bpl).setOrigin(0.5));
        this.bmTexts.push(this.add.bitmapText(this.positions[14].x, this.positions[14].y + this.vDelta, 'fontPaytable', this.paytable[8][4] * this.bpl).setOrigin(0.5));
        this.bmTexts.push(this.add.bitmapText(this.positions[15].x, this.positions[15].y + this.vDelta, 'fontPaytable', this.paytable[8][5] * this.bpl).setOrigin(0.5));        

        this.bmTexts.push(this.add.bitmapText(this.positions[16].x, this.positions[16].y + this.vDelta, 'fontPaytable', this.paytable[10][3] * this.bpl * 5).setOrigin(0.5));//scatter
        this.bmTexts.push(this.add.bitmapText(this.positions[17].x, this.positions[17].y + this.vDelta, 'fontPaytable', this.paytable[10][4] * this.bpl * 5).setOrigin(0.5));
        this.bmTexts.push(this.add.bitmapText(this.positions[18].x, this.positions[18].y + this.vDelta, 'fontPaytable', this.paytable[10][5] * this.bpl * 5).setOrigin(0.5));

        this.bmTexts.push(this.add.bitmapText(this.positions[19].x, this.positions[19].y + this.vDelta, 'fontPaytable', this.paytable[11][3] * this.bpl * 5).setOrigin(0.5));//mystery min
        this.bmTexts.push(this.add.bitmapText(this.positions[20].x, this.positions[20].y + this.vDelta, 'fontPaytable', this.paytable[12][3] * this.bpl * 5).setOrigin(0.5));//mystery max
        
        this.bmTexts.forEach(text => {text.setScale(1.0);text.letterSpacing = -20;});
    }

    updatePaytable()
    {
        if(this.index >= 0)//clear win when bet button is pressed and do not highlight it until next spin
        {
            this.bmTexts[this.index].setFont('fontPaytable');
            this.bmTexts[this.index].setScale(1.0);
            this.bmTexts[this.index].setPosition(this.positions[this.index].x, this.positions[this.index].y + this.vDelta);
        }

        this.highlightPrevent = true;

        this.bpl = this.parent.gameSocket.getBetPerLine();

        if(this.bmTexts.length == 0)
        {
            this.createPaytable();
        }

        this.bmTexts[0].setText(this.paytable[0][3] * this.bpl);//cherry, lemon, orange, plum
        this.bmTexts[1].setText(this.paytable[0][4] * this.bpl);
        this.bmTexts[2].setText(this.paytable[0][5] * this.bpl);

        this.bmTexts[3].setText(this.paytable[4][3] * this.bpl);//bell
        this.bmTexts[4].setText(this.paytable[4][4] * this.bpl);
        this.bmTexts[5].setText(this.paytable[4][5] * this.bpl);

        this.bmTexts[6].setText(this.paytable[5][3] * this.bpl);//grape, mellon
        this.bmTexts[7].setText(this.paytable[5][4] * this.bpl);
        this.bmTexts[8].setText(this.paytable[5][5] * this.bpl);

        this.bmTexts[9].setText(this.paytable[7][3] * this.bpl);//plum
        this.bmTexts[10].setText(this.paytable[7][4] * this.bpl);
        this.bmTexts[11].setText(this.paytable[7][5] * this.bpl);

        this.bmTexts[12].setText(this.paytable[8][2] * this.bpl);//seven
        this.bmTexts[13].setText(this.paytable[8][3] * this.bpl);
        this.bmTexts[14].setText(this.paytable[8][4] * this.bpl);
        this.bmTexts[15].setText(this.paytable[8][5] * this.bpl);        

        this.bmTexts[16].setText(this.paytable[10][3] * this.bpl * 5);//scatter
        this.bmTexts[17].setText(this.paytable[10][4] * this.bpl * 5);
        this.bmTexts[18].setText(this.paytable[10][5] * this.bpl * 5);

        this.bmTexts[19].setText(this.paytable[11][3] * this.bpl * 5);//min
        this.bmTexts[20].setText(this.paytable[12][3] * this.bpl * 5);//max
    }
}