// requires Vector2D
// requires Actor
// requires TextParticle

function Stage(width, height, canvas) {
	this.width = width;
	this.height = height;
	this.viewX = 0;
	this.canvas = canvas;
	this.gravity = 0;
	this.terminalVelocity = 0;
	this.bounds = [];
	this.staticObjects = [];
	this.textParticles = [];
	this.mapOffset = 0;
}

Stage.prototype.getViewX = function() {
	return parseInt(this.viewX - this.mapOffset);
}

Stage.prototype.reachedLeftLimit = function() {
	return this.viewX <= 0;
}

Stage.prototype.reachedRightLimit = function() {
	return parseInt(this.viewX + this.canvas.width) >= this.width;
}

Stage.prototype.actorIsPastHalfOfViewport = function(actor, canvas) {
	return Math.floor(actor.position.x + (actor.width / 2)) > Math.floor(canvas.width / 2);
}

Stage.prototype.actorIsBeforeHalfOfViewport = function(actor, canvas) {
	return Math.floor(actor.position.x + (actor.width / 2)) < Math.floor(canvas.width / 2);
}

Stage.prototype.doesMoveStageLeft = function(actor, canvas, velocity_x) {
	return this.actorIsPastHalfOfViewport(actor, canvas) && velocity_x > 0 && !this.reachedRightLimit();
}

Stage.prototype.doesMoveStageRight = function(actor, canvas, velocity_x) {
	return this.actorIsBeforeHalfOfViewport(actor, canvas) && velocity_x < 0 && !this.reachedLeftLimit();
}

Stage.prototype.move = function(value) {
	this.viewX += parseInt(value);
	for (var i = 0; i < this.bounds.length; i++) {
		this.bounds[i].stepPositionX(parseInt(-value));
	}
}

Stage.prototype.loadAndParseStageData = function(data) {
	for (dataType in data) {
		switch (dataType) {
		case 'bounds':
			for (idx in data[dataType]) {
				this.bounds.push(new GenericBlock(data[dataType][idx]));
			}
			break;
		}
	}
}

Stage.prototype.draw = function(ctx, framerate) {	
	// draw the text labels for the objects
	for (var i = 0; i < this.textParticles.length; i++) {
		if (this.textParticles[i].lifetime > 0) {
			var f = 1000/framerate;
			this.textParticles[i].lifetime -= f;
			var ty = (this.textParticles[i].lifetime / f) - 40;
			ctx.fillStyle = this.textParticles[i].color;
			ctx.font = this.textParticles[i].font;
			ctx.fillText(this.textParticles[i].text, this.textParticles[i].getPosition().x + 10, this.textParticles[i].getPosition().y + ty);
		} else {
			this.textParticles.splice(i, 1);
		}
	}
}
