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

		this.limitMultiplier = 10;

		this.limitValues = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500];
	}

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

		this.autoCollectLimitSelect = data.autoCollectLimitSelect;

		this.orientationInit = false;
	}

	preload ()
	{
		this.load.image("autocollectline", "../common/assets/carouselline.png?ver=" + VERSION);
		this.load.audio("autocollectcarousel", "../common/assets/sound/common/carousel.mp3?ver=" + VERSION);
	}

	create ()
	{
		this.overlay = this.add.image(816, 741, "overlay");
		this.overlay.setScale(1.0, 1.62);
		this.overlay.setInteractive();//prevent closing the scene when clicked inside bet dialog

		this.borderUp = this.add.image(816, 630, "autocollectline").setDepth(100);
		this.borderDown = this.add.image(816, 870, "autocollectline").setDepth(100);

		this.txtTitle = this.add.text(816, 232, this.getText(LOCAL_TXT_AUTO_COLLECT_TITLE), { fontFamily: "Arial", fontSize: 50, fontStyle: "bold", color: "#ffffff" }).setOrigin(0.5);
		this.txtTitle.setScale(1.25);

		this.txtConfirm1 = this.add.text(309, 290, this.getText(LOCAL_TXT_AUTO_COLLECT_TAP1), { fontFamily: "Arial", fontSize: 40, fontStyle: "bold", color: "#ffffff" }).setOrigin(0);
		this.txtConfirm2 = this.add.text(309, 330, this.getText(LOCAL_TXT_AUTO_COLLECT_TAP2), { fontFamily: "Arial", fontSize: 40, fontStyle: "bold", color: "#ffffff" }).setOrigin(0);

		this.txtSlide = this.add.text(1263, 350, this.getText(LOCAL_TXT_AUTO_COLLECT_SLIDE), { fontFamily: "Arial", fontSize: 40, fontStyle: "bold", color: "#ffffff" }).setOrigin(1.0);

		this.arrows = this.add.image(1303, 325, "arrowsUpDown");

		this.rect = new Phaser.Geom.Rectangle(316, 600, 400, 1200);

		this.pan = this.gestures.add.pan({

			bounds: this.rect,

			threshold: 10,

			velocityThreshold: 250,

			dir: 'up&down',

		});

		this.pan.on("panstart", this.onPanStart.bind(this), this);
		this.pan.on("pan", this.onPan.bind(this), this);
		this.pan.on("panend", this.onPanEnd.bind(this), this);

		this.taps = [];

		this.txtBet = this.add.text(60, 595, "", { fontFamily: "Arial", fontSize: 92, fontStyle: "bold", color: "#ffffff" }).setOrigin(0.5).setVisible(false);

		this.rtdata = [{ x: 816, y: 750 - 2 * (CAROUSEL_DISTANCE * 2), scale: 0.5 * 2, alpha: 0.6 },
		{ x: 816, y: 750 - (CAROUSEL_DISTANCE * 2), scale: 0.75 * 2, alpha: 0.8 },
		{ x: 816, y: 750, scale: 1.0 * 2, alpha: 1.0 },
		{ x: 816, y: 750 + (CAROUSEL_DISTANCE * 2), scale: 0.75 * 2, alpha: 0.8 },
		{ x: 816, y: 750 + 2 * (CAROUSEL_DISTANCE * 2), scale: 0.5 * 2, alpha: 0.6 }];

		this.rts = [];

		this.animSpeed = 50;

		this.panDelta = 0;
		this.panStep = 70;
		this.isPanning = false;

		this.autoStep = 20;

		this.toUp = false;
		this.toDown = false;

		this.camera = this.cameras.main;
		this.camera.setOrigin(0);

		this.camera.setSize(GAME_PORTRAIT_WIDTH, GAME_PORTRAIT_HEIGHT);
		this.camera.setPosition(0, 465);
		this.camera.setScroll(276, 0);

		this.scene.sleep();
	}

	setLimits ()
	{
		this.limits = [...this.limitValues].reverse();

		this.bpl = this.parentScene.gameSocket.getBetPerLine();
		let units = this.parentScene.gameSocket.getUnits();
		let currencyPrecision = this.parentScene.gameSocket.getCurrencyPrecision();

		for (let i = 0; i < this.limits.length; ++i)
		{
			this.limits[i] = formatValue(this.limits[i] * this.bpl * units, currencyPrecision);
		}

		this.limits.push("NONE");

		if (this.rts.length == 0)
		{
			for (let i = 0; i < this.limits.length; ++i)
			{
				this.txtBet.setText(this.limits[i]);

				let rt = this.add.renderTexture(816, 465, 500, 100).setVisible(false);

				rt.draw(this.txtBet, rt.displayWidth / 2, rt.displayHeight / 2);

				rt.i = i;

				let tap = this.gestures.add.tap(rt, {

					bounds: this.rect,

					time: 2500,
					tapInterval: 250,
					threshold: 9,
					tapOffset: 10,

					taps: 1,
				});

				tap.on("tap", this.onTap.bind(this));

				this.rts.push(rt);
			}
		}
		else
		{
			for (let i = 0; i < this.limits.length; ++i)
			{
				this.txtBet.setText(this.limits[i]);

				this.rts[i].clear();

				this.rts[i].setScale(1.0);

				this.rts[i].draw(this.txtBet, this.rts[i].displayWidth / 2, this.rts[i].displayHeight / 2);
			}
		}

		this.selected = this.rts.length - 1;

		this.setLimitsAtPositions();
	}

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

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

		this.setLimitsAtPositions();

		this.pan.setDetectBounds(this.rect);

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

    setLimitsAtPositions ()
 	{
		if (this.rts.length == 0)
		{
			return;
		}

		if (this.selected > 1)
		{
			this.setLimitAtPos (this.selected - 2, TOPMOST);
		}

		if (this.selected > 0)
		{
			this.setLimitAtPos (this.selected - 1, TOP);
		}

		this.setLimitAtPos (this.selected, MIDDLE);

		if (this.selected < this.rts.length - 1)
		{
			this.setLimitAtPos (this.selected + 1, BOTTOM);
		}

		if (this.selected < this.rts.length - 2)
		{
			this.setLimitAtPos (this.selected + 2, BOTTOMMOST);
		}
	}

	setLimitAtPos (i, pos)
	{
		this.rts[i].y = this.rtdata[pos].y;
		this.rts[i].setScale(this.rtdata[pos].scale);
		this.rts[i].setVisible(true);
		this.rts[i].setAlpha(this.rtdata[pos].alpha);
	}

	isTweening ()
	{
		for (let i = 0; i < this.rts.length; ++i)
		{
			if (this.tweens.isTweening(this.rts[i]))
			{
				return true;
			}
		}

		return false;
	}

	move (i, y, scale, alpha, visible)
	{
		this.tweens.add({

			targets: this.rts[i],
			ease: "Linear",
			duration: this.animSpeed,

			y: y,
			scale: scale,
			alpha: alpha,

			callbackScope: this,

			onComplete: function () 
			{
				this.rts[i].setVisible(visible);
			},
		})
	}

	up ()
	{
		if (this.selected > 1)//move topmost out
		{
			this.move(this.selected - 2, this.rtdata[TOPMOST].y - 55, 0, 0, false);
		}

		if (this.selected > 0)//move top to topmost position
		{
			this.move(this.selected - 1, this.rtdata[TOPMOST].y, this.rtdata[TOPMOST].scale, this.rtdata[TOPMOST].alpha, true);
		}

		this.move(this.selected, this.rtdata[TOP].y, this.rtdata[TOP].scale, this.rtdata[TOP].alpha, true);//	move middle to top	

		if (this.selected < this.rts.length - 1)//	move bottom to middle	
		{
			this.move(this.selected + 1, this.rtdata[MIDDLE].y, this.rtdata[MIDDLE].scale, this.rtdata[MIDDLE].alpha, true);
		}

		if (this.selected < this.rts.length - 2)//move bottommost up
		{
			this.move(this.selected + 2, this.rtdata[BOTTOM].y, this.rtdata[BOTTOM].scale, this.rtdata[BOTTOM].alpha, true);
		}

		if (this.selected < this.rts.length - 3)//move new to bottommost pos
		{
			this.rts[this.selected + 3].y = this.rtdata[BOTTOMMOST].y + 55;
			this.rts[this.selected + 3].setScale(0.0);
			this.rts[this.selected + 3].setVisible(true);

			this.move(this.selected + 3, this.rtdata[BOTTOMMOST].y, this.rtdata[BOTTOMMOST].scale, this.rtdata[BOTTOMMOST].alpha, true);
		}

		++this.selected;
	}

	down ()
	{
		if (this.selected > 2)//move new to topmost
		{
			this.rts[this.selected - 3].setVisible(true);

			this.move(this.selected - 3, this.rtdata[TOPMOST].y, this.rtdata[TOPMOST].scale, this.rtdata[TOPMOST].alpha, true);
		}

		if (this.selected > 1)//move topmost to top
		{
			this.move(this.selected - 2, this.rtdata[TOP].y, this.rtdata[TOP].scale, this.rtdata[TOP].alpha, true);
		}

		if (this.selected > 0)//move top to middle
		{
			this.move(this.selected - 1, this.rtdata[MIDDLE].y, this.rtdata[MIDDLE].scale, this.rtdata[MIDDLE].alpha, true);
		}

		this.move(this.selected, this.rtdata[BOTTOM].y, this.rtdata[BOTTOM].scale, this.rtdata[BOTTOM].alpha, true);//move middle to bottom

		if (this.selected < this.rts.length - 1)//move bottom to bottommost pos
		{
			this.move(this.selected + 1, this.rtdata[BOTTOMMOST].y, this.rtdata[BOTTOMMOST].scale, this.rtdata[BOTTOMMOST].alpha, true);
		}

		if (this.selected < this.rts.length - 2)//move bottommost out
		{
			this.move(this.selected + 2, this.rtdata[BOTTOMMOST].y + 55, 0.0, 0.0, false);
		}

		--this.selected;
	}

	onPanStart ()
	{
		if (this.selected === undefined)
		{
			return;
		}

		this.panDelta = 0;

		this.isPanning = true;
	}

	onPan (pan)
	{
		if (!this.isPanning)
		{
			return;
		}

		if (this.selected === undefined)
		{
			return;
		}

		if (this.isTweening())
		{
			return;
		}

		this.panDelta += pan.dy;

		this.toUp = false;
		this.toDown = false;

		if (pan.dy < 0)
		{
			this.toUp = true;
		}
		else
		{
			this.toDown = true;
		}

		if (Math.abs(this.panDelta) > this.panStep)
		{
			this.roll();
		}
	}

	onPanEnd (pan, obj, pointer)
	{
		if (Math.abs(pointer.velocity.y) > 10)
		{
			this.toUp = false;
			this.toDown = false;

			let steps = Math.floor(Math.abs(pointer.velocity.y) / this.autoStep);

			if (pointer.velocity.y < 0)
			{
				this.toUp = true;
				this.panDelta = -steps * this.panStep - 1;
			}
			else
			{
				this.toDown = true;
				this.panDelta = steps * this.panStep + 1;
			}
		}
		else
		{
			this.panDelta = 0;
		}

		this.isPanning = false;
	}

	roll ()
	{
		if (this.selected == 0 && this.toDown)
		{
			return;
		}

		if (this.selected == this.rts.length - 1 && this.toUp)
		{
			return;
		}
		
		this.sound.play("autocollectcarousel");

		if (this.toUp)
		{
			this.panDelta += this.panStep;
			this.up();
		}
		else if (this.toDown)
		{
			this.panDelta -= this.panStep;
			this.down();
		}
	}

	onTap (tap, rt)
	{
		if (this.isTweening())
		{
			return;
		}

		if (rt.i == this.selected)
		{
			if (this.autoCollectLimitSelect !== undefined)
			{
				if (this.selected == this.limits.length - 1)
				{
					this.autoCollectLimitSelect(-1);
				}
				else
				{
					this.autoCollectLimitSelect(this.limits[this.selected]);
				}
				
				this.scene.sleep();
			}
		}
		else
		{
			let n = Math.abs(rt.i - this.selected);

			this.toUp = (rt.i > this.selected) ? true : false;
			this.toDown = (rt.i < this.selected) ? true : false;

			if (this.toUp)
			{
				this.panDelta = -(n - 1) * this.panStep;
			}
			else if (this.toDown)
			{
				this.panDelta = (n - 1) * this.panStep;
			}
	
			this.roll();
		}
	}

	getText (textId)
	{
		return this.parentScene.getText(textId);
	}

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

		this.scene.sleep();
	}

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

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

		if (this.bpl == undefined || this.bpl != this.parentScene.gameSocket.getBetPerLine())
		{
			this.setLimits();
		}

		for (let i = 0; i < this.rts.length; ++i)
		{
			this.rts[i].setVisible(false);
		}

		this.setLimitsAtPositions();
	}

	update ()
	{
		if (this.isTweening())
		{
			return;
		}

		if (Math.abs(this.panDelta) > this.panStep)
		{
			if (this.toUp)
			{
				this.panDelta += this.panStep;
			}
			else if (this.toDown)
			{
				this.panDelta -= this.panStep;
			}

			this.roll();
		}
	}
}
