#include "VMenu.h"
#include "V2Ddraw.h"
#include "VTexture.h"

VMenu::VMenu(void){
	shMenu = true;
	hMenu = false;
	lFile = false;
	mPos = 0;
	actPos = 0;
	this->tex = new VTexture("manual.tga");
}
	
void VMenu::draw(void){ 
	glUseProgram(0);
	GLenum err = glGetError();
	if (err != GL_NO_ERROR)
		cout << "GL Error: " << gluErrorString(err) << endl;
	
	GLint matrixMode;
	GLboolean lightingOn;
	GLboolean depthOn;
	char *ch;
	shMenu = true;
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
	//Display Menu Entrys 
	
	depthOn = glIsEnabled(GL_DEPTH_TEST);
	if (depthOn) glDisable(GL_DEPTH_TEST);
	glEnable (GL_BLEND);
	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	
	lightingOn= glIsEnabled(GL_LIGHTING);        // lighting on?
	if (lightingOn) glDisable(GL_LIGHTING);
	
	glGetIntegerv(GL_MATRIX_MODE, &matrixMode);  // matrix mode? 
	
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(0.0, 1.0, 0.0, 1.0);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glPushAttrib(GL_COLOR_BUFFER_BIT);      //  save current colour 
	
	//Draw The Background 
	glColor4f(0.0f,0.0f,0.0f,0.25f);    // Black 
	DrawBox(0.0f,0.0f,-1.0f,1.0f,1.0f);    // X,Y Width Height 
	
	if (hMenu){
		mPos = 1;
		//Draw Background Box
		glColor4f(1.0f,1.0f,1.0f,0.9f);    // White Color 
		V2Ddraw::drawTexBox(this->tex, 0.0f,0.0f,1.0f,1.0f);    // X,Y Width Height 
		
	}
	else{
		//Draw Background Box
		glColor4f(0.0f,0.3f,0.6f,0.9f);    // Blueish Color 
		DrawBox(0.25f,0.28f,-1.0f,0.25f,1.0f);    // X,Y Width Height 
	
	
		//Draw The TitleBar 
		glColor4f(0.79f,0.8f,1.0f,0.5f);    // White with Blending 
		DrawBox(0.25f,0.59f,-1.0f,0.30f,0.04f);    // X,Y Width Height  
		

		//Draw Selected Item 
		glColor4f(1.0f,1.0f,1.0f,0.2f);    // White Color 
		DrawBox(0.25f,(0.445f-(actPos*0.05)),0.0f,0.25f,0.03f);    // Draws the selector bar
	}
	
	//Set Menu Entrys 
	
	char Entry0[40],Entry1[40],Entry2[40],Entry3[40],Entry4[40],Entry5[40],Entry6[40],Entry7[40];
	
	
	if (hMenu){
		sprintf(Entry0," ");
		sprintf(Entry1," ");
		sprintf(Entry2," "); 
		sprintf(Entry3," ");
		sprintf(Entry4," ");
		sprintf(Entry5," "); 
		sprintf(Entry6," ");
		sprintf(Entry7," ");
	}
	else if (lFile){
		sprintf(Entry0," Choose Dataset");
		sprintf(Entry1," Lobster");
		sprintf(Entry2," Head");
		sprintf(Entry3," Beetle");
	}
	else {
		sprintf(Entry0," VisLU");
		sprintf(Entry1," Load File");
		sprintf(Entry2," Help"); 
		sprintf(Entry3," Exit");
	}
	
	//Draw The Title 
	glColor3f(1.0, 1.0, 1.0);
	glRasterPos3f(0.252, 0.60, 0.0);							// Draws the title
	for(ch= Entry0; *ch; ch++) {							// so that it is in the title bar, and so its white
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, (int)*ch);
	}
	
	glRasterPos3f(0.26, 0.45, 1.0);
	for(ch= Entry1; *ch; ch++) {
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, (int)*ch);
	}
	
	glRasterPos3f(0.26, 0.40, 1.0);
	for(ch= Entry2; *ch; ch++) {
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, (int)*ch);
	}
	if (!hMenu){
	glRasterPos3f(0.26, 0.35, 1.0);
	for(ch= Entry3; *ch; ch++) {
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, (int)*ch);

	}
	}
	
	if (hMenu){
		glRasterPos3f(0.26, 0.30, 1.0);
		for(ch= Entry4; *ch; ch++) {
			glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, (int)*ch);
		}
		glRasterPos3f(0.26, 0.25, 1.0);
		for(ch= Entry5; *ch; ch++) {
			glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, (int)*ch);
		}
		glRasterPos3f(0.26, 0.20, 1.0);
		for(ch= Entry6; *ch; ch++) {
			glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, (int)*ch);
		}
		glRasterPos3f(0.26, 0.10, 1.0);
		for(ch= Entry7; *ch; ch++) {
			glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, (int)*ch);
		}
	}
	
	glRasterPos3f(0.8, 0.05, 1.0);
	for(ch= "a visualization by Klepp/Moreau"; *ch; ch++) {
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, (int)*ch);
	}
	glPopAttrib();
	glPopMatrix();
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(matrixMode);
	if (lightingOn) glEnable(GL_LIGHTING);
	if (depthOn) glEnable(GL_DEPTH_TEST);
	glDisable (GL_BLEND); 
}
	
void VMenu::DrawBox(GLfloat x, GLfloat y, GLfloat z, GLfloat width, GLfloat height)
{
	glBegin(GL_QUADS);
	glVertex3f(x,y,z);
	glVertex3f(x+width,y,z);
	glVertex3f(x+width,y+height,z);
	glVertex3f(x,y+height,z);
	glEnd();
}
	
void VMenu::setMouse(float _x, float _y){
	this->mouseX = _x;
	this->mouseY = _y;

	if (this->lFile){
		if ( mouseY > 0.20){
			if(mouseY < 1-0.45)
				actPos = 0;
			else if(mouseY < 1-0.40)
				actPos = 1;
			else if(mouseY < 1-0.35)
				actPos = 2;
		}
	}
	else if (this->hMenu){
		actPos = 7;
	}
	else if (this->shMenu){
		if ( mouseY > 0.20){
			if(mouseY < 1-0.45)
				actPos = 0;
			else if(mouseY < 1-0.40)
				actPos = 1;
			else if(mouseY < 1-0.35)
				actPos = 2;
		}
	}

}

void VMenu::click(void){
	this->mPos = this->actPos;
	this->actPos = 0;
}

void VMenu::showMenu(bool _shMenu){
	shMenu = _shMenu;
}

void VMenu::helpMenu(bool _helpMenu){
	hMenu = _helpMenu;
}

void VMenu::loadFile(bool _loadFile){
	lFile = _loadFile;
}

bool VMenu::showMenu(void){
	return shMenu;
}

bool VMenu::helpMenu(void){
	return hMenu;
}

bool VMenu::loadFile(void){
	return lFile;
}

int VMenu::menuPos (void){
	return mPos;
}

void VMenu::menuPos(int _pos){
	mPos = _pos;
}

void VMenu::incmenuPos(void){
	mPos++;
}

void VMenu::decmenuPos(void){
	mPos--;
}