// Dialogue template for LoZ: The Shadow Gate
// Created by Arkia
// 10/31/2011

// Define some things
dColors = Array({"r" : 1.0, "g" : 1.0, "b" : 1.0},	// /0 - White
				{"r" : 0.6, "g" : 0.6, "b" : 0.6},	// /1 - Light Gray
				{"r" : 0.3, "g" : 0.3, "b" : 0.3},	// /2 - Dark Gray
				{"r" : 0.0, "g" : 0.0, "b" : 0.0},	// /3 - Black
				{"r" : 0.9, "g" : 0.0, "b" : 0.0},	// /4 - Red
				{"r" : 0.0, "g" : 0.9, "b" : 0.0},	// /5 - Green
				{"r" : 0.0, "g" : 0.0, "b" : 0.9},	// /6 - Blue
				{"r" : 0.8, "g" : 0.8, "b" : 0.0},	// /7 - Yellow
				{"r" : 0.0, "g" : 0.8, "b" : 0.8},	// /8 - Cyan
				{"r" : 0.8, "g" : 0.0, "b" : 0.8});	// /9 - Magenta
dEscapeCodes = "n0123456789";

// Create dialogue UI elements
dH = ui.height;
dW = ui.width;
dContainer = UIContainer(dW/2, dH);
dBoxContainer = UIContainer(0, -96);
dImageContainer = UIContainer(0, -224);
dTextContainer = UIContainer(-120, 2);
dImage = UISprite("/none.png", -64, 0, 128, 128, 0, 0);
dBox = UISprite("/hud/chat.png", -128, 0, 256, 64, 0, 0);
dIcon = UISprite("/hud/chat.png", -8, 56, 16, 16, 0, 64);
dCursor = UISprite("/hud/chat.png", 0, 0, 8, 8, 240, 248);


dContainer.add(dImageContainer);
dImageContainer.add(dImage);
dBoxContainer.add(dBox);
dBoxContainer.add(dIcon);
dBoxContainer.add(dTextContainer);
dContainer.add(dBoxContainer);
dContainer.removeFromScreen();
dCursor.removeFromScreen();

// Function to display a dialogue message
// message is the message that will be displayed
// end(optional) specifies whether to show the end of message button or the continue message button
// image(optional) is the image that will be displayed above the dialogue box
// The color of the message can be changed by including a "/" followed by a number 0-9
// which corresponds with the colors defined in the dColors array.
// This will make all text past that point whatever color you chose.
// So a message string like "/4Test" would display "Test" in red text
dShowMessage = function(message, end, image)
{
	dTextContainer.clear();

	image = typeof(image) != "undefined" ? image : "/none.png";
	
	dImage.texture = image;
	dIcon.u = (typeof(end) == "undefined" ? 32 : (end ? 16 : 0))
	
	var mString = "";
	var mIndex = 0;
	var mPos = {"x" : 0, "y" : 0};
	var color = 0;
	
	for (var i=0; i<message.length; i++)
	{
		if (message.charAt(i) != "/")
			mString += message.charAt(i);
		else
		{
			if (dEscapeCodes.indexOf(message.charAt(i+1)) != -1)
			{
				var text = new UILabel(mString, mPos.x, mPos.y)
				text.red = dColors[color].r;
				text.green = dColors[color].g;
				text.blue = dColors[color].b;
				dTextContainer.add(text);
				
				if (message.charAt(i+1) == 'n')
				{
					mPos.x = 0;
					mPos.y += 8;
				}
				else
				{
					mPos.x += ui.getStringWidth(mString);
					color = message.charAt(i+1);
				}
				
				mString = "";
			}
			else
				mString += message.charAt(i+1);
				
			i += 1;
		}
	}
	
	var text = new UILabel(mString, mPos.x, mPos.y)
	text.red = dColors[color].r;
	text.green = dColors[color].g;
	text.blue = dColors[color].b;
	dTextContainer.add(text);
	
	dContainer.addToScreen();
};

// Creates a dialogue message that lets the player pick from an array of options before proceeding.
dShowDecision = function(message, options, image)
{
	dTextContainer.clear();

	image = typeof(image) != "undefined" ? image : "/none.png";
	dImage.texture = image;
	
	var mString = "";
	var mIndex = 0;
	var mPos = {"x" : 0, "y" : 0};
	var color = 0;
	var cTop = 6 - options.length;
	var oText = "";
	var oIndex = 0;
	var debugFlag = true;
	
	for (var i=0; i<message.length; i++)
	{
		if (message.charAt(i) != "/")
			mString += message.charAt(i);
		else
		{
			if (dEscapeCodes.indexOf(message.charAt(i+1)) != -1)
			{
				var text = new UILabel(mString, mPos.x, mPos.y)
				text.red = dColors[color].r;
				text.green = dColors[color].g;
				text.blue = dColors[color].b;
				dTextContainer.add(text);
				
				if (message.charAt(i+1) == 'n')
				{
					mPos.x = 0;
					mPos.y += 8;
				}
				else
				{
					mPos.x += ui.getStringWidth(mString);
					color = message.charAt(i+1);
				}
				
				mString = "";
			}
			else
				mString += message.charAt(i+1);
				
			i += 1;
		}
	}
	
	var text = new UILabel(mString, mPos.x, mPos.y);
	text.red = dColors[color].r;
	text.green = dColors[color].g;
	text.blue = dColors[color].b;
	dTextContainer.add(text);
	
	for (var i=0; i<options.length; i++)
	{
		color = 0;
		mPos.x = 8;
		mPos.y = (cTop + i) * 8;
		oText = options[i].text;
		oString = "";
		
		for (var j=0; j<oText.length; j++)
		{
			if (oText.charAt(j) != "/")
				oString += oText.charAt(j);
			else
			{
				if (dEscapeCodes.indexOf(oText.charAt(j+1)) != -1)
				{
					var text = new UILabel(oString, mPos.x, mPos.y);
					text.red = dColors[color].r;
					text.green = dColors[color].g;
					text.blue = dColors[color].b;
					dTextContainer.add(text);
					
					if (oText.charAt(j+1) != 'n')
					{
						mPos.x += ui.getStringWidth(oString);
						color = oText.charAt(j+1);
					}
					
					oString = "";
				}
				else
					oString += oText.charAt(j+1);
					
				j += 1;
			}
		}
		
		var text = new UILabel(oString, mPos.x, mPos.y)
		text.red = dColors[color].r;
		text.green = dColors[color].g;
		text.blue = dColors[color].b;
		dTextContainer.add(text);
		
	}
	
	dCursor.u = 248;
	dCursor.y = cTop * 8;
	dTextContainer.add(dCursor);
	
	dContainer.addToScreen();
	
	time.sleep(0.1);
	
	while (!keyboard.isKeyDown(28))
	{
		if (keyboard.isKeyDown(200))
		{
			sound.playSoundUI("sound.cursormove");
			oIndex -= 1;
			if (oIndex < 0)
				oIndex = 0;
		}
		if (keyboard.isKeyDown(208))
		{
			sound.playSoundUI("sound.cursormove");
			oIndex += 1;
			if (oIndex >= options.length)
				oIndex = options.length - 1;
		}
		
		dCursor.y = (cTop + oIndex) * 8;
		time.sleep(0.01);
	}
	
	return options[oIndex].code;
};

// Function to remove a dialogue message
dClearMessage = function()
{
	dTextContainer.clear();
	dContainer.removeFromScreen();
};

// Keeps the UI centered on the screen. Should be called in onUpdate
dUpdateUI = function()
{
	dH = ui.height;
	dW = ui.width;
	dContainer.x = dW/2;
	dContainer.y = dH;
};

// Creates a color used to color messages. Values should be between 0.0 and 1.0
dColor = function(r, g, b)
{
	return {"r" : r, "g" : g, "b" : b};
};

// Creates a dialogue option with the given text. 'code' is what will be returned if the player chooses this option
dOption = function(text, code)
{
	return {"text" : text, "code" : code};
};