class SceneHistory extends Phaser.Scene
{
    constructor()
    {
        super({ key: "SceneHistory", active: true });
    }

    init (data)
    {
        this.parentScene = data.scene;
        this.lang = data.lang;

        this.orientationInit = false;
    }

    preload ()
    {
        this.load.image("historyIcon", "../common/assets/history_icon.png?ver=" + VERSION);
        this.load.image("historyBar", "../common/assets/history_bar.png?ver=" + VERSION);
        this.load.spritesheet("prevArrowBtn", "../common/assets/prev_arrow.png?ver=" + VERSION, { frameWidth: 90, frameHeight: 93 });
        this.load.spritesheet("nextArrowBtn", "../common/assets/next_arrow.png?ver=" + VERSION, { frameWidth: 90, frameHeight: 93 });
    }

    create (data)
    {
        this.parentScene = data.scene;

        this.getPayInfo();

        this.bgrpanel = this.add.image(540, 1016, "bgrpanel");

        this.symbol = this.add.image(536, 1190, undefined);

        this.multiplier = this.add.bitmapText(540, 800, "multiplierFont", "").setScale(0.9).setLetterSpacing(-90).setOrigin(0.5);

        this.txtProgressiveBonus = this.add.text(540, 1507, "", { fontFamily: "Arial", fontSize: 60, fontStyle: "bold", color: "#02a014" }).setOrigin(0.5);

        this.cardsLeft = [];

        this.cardsLeft.push(this.add.image(216, 1384, undefined));//bottom card
        this.cardsLeft.push(this.add.image(214, 1254, undefined).setAngle(-5));
        this.cardsLeft.push(this.add.image(208, 1126, undefined).setAngle(-10));
        this.cardsLeft.push(this.add.image(196, 1002, undefined).setAngle(-13));//top
        
        this.cardsRight = [];

        this.cardsRight.push(this.add.image(864, 1384, undefined));//bottom card
        this.cardsRight.push(this.add.image(866, 1254, undefined).setAngle(5));
        this.cardsRight.push(this.add.image(874, 1126, undefined).setAngle(10));
        this.cardsRight.push(this.add.image(886, 1002, undefined).setAngle(13));//top

        this.historyIcon = this.add.image(490, 76, "historyIcon");
        this.txtHistory = this.add.text(560, 50, this.parentScene.getText(GLOBAL_TXT_HISTORY), { fontFamily: "Arial", fontSize: 48, fontStyle: "bold", color: "#ffffff" }).setOrigin(0);
        
        this.historyBar = this.add.image(540, 263, "historyBar");

        this.txtSpinId = this.add.text(260, 215, this.parentScene.getText(GLOBAL_TXT_HISTORY_SPIN_ID), { fontFamily: "Arial", fontSize: 42, color: "#fcd51a" }).setOrigin(0.5);

        this.txtTime = this.add.text(17, 315, this.parentScene.getText(GLOBAL_TXT_HISTORY_TIME), { fontFamily: "Arial", fontSize: 30, fontStyle: "bold", color: "#ffe670" }).setOrigin(0);
        this.txtBalance = this.add.text(580, 330, this.parentScene.getText(GLOBAL_TXT_HISTORY_BALANCE), { fontFamily: "Arial", fontSize: 30, fontStyle: "bold", color: "#ffe670" }).setOrigin(0.5);
        this.txtBet = this.add.text(800, 330, this.parentScene.getText(GLOBAL_TXT_HISTORY_BET), { fontFamily: "Arial", fontSize: 30, fontStyle: "bold", color: "#ffe670" }).setOrigin(0.5);
        this.txtWin = this.add.text(990, 330, this.parentScene.getText(GLOBAL_TXT_HISTORY_WIN), { fontFamily: "Arial", fontSize: 30, fontStyle: "bold", color: "#ffe670" }).setOrigin(0.5);

        let historyPrevBtnData = {
            "scene": this,
            "key": "prevArrowBtn",
            "onClick": this.prev,
            "onPressed": null,
            "onHold": null
        };

        this.historyPrevBtn = this.scene.add("HistoryPrevBtn", SceneButton, true, historyPrevBtnData);
        this.historyPrevBtn.setPosition(88, 215);

        let historyNextBtnData = {
            "scene": this,
            "key": "nextArrowBtn",
            "onClick": this.next,
            "onPressed": null,
            "onHold": null
        };

        this.historyNextBtn = this.scene.add("HistoryNextBtn", SceneButton, true, historyNextBtnData);
        this.historyNextBtn.setPosition(990, 215);

        this.combinationId = this.add.text(550, 214, "", { fontFamily: "Arial", fontSize: 48, color: "#fcd51a" }).setOrigin(0.5);
        this.combinationId.setPosition(550, 214);

        this.date = this.add.text(180, 430, "", { fontFamily: "Arial", fontSize: 36, color: "#ffffff" }).setOrigin(0.5);
        this.balance = this.add.text(570, 430, "", { fontFamily: "Arial", fontSize: 36, color: "#ffffff" }).setOrigin(0.5);
        this.bet = this.add.text(800, 430, "", { fontFamily: "Arial", fontSize: 36, color: "#ffffff" }).setOrigin(0.5);
        this.win = this.add.text(990, 430, "", { fontFamily: "Arial", fontSize: 36, color: "#ffffff" }).setOrigin(0.5);

        this.spinHistory = [];
        this.spinHistoryPos = -1;

        this.swRect = new Phaser.Geom.Rectangle(0, 377, 1100, 1260);

		this.swipe = this.gestures.add.swipe({

            bounds: this.swRect,

			threshold: 10,

			velocityThreshold: 250,

			dir: 'left&right',

		});

		this.swipe.on("swipe", this.onSwipe.bind(this));

        this.bpl = 0;
        this.units = 0;

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

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

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

    sleep ()
    {
        this.historyPrevBtn.sleep();
        this.historyNextBtn.sleep();

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

        this.scene.sleep();
    }

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

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

        this.getPayInfo();

        this.historyPrevBtn.wake();
        this.historyNextBtn.wake();

        if (this.spinHistory.length > 0)
        {
            this.spinHistoryPos = this.spinHistory.length - 1;

            this.formatSpin();
        }
        else
        {
            this.spinHistoryPos = -1;
        }
    }

    prev ()
    {
        if (this.spinHistory.length == 0 ) return;

        if ((this.spinHistoryPos - 1) >= 0)
        {
            --this.spinHistoryPos;

            this.formatSpin();
        }
    }

    next ()
    {
        if (this.spinHistory.length == 0) return;

        if ((this.spinHistoryPos + 1) < this.spinHistory.length)
        {
            ++this.spinHistoryPos;

            this.formatSpin();
        }
    }

    onSwipe (swipe)
    {
        if (swipe.left)
        {
            this.next();
        }
        else if(swipe.right)
        {
            this.prev();
        }
    }

    addSpin (spinInfo)
    {
        let spinData = {};
        
        spinData.spinSymbol = spinInfo.spinSymbol;
        spinData.suitSymbol = spinInfo.suitSymbol;
        
        spinData.state = [...spinInfo.newState];
        
        spinData.combinationId = spinInfo.combinationId;
        spinData.win = spinInfo.win;
        spinData.bet = spinInfo.bet;
        spinData.units = spinInfo.units;
        spinData.bpl = spinInfo.bpl;

        spinData.balance = spinInfo.balance;

        spinData.date = this.formatDate(new Date());

        this.spinHistory.push(spinData);
    }

    padTo2Digits (num)
    {
        return num.toString().padStart(2, '0');
    }

    formatDate (date)
    {
        return (
            [
                date.getFullYear(),
                this.padTo2Digits(date.getMonth() + 1),
                this.padTo2Digits(date.getDate()),
            ].join('-') +
            ' ' +
            [
                this.padTo2Digits(date.getHours()),
                this.padTo2Digits(date.getMinutes()),
                this.padTo2Digits(date.getSeconds()),
            ].join(':')
        );
    }

    formatSpin ()
    {
        this.combinationId.setText(this.spinHistory[this.spinHistoryPos].combinationId);

        this.date.setText(this.spinHistory[this.spinHistoryPos].date);
        this.balance.setText(formatValue(this.spinHistory[this.spinHistoryPos].balance, this.currencyPrecision));
        this.bet.setText(formatValue(this.spinHistory[this.spinHistoryPos].bet, this.currencyPrecision));
        this.win.setText(formatValue(this.spinHistory[this.spinHistoryPos].win, this.currencyPrecision));

        let state = this.spinHistory[this.spinHistoryPos].state;

        if (state[0] > 0)
        {
            this.multiplier.setText(formatValue(state[0] * this.spinHistory[this.spinHistoryPos].bet, this.currencyPrecision)).setVisible(true);
        }
        else
        {
            this.multiplier.setVisible(false);
        }

        this.txtProgressiveBonus.setText(formatValue(state[1], this.currencyPrecision));

        if (this.spinHistory[this.spinHistoryPos].spinSymbol > 0)
        {
            this.symbol.setTexture(SYMBOLS[this.spinHistory[this.spinHistoryPos].spinSymbol - 1]);
        }
        else
        {
            this.symbol.setTexture(SUITS[this.spinHistory[this.spinHistoryPos].suitSymbol - 1]);
        }

        this.cardsLeft[0].setTexture(SUITS_SMALL[state[5] - 1]);//bottom card
        this.cardsLeft[1].setTexture(SUITS_SMALL[state[4] - 1]);
        this.cardsLeft[2].setTexture(SUITS_SMALL[state[3] - 1]);
        this.cardsLeft[3].setTexture(SUITS_SMALL[state[2] - 1]);//top
        
        this.cardsRight[0].setTexture(SUITS_SMALL[state[5] - 1]);//bottom card
        this.cardsRight[1].setTexture(SUITS_SMALL[state[4] - 1]);
        this.cardsRight[2].setTexture(SUITS_SMALL[state[3] - 1]);
        this.cardsRight[3].setTexture(SUITS_SMALL[state[2] - 1]);//top
    }

    getPayInfo ()
    {
        this.currencyPrecision = this.parentScene.parentScene.gameSocket.getCurrencyPrecision();
    }
}
