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

using namespace std;
using namespace corona;

VSliceElement::VSliceElement(int* width, int* height, unsigned int* fps, int* _sliceNo):
	GUIelement(width,height,fps)
{
	float * col = new float[3];
	col[0] = col[1] = 1.0;
	col[2] = 0.0;
	this->width = (int)(0.2*(*this->wwidth));
	this->height = (int)(0.2*(*this->wheight));
	this->x = (int) *(this->wwidth)*0.8;
	this->y = (int) *(this->wheight)*0.8;
	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;
	this->exit = new VExitBox(this->x+this->width-15,this->y+5,10,bg,col,&(this->alpha));
	this->slider = new VSlider(0.5,this->x+25,this->y+this->height-25,this->x-5,this->y+35,&(this->alpha));

	this->sliceNo = new int();
	this->sliceNo = _sliceNo;
	this->count = 0;
	this->slice = false;

	this->tex = new VTexture("slices.png");
	this->dragging = false;
}

VSliceElement::~VSliceElement(void)
{
	delete exit;
	delete slider;
	delete bg;
	delete sliceNo;
}

void VSliceElement::motion(int xm, int ym) {
	exit->inside(xm,ym);
}

void VSliceElement::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 >= 1) {
			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 VSliceElement::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);
	exit->draw();

	if (!(count%15)){
		*(this->sliceNo) = this->slice;
	}
	if (count > 15){
		count = 1;
	}
	else {
		this->count++;
	}
	slider->render();
}

void VSliceElement::released(int xm, int ym) {
	this->slider->released(xm,ym);
	if(!this->dragging) {
		if(exit->isInside(xm,ym))
			this->setBlend(false);
		else {
			if(((xm-this->x) > 0) && ((xm-this->x) < this->width)){
				if (((ym-this->y) > 0) && ((ym-this->y) < this->height/2)){
					float * value = this->slider->getValue();
					this->slider->setValue(*value + 0.033f);
					this->slice = -1;
				}
				else if (((ym-this->y) > this->height/2) && ((ym-this->y) < this->height)){
					float * value = this->slider->getValue();
					this->slider->setValue(*value - 0.033f);
					this->slice = 1;
				}
			}
		}
	}
	this->dragging = false;
}

void VSliceElement::pressed(int xm, int ym) {
	this->slider->pressed(xm,ym);
	this->slice = 0;
	*(this->sliceNo) = 0;
	count = 0;
}

void VSliceElement::drag(int xm, int ym) {
	if(this->slider->isInside(xm,ym))
		this->slider->drag(xm,ym);
	this->dragging = true;
}

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

float * VSliceElement::getSliderValue() {
	return this->slider->getValue();
}