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

    init (data)
    {
        this.parentScene = data.scene;
        
        this.buyBonusEnable = data.buyBonusEnable;
        this.jackpotEnable = data.jackpotEnable;

        this.lang = data.lang;
        this.files = data.files;

        this.rtp = data.rtp;
        
        this.minBetPos = {x: 0, y:0};

        if (data.minbetpos != undefined)
        {
            let v = Number.parseFloat(data.minbetpos.x);

            if (!Number.isNaN(v))
            {
                this.minBetPos.x = v;
            }

            v = Number.parseFloat(data.minbetpos.y);

            if (!Number.isNaN(v))
            {
                this.minBetPos.y = v;
            }
        }

        this.maxBetPos = {x: 0, y:0};

        if (data.maxbetpos != undefined)
        {
            let v = Number.parseFloat(data.maxbetpos.x);

            if (!Number.isNaN(v))
            {
                this.maxBetPos.x = v;
            }

            v = Number.parseFloat(data.maxbetpos.y);

            if (!Number.isNaN(v))
            {
                this.maxBetPos.y = v;
            }
        }
        
        this.rtpPos = {x: 0, y:0};
        
        if (data.rtppos != undefined)
        {
            let v = Number.parseFloat(data.rtppos.x);

            if (!Number.isNaN(v))
            {
                this.rtpPos.x = v;
            }

            v = Number.parseFloat(data.rtppos.y);

            if (!Number.isNaN(v))
            {
                this.rtpPos.y = v;
            }
        }

        this.orientationInit = false;
    }

    preload ()
    {
        this.load.image("infoIcon", "../common/assets/info_icon.png?ver=" + VERSION);

        for (let i = 0; i < this.files.length; ++i)
        {
            this.load.image(this.files[i].key, this.files[i].path + this.lang + "/" + this.files[i].file + "?ver=" + VERSION);
        }
    }

    create ()
    {
        this.infoIcon = this.add.image(490, 76, "infoIcon");
        this.infoIcon.setScrollFactor(0);

        this.txtInfo = this.add.text(530, 50, this.parentScene.getText(GLOBAL_TXT_INFO), { fontFamily: "Arial", fontSize: 48, fontStyle: "bold", color: "#ffffff" }).setOrigin(0);
        this.txtInfo.setScrollFactor(0);

        this.y = this.parentScene.topBar.displayHeight;

        this.layer = this.add.layer();

        this.graphicsL = this.make.graphics();

        this.graphicsL.fillStyle(0xffffff);
        this.graphicsL.fillRect(0, this.y, 1080, 800);

        this.maskL = this.graphicsL.createGeometryMask();

        this.graphicsP = this.make.graphics();

        this.graphicsP.fillStyle(0xffffff);
        this.graphicsP.fillRect(0, this.y, 1080, 1466);

        this.maskP = this.graphicsP.createGeometryMask();

        this.layer.setMask(this.maskP);

        this.images = [];

        for (let i = 0; i < this.files.length; ++i)
        {
            if (this.files[i].key == "callforGame" && !this.buyBonusEnable)
            {
                continue;
            }

            if (this.files[i].key == "jackpotGame" && !this.jackpotEnable)
            {
                continue;
            }

            let img = this.add.image(0, 0, this.files[i].key);
            img.setPosition(GAME_PORTRAIT_WIDTH / 2, this.y + img.height / 2);

            this.images.push(img);

            this.layer.add(img);
            this.y += img.height;
        }

        this.yl = this.y + GAME_LANDSCAPE_HEIGHT - 934;
        this.yp = this.y + GAME_PORTRAIT_HEIGHT - (this.parentScene.infoBtn.getY() - this.parentScene.infoBtn.getOriginY());

        let config =
        {
            scene: this,
            kineticMovement: true,
            timeConstantScroll: 325,
            horizontalScroll: false,
            verticalScroll: true,
            horizontalWheel: false,
            verticalWheel: false,
            deltaWheel: 40,

            thresholdOfTapTime: 300,
            thresholdOfTapDistance: 10,

            onDown: null,
            onUp: null,
            onUpdate: null,
            //scene pointer capture rect
            xmin: 0,
            xmax: GAME_PORTRAIT_WIDTH - 30,
            ymin: this.parentScene.topBar.displayHeight,
            ymax: GAME_PORTRAIT_HEIGHT - this.parentScene.topBar.displayHeight - this.parentScene.infoBtn.getHeight(),

            scrollXMin: 0,
            scrollXMax: GAME_PORTRAIT_WIDTH,
            scrollYMin: 0,
            scrollYMax: this.yp - GAME_PORTRAIT_HEIGHT,

            cameraBoundX: 0,
            cameraBoundY: 0,
            cameraBoundW: GAME_PORTRAIT_WIDTH,
            cameraBoundH: this.yp
        }

        this.scroller = new Scroller(config);

        this.scroller.start();

        this.bmTexts = [];

        this.index = -1;
        this.bpl = 1;
        this.units = 1;

        this.txtMinbet = this.add.text(0, 0, "", { fontFamily: "Arial", fontSize: 48, fontStyle: "bold", color: "#ffffff" }).setOrigin(0.5);
        this.layer.add(this.txtMinbet);
        
        this.txtMaxbet = this.add.text(0, 0, "", { fontFamily: "Arial", fontSize: 48, fontStyle: "bold", color: "#ffffff" }).setOrigin(0.5);
        this.layer.add(this.txtMaxbet);

        this.txtRTP = this.add.text(0, 0, "", { fontFamily: "Arial", fontSize: 38, color: "#ffffff" }).setOrigin(0.5);
        this.layer.add(this.txtRTP);

        let x = GAME_PORTRAIT_WIDTH - 10;
        let y = GAME_PORTRAIT_HEIGHT / 2 - this.parentScene.topBar.displayHeight / 2;

        let th = this.parentScene.topBar.displayHeight;
        let bh = this.parentScene.infoBtn.getHeight();

        this.scrollbar = this.scene.add("SceneScrollbarInfo", SceneScrollbar, true, { scene: this, x: x, y: y, th: th, bh: bh});

        this.sleep();
    }

    scrollChange (newValue)
    {
        if (this.parentScene.isLandscapeMode ())
        {
            this.cameras.main.setScroll(this.cameras.main.scrollX, (this.yl - GAME_LANDSCAPE_HEIGHT) * newValue);
        }
        else
        {
            this.cameras.main.setScroll(this.cameras.main.scrollX, (this.yp - GAME_PORTRAIT_HEIGHT) * newValue);
        }
    }

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

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

        this.graphicsP.setPosition(0, this.yp);

        this.infoIcon.setPosition(490, 76);
        this.txtInfo.setPosition(530, 50);

        for (let i = 0; i < this.images.length; ++i)
        {
            this.images[i].setPosition(540, this.images[i].y);
        }

        this.txtMinbet.setPosition(this.minBetPos.x, this.minBetPos.y);
        this.txtMaxbet.setPosition(this.maxBetPos.x, this.maxBetPos.y);
        this.txtRTP.setPosition(this.rtpPos.x, this.rtpPos.y);

        this.scroller.setBounds(0, GAME_PORTRAIT_WIDTH - 30, this.parentScene.topBar.displayHeight, GAME_PORTRAIT_HEIGHT - this.parentScene.topBar.displayHeight - this.parentScene.infoBtn.getHeight());
        this.scroller.setScrollRange(0, GAME_PORTRAIT_WIDTH, 0, this.yp - GAME_PORTRAIT_HEIGHT);
        this.scroller.setCameraBounds(0, 0, GAME_PORTRAIT_WIDTH, this.yp);

        this.scrollbar.setHeight (GAME_PORTRAIT_HEIGHT - 2 * this.parentScene.topBar.displayHeight - this.parentScene.infoBtn.getHeight());
        this.scrollbar.setPosition (GAME_PORTRAIT_WIDTH - 10, GAME_PORTRAIT_HEIGHT / 2 - this.parentScene.topBar.displayHeight / 2);

        this.layer.setMask(this.maskP);


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

    getPayInfo ()
    {
        let pi = this.parentScene.getPayInfo();

        this.units = pi.units;
        this.bpl = pi.bpl;
        this.lines = pi.lines;
        this.currencyPrecision = pi.precision;

        this.txtMinbet.setText(formatValue(pi.minbet, this.currencyPrecision));
        this.txtMaxbet.setText(formatValue(pi.maxbet, this.currencyPrecision));
    }

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

        this.scene.sleep();

        let status = this.scene.getStatus(this.scrollbar.scene.key);

        if (status < Phaser.Scenes.RUNNING)
        {
            return;
        }
        else if (status != Phaser.Scenes.SLEEPING)
        {
            this.scrollbar.sleep();
        }
    }

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

        this.getPayInfo();

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

        if (this.yield == undefined)
        {
            this.yield = this.parentScene.getYield();

            for (let i = 0; i < this.rtp.length; ++i)
            {
                if (this.rtp[i][0] == this.yield)
                {
                    this.txtRTP.setText(this.rtp[i][1] + " %");
                    break;
                }
            }

            if (this.txtRTP.text == "")
            {
                let a = [];
                let v;

                for (let i = 0; i < this.rtp.length; ++i)
                {
                    a.push(parseInt(this.rtp[i][0]));
                }
                 
                v = closest(parseInt(this.yield), a);

                for (let i = 0; i < this.rtp.length; ++i)
                {
                    if (parseInt(this.rtp[i][0]) == v)
                    {
                        this.txtRTP.setText(this.rtp[i][1] + " %");
                        break;
                    }
                }
            }
        }

        this.scrollbar.wake();
    }

    update ()
    {
        this.scroller.update();

        if (this.parentScene.isLandscapeMode ())
        {
            this.maskL.geometryMask.y = this.cameras.main.scrollY;
            this.scrollbar.setValue(this.cameras.main.scrollY / (this.yl - GAME_LANDSCAPE_HEIGHT));
        }
        else
        {
            this.maskP.geometryMask.y = this.cameras.main.scrollY;
            this.scrollbar.setValue(this.cameras.main.scrollY / (this.yp - GAME_PORTRAIT_HEIGHT));
        }
    }
}
