/*
--------------------------------------------------------------------------------
text pop-up
--------------------------------------------------------------------------------
used for dialogue + in-game text
if we end up with multiple variants of this, they'll all be here
*/

function TextBox(x, y, string) {
	this.string = string;
	this.startX = x;
	this.startY = y - 8;
	this.remove = false;
	this.timer = 0;
	
	var lineCount = Math.ceil(this.string.length / 32); 
	
	this.endLeft = 8;
	this.endTop = y - 16 * (lineCount + 2);
	this.endWidth = 256 - 16;
	this.endHeight = 16 * (lineCount + 1);
	
	this.animationTime = 16;
	
	this.fading = false;
	
	this.update = function() {
		// check key input?
		
		// gradually appear
		if (this.fading == false) {
			if (this.timer < this.animationTime)
				this.timer ++;
		}

		// gradually shrink and disappear
		else if (this.fading) {
			this.timer --;
			if (this.timer <= 0)
				this.remove = true;
		}
		
		this.blend = Math.min(1.0, this.timer / this.animationTime);
	}
	
	this.draw = drawSimple;
	
}


function drawSimple() {
	var lineCount = Math.ceil(this.string.length / this.animationTime); 
	var border = 1;

	// transition from start shape to end shape

	// start shape
	var startLeft = this.startX;
	var startTop = this.startY;
	var startWidth = 0;
	var startHeight = 0;
	
	var left = lerp(startLeft, this.endLeft, this.blend);
	var top = lerp(startTop, this.endTop, this.blend);
	var width = lerp(startWidth, this.endWidth, this.blend);
	var height = lerp(startHeight, this.endHeight, this.blend);
	
	offscreenContext.fillStyle = "#ffffff";
	offscreenContext.fillRect(left, top, width, height);
	offscreenContext.fillStyle = "#000000";
	offscreenContext.fillRect(left + border, top + border, width - 2 * border, height - 2 * border);
	
	// draw stem
	drawSpriteToTarget(168, this.startX, top + height - 1, spriteCanvas, offscreenContext);
	
	if (this.blend > 0.999)
		drawStringWrapped(this.endLeft + 16, this.endTop + 8, this.endLeft + this.endWidth - 16, this.string);
}


// ADD: various sorts of boxes such as dialogue boxes, signboards, etc.


// with line wrap, but no centering
// it takes a context as an argument so you can draw to targets other than the world canvas
function drawStringToTargetWrapped(string, x, y, wrapWidth, target) {
	var lineStartIndex = 0;
	var lineTrimLength = wrapWidth;
	var done = false;
	var i = 0;
	var lineCount = 0;
	var lineHeight = 8;
	while (!done) {
		lineTrimLength = wrapWidth; // start assuming each line will have the full wrap width
		// step forward one if we're starting on a space
		if (string[lineStartIndex] == " ")
			lineStartIndex ++;
		// special case for really long words (also kicks in if we have a single word line at the end)
		if (lineStartIndex + lineTrimLength >= string.length)
			lineTrimLength = string.length - lineStartIndex;
		else {
			// if we find we've hit the middle of a word, start stepping backward
			// find a space and trim there
			var endCharacter = string[lineStartIndex + lineTrimLength - 1];
			while (endCharacter != " ") {
				endCharacter = string[lineStartIndex + lineTrimLength - 1];
				lineTrimLength --;
			}
		}
		var currentString = string.substring(lineStartIndex, lineStartIndex + lineTrimLength);
		lineStartIndex += lineTrimLength;
		if ((currentString != " ") && (currentString.length > 0)) {
			drawString(currentString,x, y + 16 + lineHeight * i);
			lineCount ++;
		}
		i ++;
		if ((i - 1) * wrapWidth >= string.length)
			done = true;
	}
	return lineCount;
}


function drawStringWrapped(string, x, y, endX) {
	return drawStringToTargetWrapped(string, x, y, endX, offscreenContext);
}


function drawStringRotated(string, x, y, rotation) {
	// this doesn't work because this doesn't use drawimage
	// offscreenContext.save();
	// offscreenContext.rotate(Math.random() * 360);
	drawString(x, y, string);
	//offscreenContext.restore();
}


// simple for now, no line wrap, no centering, no length estimate
// it takes a context as an argument so you can draw to targets other than the world canvas
function drawStringToTarget(x, y, string, target) {
	//target.save();
	//target.globalCompositeOperation = "lighter";
	var currentIndex;
	var startX = x;
	var drawX = x;
	var drawY = y;
	var characterWidth = 7;
	var lineHeight = 8;
	var charactersThisLine = 0;
	var charactersAcross = 16;

	//string = string.toUpperCase();

	for (var i = 0; i < string.length; i ++) {
		currentIndex = string.charCodeAt(i)
		//drawCharacterSimple(currentIndex, drawX, drawY);
		drawCharacterIndexed(currentIndex, drawX, drawY);
		drawX += characterWidth;
		charactersThisLine ++;
	}
	//target.restore();
}


// simpler version which uses the main world canvas by default
function drawString(string, x, y) {
	drawStringToTarget(x, y, string, offscreenContext);
}

// character draw functions


// draws an rgb sprite using drawImage
function drawCharacterSimple(index, x, y) {
	drawSprite(index, 8, x, y);
}


function drawCharacterIndexed(index, x, y) {
 	if (index != 32) { // skip space
 		index = index - 65 + 120; //96; //104;
	 	// drawIndexedSpriteToRGB(spriteImageIndexed, index, newPalette, offscreenImageData, Math.floor(x - 4), Math.floor(y - 4));
		drawIndexedSpriteToRGBClipped(spriteImageIndexed, index, palette, offscreenImageData, Math.floor(x - 4), Math.floor(y - 4));
	}
}