/**** 
* Classes
****/
var Paddle = Container.expand(function () {
	var self = Container.call(this);
	var paddleGraphics = self.attachAsset('paddle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.move = function (x) {
		self.x = x;
	};
});
var Ball = Container.expand(function () {
	var self = Container.call(this);
	var ballGraphics = self.attachAsset('ball', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.velocity = {
		x: 5,
		y: -5
	};
	self.move = function () {
		self.x += self.velocity.x;
		self.y += self.velocity.y;
	};
	self.bounce = function (axis) {
		if (axis === 'x') {
			self.velocity.x *= -1;
			LK.effects.flashObject(self, 0xff0000, 300);
		} else if (axis === 'y') {
			self.velocity.y *= -1;
			LK.effects.flashObject(self, 0xff0000, 300);
		}
	};
});
var Brick = Container.expand(function (scoreValue) {
	var self = Container.call(this);
	self.scoreValue = scoreValue;
	var brickGraphics = self.attachAsset('brick', {
		anchorX: 0.5,
		anchorY: 0.5
	});
});
var StoryManager = Container.expand(function () {
	var self = Container.call(this);
	self.choices = [];
	self.currentChoiceIndex = 0;
	self.makeChoice = function (choiceIndex) {
		self.currentChoiceIndex = choiceIndex;
		var choice = self.choices[choiceIndex];
		choice.score = LK.getScore();
		self.emit('choiceMade', choice);
	};
	self.addChoice = function (choice) {
		self.choices.push(choice);
	};
	self.displayChoices = function () {
		// Display logic for choices goes here
	};
	self.on('choiceMade', function (choice) {
		if (choice.score >= 100) {
			// Outcome for high score
		} else {
			// Outcome for low score
		}
		startNextLevel(); // Proceed to the next level after choice
	});
});
/**** 
* Initialize Game
****/
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background
});
/**** 
* Game Code
****/
var storyManager = game.addChild(new StoryManager());
storyManager.addChoice({
	text: 'Take the safe path',
	outcome: 'safe'
});
storyManager.addChoice({
	text: 'Take the risky path',
	outcome: 'risky'
});
var scoreTxt = new Text2('0', {
	size: 150,
	fill: '#ffffff',
	anchorX: 0.5,
	anchorY: 0
});
LK.gui.top.addChild(scoreTxt);
var paddle = game.addChild(new Paddle());
paddle.x = game.width / 2;
paddle.y = game.height - 100;
var ball = game.addChild(new Ball());
ball.x = game.width / 2;
ball.y = game.height - 150;
var bricks = [];
for (var i = 0; i < 5; i++) {
	for (var j = 0; j < 8; j++) {
		var scoreValue = (i + 1) * (j + 1);
		var brick = game.addChild(new Brick(scoreValue));
		brick.x = 150 + j * (brick.width + 10);
		brick.y = 100 + i * (brick.height + 10);
		bricks.push(brick);
	}
}
game.on('move', function (obj) {
	var pos = obj.event.getLocalPosition(game);
	paddle.move(pos.x);
});
LK.on('tick', function () {
	ball.move();
	// Ball collision with walls
	if (ball.x <= ball.width / 2 || ball.x >= game.width - ball.width / 2) {
		ball.bounce('x');
	}
	if (ball.y <= ball.height / 2) {
		ball.bounce('y');
	}
	// Ball collision with paddle
	if (ball.intersects(paddle)) {
		ball.bounce('y');
	}
	// Ball collision with bricks
	for (var b = bricks.length - 1; b >= 0; b--) {
		if (ball.intersects(bricks[b])) {
			ball.bounce('y');
			var scoreIncrement = bricks[b].scoreValue;
			LK.setScore(LK.getScore() + scoreIncrement);
			LK.effects.flashObject(bricks[b], 0xffffff, 500);
			LK.setTimeout(function () {
				bricks[b].destroy();
			}, 500);
			bricks.splice(b, 1);
			scoreTxt.setText(LK.getScore().toString());
			// Check if all bricks are destroyed and initiate level transition
			if (bricks.length === 0) {
				transitionToNextLevel();
			}
		}
	}
	// Ball out of bounds
	if (ball.y > game.height) {
		ball.x = game.width / 2;
		ball.y = game.height - 150;
		ball.velocity = {
			x: 5,
			y: -5
		};
	}
	// Function to handle level transition
	function transitionToNextLevel() {
		// Animate paddle and ball soaring upwards
		var levelTransitionDuration = 2000; // Duration of the transition in milliseconds
		var levelTransitionTick = 0;
		var levelTransitionInterval = LK.setInterval(function () {
			paddle.y -= 5;
			ball.y -= 5;
			levelTransitionTick += 16.6667; // Approximately 1 tick at 60FPS
			if (levelTransitionTick >= levelTransitionDuration) {
				LK.clearInterval(levelTransitionInterval);
				storyManager.displayChoices();
			}
		}, 16.6667);
	}
	// Function to start the next level
	function startNextLevel() {
		// Reset paddle and ball position
		paddle.x = game.width / 2;
		paddle.y = game.height - 100;
		ball.x = game.width / 2;
		ball.y = game.height - 150;
		// TODO: Increase difficulty for the next level
	}
}); /**** 
* Classes
****/
var Paddle = Container.expand(function () {
	var self = Container.call(this);
	var paddleGraphics = self.attachAsset('paddle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.move = function (x) {
		self.x = x;
	};
});
var Ball = Container.expand(function () {
	var self = Container.call(this);
	var ballGraphics = self.attachAsset('ball', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.velocity = {
		x: 5,
		y: -5
	};
	self.move = function () {
		self.x += self.velocity.x;
		self.y += self.velocity.y;
	};
	self.bounce = function (axis) {
		if (axis === 'x') {
			self.velocity.x *= -1;
			LK.effects.flashObject(self, 0xff0000, 300);
		} else if (axis === 'y') {
			self.velocity.y *= -1;
			LK.effects.flashObject(self, 0xff0000, 300);
		}
	};
});
var Brick = Container.expand(function (scoreValue) {
	var self = Container.call(this);
	self.scoreValue = scoreValue;
	var brickGraphics = self.attachAsset('brick', {
		anchorX: 0.5,
		anchorY: 0.5
	});
});
var StoryManager = Container.expand(function () {
	var self = Container.call(this);
	self.choices = [];
	self.currentChoiceIndex = 0;
	self.makeChoice = function (choiceIndex) {
		self.currentChoiceIndex = choiceIndex;
		var choice = self.choices[choiceIndex];
		choice.score = LK.getScore();
		self.emit('choiceMade', choice);
	};
	self.addChoice = function (choice) {
		self.choices.push(choice);
	};
	self.displayChoices = function () {
		// Display logic for choices goes here
	};
	self.on('choiceMade', function (choice) {
		if (choice.score >= 100) {
			// Outcome for high score
		} else {
			// Outcome for low score
		}
		startNextLevel(); // Proceed to the next level after choice
	});
});
/**** 
* Initialize Game
****/
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background
});
/**** 
* Game Code
****/
var storyManager = game.addChild(new StoryManager());
storyManager.addChoice({
	text: 'Take the safe path',
	outcome: 'safe'
});
storyManager.addChoice({
	text: 'Take the risky path',
	outcome: 'risky'
});
var scoreTxt = new Text2('0', {
	size: 150,
	fill: '#ffffff',
	anchorX: 0.5,
	anchorY: 0
});
LK.gui.top.addChild(scoreTxt);
var paddle = game.addChild(new Paddle());
paddle.x = game.width / 2;
paddle.y = game.height - 100;
var ball = game.addChild(new Ball());
ball.x = game.width / 2;
ball.y = game.height - 150;
var bricks = [];
for (var i = 0; i < 5; i++) {
	for (var j = 0; j < 8; j++) {
		var scoreValue = (i + 1) * (j + 1);
		var brick = game.addChild(new Brick(scoreValue));
		brick.x = 150 + j * (brick.width + 10);
		brick.y = 100 + i * (brick.height + 10);
		bricks.push(brick);
	}
}
game.on('move', function (obj) {
	var pos = obj.event.getLocalPosition(game);
	paddle.move(pos.x);
});
LK.on('tick', function () {
	ball.move();
	// Ball collision with walls
	if (ball.x <= ball.width / 2 || ball.x >= game.width - ball.width / 2) {
		ball.bounce('x');
	}
	if (ball.y <= ball.height / 2) {
		ball.bounce('y');
	}
	// Ball collision with paddle
	if (ball.intersects(paddle)) {
		ball.bounce('y');
	}
	// Ball collision with bricks
	for (var b = bricks.length - 1; b >= 0; b--) {
		if (ball.intersects(bricks[b])) {
			ball.bounce('y');
			var scoreIncrement = bricks[b].scoreValue;
			LK.setScore(LK.getScore() + scoreIncrement);
			LK.effects.flashObject(bricks[b], 0xffffff, 500);
			LK.setTimeout(function () {
				bricks[b].destroy();
			}, 500);
			bricks.splice(b, 1);
			scoreTxt.setText(LK.getScore().toString());
			// Check if all bricks are destroyed and initiate level transition
			if (bricks.length === 0) {
				transitionToNextLevel();
			}
		}
	}
	// Ball out of bounds
	if (ball.y > game.height) {
		ball.x = game.width / 2;
		ball.y = game.height - 150;
		ball.velocity = {
			x: 5,
			y: -5
		};
	}
	// Function to handle level transition
	function transitionToNextLevel() {
		// Animate paddle and ball soaring upwards
		var levelTransitionDuration = 2000; // Duration of the transition in milliseconds
		var levelTransitionTick = 0;
		var levelTransitionInterval = LK.setInterval(function () {
			paddle.y -= 5;
			ball.y -= 5;
			levelTransitionTick += 16.6667; // Approximately 1 tick at 60FPS
			if (levelTransitionTick >= levelTransitionDuration) {
				LK.clearInterval(levelTransitionInterval);
				storyManager.displayChoices();
			}
		}, 16.6667);
	}
	// Function to start the next level
	function startNextLevel() {
		// Reset paddle and ball position
		paddle.x = game.width / 2;
		paddle.y = game.height - 100;
		ball.x = game.width / 2;
		ball.y = game.height - 150;
		// TODO: Increase difficulty for the next level
	}
});