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.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(100, 330, 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.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.symbols = [[null, null, null],
                        [null, null, null],
                        [null, null, null],
                        [null, null, null],
                        [null, null, null]];
        
        this.symXL = [528, 744, 960, 1174, 1392],
        this.symYL = [533, 693, 853];

        this.symXP = [108, 324, 540, 756, 972],
        this.symYP = [653, 852, 1053];
        
        this.swRectL = new Phaser.Geom.Rectangle(420, 429, 1080, 505);
        this.swRectP = new Phaser.Geom.Rectangle(0, 377, 1100, 1260);

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

            bounds: this.swRectP,

			threshold: 10,

			velocityThreshold: 250,

			dir: 'left&right',

		});

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

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

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

        if (this.parentScene.isLandscapeMode ())
        {
            this.historyBar.setPosition(960, 263);
            this.historyIcon.setPosition(890, 76);
            this.txtHistory.setPosition(950, 50);

            this.txtSpinId.setPosition(260 + 420, 215);

            this.txtTime.setPosition(435, 315);
            this.txtBalance.setPosition(580 + 420, 330);
            this.txtBet.setPosition(800 + 420, 330);
            this.txtWin.setPosition(990 + 420, 330);

            this.combinationId.setPosition(550 + 420, 214);

            this.date.setPosition(180 + 420, 410);
            this.balance.setPosition(570 + 420, 410);
            this.bet.setPosition(800 + 420, 410);
            this.win.setPosition(990 + 420, 410);

            this.historyPrevBtn.setPosition(88 + 420, 215);
            this.historyNextBtn.setPosition(990 + 420, 215);

            for (let i = 0; i < this.symbols.length; ++i)
            {
                for (let j = 0; j < this.symbols[i].length; ++j)
                {
                    if (this.symbols[i][j] != null)
                    {
                        this.symbols[i][j].setPosition(this.symXL[i], this.symYL[j]).setScale(0.8);
                    }
                }
            }

            this.swipe.bounds = this.swRectL;
        }
        else
        {
            this.historyBar.setPosition(540, 263);
            this.historyIcon.setPosition(490, 76);
            this.txtHistory.setPosition(550, 50);

            this.txtSpinId.setPosition(260, 215);

            this.txtTime.setPosition(17, 315);
            this.txtBalance.setPosition(580, 330);
            this.txtBet.setPosition(800, 330);
            this.txtWin.setPosition(990, 330);

            this.combinationId.setPosition(550, 214);

            this.date.setPosition(180, 430);
            this.balance.setPosition(570, 430);
            this.bet.setPosition(800, 430);
            this.win.setPosition(990, 430);

            this.historyPrevBtn.setPosition(88, 215);
            this.historyNextBtn.setPosition(990, 215);

            for (let i = 0; i < this.symbols.length; ++i)
            {
                for (let j = 0; j < this.symbols[i].length; ++j)
                {
                    if (this.symbols[i][j] != null)
                    {
                        this.symbols[i][j].setPosition(this.symXP[i], this.symYP[j]).setScale(1.0);
                    }
                }
            }

            this.swipe.bounds = this.swRectP;
        }

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

    sleep ()
    {
        if (!this.historyPrevBtn.scene.isSleeping())
        {
            let status = this.historyPrevBtn.scene.getStatus(this.scene.key);

            if (status == Phaser.Scenes.RUNNING)
            {
                this.historyPrevBtn.sleep();
            }
        }

        if (!this.historyNextBtn.scene.isSleeping())
        {
            let status = this.historyNextBtn.scene.getStatus(this.scene.key);

            if (status == Phaser.Scenes.RUNNING)
            {
                this.historyNextBtn.sleep();
            }
        }

        for (let i = 0; i < this.symbols.length; ++i)
        {
            for (let j = 0; j < this.symbols[i].length; ++j)
            {
                if (this.symbols[i][j] != null)
                {
                    this.symbols[i][j].destroy();
                    this.symbols[i][j] = null;
                }
            }
        }

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

        this.scene.sleep();
    }

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

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

        if (this.currencyPrecision == undefined)
        {
            this.currencyPrecision = this.parentScene.getPrecision();
        }

        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.combinationId = spinInfo.combinationId;
        spinData.balance = spinInfo.balance;
        spinData.bet = spinInfo.bet;
        spinData.win = spinInfo.win;

        let symbolField = [];

        for (let i = 0; i < spinInfo.spinResult.symbolField.length; ++i)
        {
            let a = [...spinInfo.spinResult.symbolField[i]];

            symbolField.push(a);
        }

        spinData.symbolField = symbolField;

        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));

        for (let i = 0; i < this.spinHistory[this.spinHistoryPos].symbolField.length; ++i)
        {
            for (let j = 0; j < this.spinHistory[this.spinHistoryPos].symbolField[i].length; ++j)
            {
                let sym = this.spinHistory[this.spinHistoryPos].symbolField[i][j];

                if (this.symbols[i][j] == null)
                {
                    if (this.parentScene.isLandscapeMode ())
                    {
                        this.symbols[i][j] = this.add.image(this.symXL[i], this.symYL[j], SYMBOLS[sym]).setScale(0.8);
                    }
                    else
                    {
                        this.symbols[i][j] = this.add.image(this.symXP[i], this.symYP[j], SYMBOLS[sym]);
                    }
                }
                else
                {
                    this.symbols[i][j].setTexture(SYMBOLS[sym]);
                }
            }
        }
    }

    update ()
    {
    }
}
