#include "VColorChooser.h"
#include "V2Ddraw.h"
#include <GL/glew.h>
#include <GL/glut.h>

VColorChooser::VColorChooser(float* alpha,int fx,int fy,int fwidth, int fheight)
{
	this->tex = new VTexture("palette.tga");
	this->width = 144;
	this->height = 72;
	this->x = 0;
	this->y = 0;
	this->active = false;
	this->alpha = alpha;
	this->func_x = fx;
	this->func_y = fy;
	this->func_width = fwidth;
	this->func_height = fheight;
	this->position = 0;
}

void VColorChooser::draw() {
	int x;
	int y;
	switch(this->position) {
		case 0: x = this->x;
				y = this->y;
				break;
		case 1: x = this->x - this->width;
				y = this->y;
				break;
		case 2: x = this->x;
				y = this->y - this->height;
				break;
		case 3: x = this->x - this->width;
				y = this->y - this->height;
				break;
	}
	
	glEnable(GL_TEXTURE_2D);
	//glDisable(GL_BLEND);
	//glBlendFunc(GL_SRC_ALPHA,GL_DST_ALPHA);
	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glColor4f(1.0,1.0,1.0,*(this->alpha));
	this->tex->bind();
	glBegin(GL_QUADS);
		glTexCoord2f(0,0);
		glVertex2f(x,y);
		glTexCoord2f(0.75,0);
		glVertex2f(x+this->width,y);
		glTexCoord2f(0.75,0.75);
		glVertex2f(x+this->width,y+this->height);
		glTexCoord2f(0,0.75);
		glVertex2f(x,y+this->height);
	glEnd();
	glDisable(GL_TEXTURE_2D);
	//glEnable(GL_BLEND);
	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

bool VColorChooser::isInside(int xm, int ym) {
	bool inside = false;
	switch(this->position) {
		case 0: inside = xm > this->x && xm < this->x + this->width && ym > this->y && ym < this->y + this->height;
				break;
		case 1: inside = xm > this->x - this->width && xm < this->x && ym > this->y && ym < this->y + this->height;
				break;
		case 2: inside = xm > this->x && xm < this->x + this->width && ym > this->y - this->height && ym < this->y;
				break;
		case 3: inside = xm > this->x - this->width && xm < this->x && ym > this->y - this->height && ym < this->y;
				break;
	}
	return inside;
}

bool VColorChooser::isActive() {
	return this->active;
}

void VColorChooser::setActive(bool active,int x,int y) {
	this->active = active;
	this->x = x;
	this->y = y;

	int breite = this->func_width/2;
	int hoehe = this->func_height/2;
	//links oben
	if(this->x > this->func_x && this->x < this->func_x + breite && this->y > this->func_y && this->y < this->func_y + hoehe) {
		this->position = 0;
	//rechts oben
	} else if(this->x > this->func_x + breite && this->x < this->func_x + this->func_width && this->y > this->func_y && this->y < this->func_y + hoehe) {
		this->position = 1;
	//links unten
	} else if(this->x > this->func_x && this->x < this->func_x + breite && this->y > this->func_y + hoehe && this->y < this->func_y + this->func_height) {
		this->position = 2;
	//rechts unten
	} else if(this->x > this->func_x + breite && this->x < this->func_x + this->func_width && this->y > this->func_y + hoehe && this->y < this->func_y + this->func_height) {
		this->position = 3;
	}
}

void VColorChooser::getColor(int x, int y, float * pixels) {
	GLint viewport[4];
	glGetIntegerv(GL_VIEWPORT,viewport);
	glReadPixels(x,viewport[3]-y,1,1,GL_RGB,GL_FLOAT,pixels);
}

VColorChooser::~VColorChooser(void)
{
	delete this->tex;
}
