#include "GL/Menu.h"
#include "GL/Font.h"
#include "GL/ImmediateMode.h"
#include "GL/Program.h"


BEGIN_NAMESPACE


static bool IsPointInsideRect(int x, int y, int x1, int y1, int x2, int y2)
{
	if (x < x1 || x > x2 || y < y1 || y > y2)
		return false;
	return true;
}


Menu::Menu()
{
	selectedMenu = -1;
	toggled = 0;
}


void Menu::draw()
{
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glTranslatef(-g_xOff, -g_yOff, 0.0);
	glScalef(1.0f/g_zoomFactor,1.0f/g_zoomFactor, 0.0f);
	float oldZoom = g_zoomFactor;
	g_zoomFactor = 1.0;

	const int shadowWidth = 5;
	const int itemHeight = 20;
	const int radius = 9;
		
	glDrawFillRect(0.0f, 0.0f, (float)w, (float)itemHeight, 0x60202020);

	float tw = 10;
	float menuOff = 0;
	int fontSize = 15;
	for (size_t i = 0; i < menus.size(); ++i) {
		float curTw = textWidth(menus[i].first.c_str()) * fontSize;
		if ( IsPointInsideRect(mouseX, mouseY, int(tw - 10.0f), 0, int(tw + curTw + 10.0f), itemHeight) ) {
			if ( lastMouseButtons != mouseButtons && !mouseButtons )
				toggled = (toggled) ? 0 : 1;
			selectedMenu = ( lastMouseButtons != mouseButtons && !toggled ) ? -1 : (( toggled || selectedMenu != -1 ) ? i : selectedMenu );
		}
		if ( i == selectedMenu ) {
			menuOff = tw;
			glDrawFillRect(tw-10.0f, 0.0f, tw + curTw + 10.0f, (float)itemHeight, 0xc0ff901a);//0xc0c0d0ff);
			glDrawText(int(tw), 15, menus[i].first.c_str(), 0xff000000, 0xffffffff, fontSize, false);
		} else {
			glDrawText(int(tw), 15, menus[i].first.c_str(), 0xffffffff, 0xff000000, fontSize, true);
		}
		tw += curTw;
		tw += 20.0f;
	}

	if ( selectedMenu != -1 )
	{
		x += int(menuOff - 10.0f);
		int h = 6;
		for (unsigned i = 0; i < menus[selectedMenu].second.size(); ++i) {
			if ( menus[selectedMenu].second[i] == "**********" )
				h += 12;
			else
				h += itemHeight;
		}
		w = 165; // should calculate this based on textwidth of items
		for (int i = 0; i < shadowWidth; ++i)
			glDrawFillRoundedRect((float)x-i, (float)y+shadowWidth-i, (float)x+w+i, (float)y+h+i, (float)radius+i*2, 0x10000000);
		glDrawFillRoundedRect((float)x, (float)y, (float)x+w, (float)y+h, (float)radius, 0x60202020);
		glDrawRoundedRect((float)x, (float)y, (float)x+w, (float)y+h, (float)radius, 0x80101010);

		int runningY = 0;
		for (unsigned i = 0; i < menus[selectedMenu].second.size(); ++i) {
			int yo = 12;
			if ( menus[selectedMenu].second[i] == "**********" )
			{
				glLineWidth(0.0025f);
				glDrawFillRect((float)x+1, (float)y-yo +6+ 17+runningY , (float)x+w-1, (float)y-yo + 6+17+runningY  + 1, 0xb0808080);
				//glDrawLine(x+1, y+1-yo +6+ 17+runningY - 2, x+w-2, y+1-yo + 6+17+runningY + 2, 0xb0000000);
				runningY += 12;
			}
			else
			{
				if ( mouseY >= y -yo+ 17+runningY && mouseY <= y -yo+17+17+runningY ) {				
					glDrawFillRect((float)x+2, (float)y-yo + 17+runningY - 2, (float)x+w-2, (float)y-yo + 17+17+runningY + 2, 0xc0ff901a);//0xc0c0d0ff);
					glDrawText(x + radius, y + 17+runningY, menus[selectedMenu].second[i].c_str(), 0xff000000, 0xffffffff, fontSize, false);
				} else {
					glDrawText(x + radius, y + 17+runningY, menus[selectedMenu].second[i].c_str(), 0xffffffff, 0xff000000, fontSize, true);
				}
				runningY += itemHeight;
			}
		}
		//glDrawShadowText(x + radius, y + i*itemHeight, menuItems[i].data());
	}

	g_zoomFactor = oldZoom;
	glPopMatrix();
	lastMouseButtons = mouseButtons;
}


void Menu::initFromStringList(const char* a_strings[], int a_stringCount)
{
	for (int i = 0; i < a_stringCount; ++i)
	{
		if ( strncmp("___", a_strings[i], 3) != 0 )
		{
			menus.back().second.push_back(a_strings[i]);
		}
		else
		{
			std::string menuText(a_strings[i] + 3);
			menuText = menuText.substr(0, menuText.find('_'));
			menus.push_back(std::pair<std::string, std::vector<std::string> >(menuText, std::vector<std::string>() ));
		}
	}
}


END_NAMESPACE
