#include "VRenderMode.h"
#include <iostream>
#include "V2Ddraw.h"
#include "corona.h"
#include "VTexture.h"

using namespace std;
using namespace corona;

VRenderMode::VRenderMode(int* width, int* height, unsigned int* fps, int* _actPos):
	GUIelement(width,height,fps)
{
	float * col = new float[3];
	col[0] = col[1] = 1.0;
	col[2] = 0.0;
	this->width = (int)(0.15*(*this->wwidth));
	this->height = (int)(0.2*(*this->wheight));
	this->x = (int) *(this->wwidth)*0.78;
	this->y = (int) *(this->wheight)*0.02;
	this->bg = new float[4];
	this->bg[0] = 1.0f;
	this->bg[1] = 1.0f;
	this->bg[2] = 1.0f;
	this->bg[3] = 1.0f;
	pos = 1;
	this->actPos = _actPos;
	*actPos = 1;
	this->tex = new VTexture("transferfunc.tga");
	this->dragging = false;
}

VRenderMode::~VRenderMode(void)
{
	//delete exit;
	delete bg;
	delete tex;
}

void VRenderMode::motion(int xm, int ym) {
	//exit->inside(xm,ym);
	if ((ym-this->y) < this->y+(this->height/4.0f)*0.9){
		pos = 1;
	}
	else if((ym-this->y) < this->y+(this->height/4.0f)*2*0.9){
		pos = 2;
	}
	else if((ym-this->y) < this->y+(this->height/4.0f)*3*0.9){
		pos = 3;
	}
	else if((ym-this->y) < this->y+(this->height/4.0f)*4*0.9){
		pos = 4;
	}
}

void VRenderMode::render() {
	if(!this->active && this->blend) { //einblenden
		float frames = (float)*(this->fps);
		this->alpha+=sin((1.3/frames));
		this->bg[3] = this->alpha;
		this->draw();
		if(this->alpha >= 0.6) {
			this->alpha = 1;
			this->setActive(true);
		}
	}
	if(this->active && !this->blend) { //ausblenden
		float frames = (float)*(this->fps);
		this->alpha-=sin((1.3/frames));
		this->bg[3] = this->alpha;
		this->draw();
		if(this->alpha <= 0) {
			this->alpha = 0;
			this->setActive(false);
		}

	}
	if(this->active && this->blend) { //aktiv
		this->draw();
	}
}

void VRenderMode::draw() {
	glColor4fv(bg);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
	glEnable(GL_TEXTURE_2D);
	this->tex->bind();
	V2Ddraw::drawBox(this->x,this->y,this->width,this->height, 1);
	glDisable(GL_TEXTURE_2D);
	
	char Entry0[40],Entry1[40],Entry2[40],Entry3[40];
	sprintf(Entry0," Levoy");
	sprintf(Entry1," MIP");
	sprintf(Entry2," First Hit"); 
	sprintf(Entry3," Debug");
	if (pos){
	//Draw Selected Item 
	glColor4f(1.0f,1.0f,1.0f,this->alpha*0.1f);    // White Color 
	V2Ddraw::drawBox(this->x+10, (this->y)+(this->height/4.0f)*this->pos*0.9-17.0f,this->width*0.8f,20.0f);
	}

	glColor4f(1.0f,1.0f,1.0f,this->alpha*0.3f);    // White Color 
	V2Ddraw::drawBox(this->x+10, (this->y)+(this->height/4.0f)* *(this->actPos)*0.9-17.0f,this->width*0.8f,20.0f);

	char *ch;
	glColor4f(1.0, 1.0, 1.0, this->alpha);
	glRasterPos3f(this->x+10, this->y+(this->height/4.0f)*0.9, 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(this->x+10, this->y+(this->height/4.0f)*2*0.9, 0.0);	
	for(ch= Entry1; *ch; ch++) {
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, (int)*ch);
	}
	
	glRasterPos3f(this->x+10, this->y+(this->height/4.0f)*3*0.9, 0.0);	
	for(ch= Entry2; *ch; ch++) {
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, (int)*ch);
	}

	glRasterPos3f(this->x+10, this->y+(this->height/4.0f)*4*0.9, 0.0);	
	for(ch= Entry3; *ch; ch++) {
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, (int)*ch);
	}
	//exit->draw();
}

void VRenderMode::released(int xm, int ym) {
	if(!this->dragging) { //click
		/*if(exit->isInside(xm,ym))
			this->setBlend(false);
		else {*/
			if ((ym-this->y) < this->y+(this->height/4.0f)*0.9){
				*(this->actPos) = 1;
				this->setBlend(false);
			}
			else if((ym-this->y) < this->y+(this->height/4.0f)*2*0.9){
				*(this->actPos) = 2;
				this->setBlend(false);
			}
			else if((ym-this->y) < this->y+(this->height/4.0f)*3*0.9){
				*(this->actPos) = 3;
				this->setBlend(false);
			}
			else if((ym-this->y) < this->y+(this->height/4.0f)*4*0.9){
				*(this->actPos) = 4;
				this->setBlend(false);
			}
		//}
	}
	this->dragging = false;
}

void VRenderMode::drag(int x, int y) {
	this->dragging = true;
}

bool VRenderMode::isInside(int xm, int ym) {
	return xm > this->x && xm < this->x+this->width && ym > this->y && ym < this->y+this->width;
}