#include "graphics.h"
#include "gui.h"

void FFTWindow::Create(int sx, int sy, int sw, int sh, int stype, std::string sstr, int fsize)
{
	str=sstr;
	type=stype;
	x=sx;
	y=sy;
	w=sw;
	h=sh;
	state=0;
	fontsize=fsize;
}
int FFTWindow::GetType()
{
	return type;
}
int FFTWindow::GetState()
{
	return state;
}
int FFTWindow::GetX()
{
	return x;
}
int FFTWindow::GetY()
{
	return y;
}
int FFTWindow::GetW()
{
	return w;
}
int FFTWindow::GetH()
{
	return h;
}
int FFTWindow::GetFontSize()
{
	return fontsize;
}
std::string FFTWindow::GetString()
{
	return str;
}
void FFTWindow::SetState(int newstate)
{
	state=newstate;
}
void FFTWindow::SetString(std::string newstring)
{
	str=newstring;
}

GUI::~GUI()
{
	int i;
	for(i=0; i<wnext; i++)
	{
		delete windows[i];
	}
	SDL_FreeSurface(bg);
	SDL_FreeSurface(right);
	SDL_FreeSurface(bottom);
	SDL_FreeSurface(buttonup);
	SDL_FreeSurface(buttondown);
	SDL_FreeSurface(buttonuphover);
	SDL_FreeSurface(buttondownhover);
	SDL_FreeSurface(dirup);
	SDL_FreeSurface(dirdown);
	SDL_FreeSurface(dirleft);
	SDL_FreeSurface(dirright);
	SDL_FreeSurface(dirupg);
	SDL_FreeSurface(dirdowng);
	SDL_FreeSurface(dirleftg);
	SDL_FreeSurface(dirrightg);
	TTF_CloseFont(font10);
	TTF_CloseFont(font12);
	TTF_CloseFont(font14);
	TTF_CloseFont(font16);
	TTF_CloseFont(font18);
}
void GUI::Init()
{
	SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
	SDL_Color grey = { 0xAA, 0xAA, 0xAA, 0 };
	wnext=0;
	bg=LoadIMG("gfx/gui/bluebg.png");
	right=LoadIMG("gfx/gui/bgright.png");
	bottom=LoadIMG("gfx/gui/bgbottom.png");
	buttonup=LoadIMG("gfx/gui/buttonup.png");
	buttondown=LoadIMG("gfx/gui/buttondown.png");
	buttonuphover=LoadIMG("gfx/gui/buttonuphover.png");
	buttondownhover=LoadIMG("gfx/gui/buttondownhover.png");
	font10=TTF_OpenFont("fonts/arial.ttf", 10);
	font12=TTF_OpenFont("fonts/arial.ttf", 12);
	font14=TTF_OpenFont("fonts/arial.ttf", 14);
	dirright=TTF_RenderText_Blended(font14, ">", white);
	dirrightg=TTF_RenderText_Blended(font14, ">", grey);
	dirup=rotozoomSurface(dirright, 90, 1.0, SMOOTH);
	dirdown=rotozoomSurface(dirright, 270, 1.0, SMOOTH);
	dirleft=rotozoomSurface(dirright, 180, 1.0, SMOOTH);
	dirupg=rotozoomSurface(dirrightg, 90, 1.0, SMOOTH);
	dirdowng=rotozoomSurface(dirrightg, 270, 1.0, SMOOTH);
	dirleftg=rotozoomSurface(dirrightg, 180, 1.0, SMOOTH);
	font16=TTF_OpenFont("fonts/arial.ttf", 16);
	font18=TTF_OpenFont("fonts/arial.ttf", 18);
	buttonisdown=false;
}
int GUI::MakeWindow(int x, int y, int w, int h)
{
	windows[wnext]=new FFTWindow();
	windows[wnext]->Create(x, y, w, h, WINDOW, "", 0);
	return wnext++;
}
int GUI::MakeButton(int x, int y, int w, std::string str, int fsize)
{
	int h=24;
	windows[wnext]=new FFTWindow();
	windows[wnext]->Create(x, y, w, h, BUTTON, str, fsize);
	return wnext++;
}
int GUI::MakeDirButton(int x, int y, int dir)
{
	int h=12;
	int w=22;
	int fsize=14;
	int buttondir=-1;
	if (dir==0) buttondir=DIRBUTTONUP;
	else if (dir==1) buttondir=DIRBUTTONDOWN;
	else if (dir==2) buttondir=DIRBUTTONLEFT;
	else if (dir==3) buttondir=DIRBUTTONRIGHT;
	windows[wnext]=new FFTWindow();
	windows[wnext]->Create(x, y, w, h, buttondir, "", fsize);
	return wnext++;
}
int GUI::MakeBigButton(int x, int y, int w, int h, std::string str, int fsize)
{
	windows[wnext]=new FFTWindow();
	windows[wnext]->Create(x, y, w, h, BUTTON, str, fsize);
	return wnext++;
}
int GUI::MakeStatic(int x, int y, int w, int h, std::string str, int fsize)
{
	windows[wnext]=new FFTWindow();
	windows[wnext]->Create(x, y, w, h, STATIC, str, fsize);
	return wnext++;
}
int GUI::MakeStaticLeft(int x, int y, int w, int h, std::string str, int fsize)
{
	windows[wnext]=new FFTWindow();
	windows[wnext]->Create(x, y, w, h, STATICLEFT, str, fsize);
	return wnext++;
}
void GUI::NewString(int windowid, std::string str)
{
	windows[windowid]->SetString(str);
}
int GUI::GetButtonID(int x, int y, int w, std::string str)
{
	int i;
	for(i=0; i<30; i++)
	{
		if (windows[i]->GetX()==x && windows[i]->GetY()==y && windows[i]->GetW()==w && !strcmp(windows[i]->GetString().c_str(),str.c_str()))
			return i;
	}
	return -1;
}
void GUI::Draw()
{
	SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
	SDL_Color grey = { 0xAA, 0xAA, 0xAA, 0 };
	TTF_Font *font=font10;
	int i, x, y, w, h;
	int fontsize=10;
	for (i=0; i<wnext; i++)
	{
		x=windows[i]->GetX();
		y=windows[i]->GetY();
		w=windows[i]->GetW();
		h=windows[i]->GetH();
		if (fontsize!=windows[i]->GetFontSize())
		{
			fontsize=windows[i]->GetFontSize();
			if (fontsize==10) font=font10;
			else if (fontsize==12) font=font12;
			else if (fontsize==14) font=font14;
			else if (fontsize==16) font=font16;
			else if (fontsize==18) font=font18;
		}
		if (windows[i]->GetType()==STATIC)
		{
			if (strcmp(windows[i]->GetString().c_str(),"")) centerString(font, x+(int)w/2, y+(int)h/2, windows[i]->GetString(), white);
		}
		else if (windows[i]->GetType()==STATICLEFT)
		{
			if (strcmp(windows[i]->GetString().c_str(),"")) drawString(font, x, y, windows[i]->GetString(), white);
		}
		else if (windows[i]->GetType()==WINDOW)
		{
			DrawIMG(bg, x, y, w, h);
			DrawIMG(right, x+w, y, 1, h);
			DrawIMG(bottom, x, y+h, w, 1);
		}
		else if (windows[i]->GetType()==BUTTON)
		{
			if (windows[i]->GetState()==BUTTON_UP)
			{
				SDL_Surface *temp=zoomSurface(buttonup, (double)w/60.0, (double)h/60.0, SMOOTH);
				DrawIMG(temp, x, y);
				if (strcmp(windows[i]->GetString().c_str(),"")) centerString(font, x+(int)w/2, y+(int)h/2, windows[i]->GetString(), white);
				SDL_FreeSurface(temp);
			}
			else if (windows[i]->GetState()==BUTTON_DOWN)
			{
				SDL_Surface *temp=zoomSurface(buttondown, (double)w/60.0, (double)h/60.0, SMOOTH);
				DrawIMG(temp, x, y);
				if (strcmp(windows[i]->GetString().c_str(),"")) centerString(font, x+(int)(w/2)+1, y+(int)(h/2)+1, windows[i]->GetString(), grey);
				SDL_FreeSurface(temp);
			}
			else if (windows[i]->GetState()==BUTTON_UPHOVER)
			{
				SDL_Surface *temp=zoomSurface(buttonuphover, (double)w/60.0, (double)h/60.0, SMOOTH);
				DrawIMG(temp, x, y);
				if (strcmp(windows[i]->GetString().c_str(),"")) centerString(font, x+(int)w/2, y+(int)h/2, windows[i]->GetString(), white);
				SDL_FreeSurface(temp);
			}
			else if (windows[i]->GetState()==BUTTON_DOWNHOVER)
			{
				SDL_Surface *temp=zoomSurface(buttondownhover, (double)w/60.0, (double)h/60.0, SMOOTH);
				DrawIMG(temp, x, y);
				if (strcmp(windows[i]->GetString().c_str(),"")) centerString(font, x+(int)(w/2)+1, y+(int)(h/2)+1, windows[i]->GetString(), grey);
				SDL_FreeSurface(temp);
			}
		}
		else if (windows[i]->GetType()==DIRBUTTONUP || windows[i]->GetType()==DIRBUTTONDOWN || windows[i]->GetType()==DIRBUTTONLEFT || windows[i]->GetType()==DIRBUTTONRIGHT)
		{
			if (windows[i]->GetState()==BUTTON_UP)
			{
				SDL_Surface *temp=zoomSurface(buttonup, (double)w/60.0, (double)h/60.0, SMOOTH);
				DrawIMG(temp, x, y);
				if (windows[i]->GetType()==DIRBUTTONUP) DrawIMG(dirup,x,y);
				else if (windows[i]->GetType()==DIRBUTTONDOWN) DrawIMG(dirdown,x,y+1);
				else if (windows[i]->GetType()==DIRBUTTONLEFT) DrawIMG(dirleft,x+5,y-6);
				else if (windows[i]->GetType()==DIRBUTTONRIGHT) DrawIMG(dirright,x+7,y-5);
				SDL_FreeSurface(temp);
			}
			else if (windows[i]->GetState()==BUTTON_DOWN)
			{
				SDL_Surface *temp=zoomSurface(buttondown, (double)w/60.0, (double)h/60.0, SMOOTH);
				DrawIMG(temp, x, y);
				if (windows[i]->GetType()==DIRBUTTONUP) DrawIMG(dirupg,x+1,y+1);
				else if (windows[i]->GetType()==DIRBUTTONDOWN) DrawIMG(dirdowng,x+1,y+2);
				else if (windows[i]->GetType()==DIRBUTTONLEFT) DrawIMG(dirleftg,x+6,y-5);
				else if (windows[i]->GetType()==DIRBUTTONRIGHT) DrawIMG(dirrightg,x+8,y-4);
				SDL_FreeSurface(temp);
			}
			else if (windows[i]->GetState()==BUTTON_UPHOVER)
			{
				SDL_Surface *temp=zoomSurface(buttonuphover, (double)w/60.0, (double)h/60.0, SMOOTH);
				DrawIMG(temp, x, y);
				if (windows[i]->GetType()==DIRBUTTONUP) DrawIMG(dirup,x,y);
				else if (windows[i]->GetType()==DIRBUTTONDOWN) DrawIMG(dirdown,x,y+1);
				else if (windows[i]->GetType()==DIRBUTTONLEFT) DrawIMG(dirleft,x+5,y-6);
				else if (windows[i]->GetType()==DIRBUTTONRIGHT) DrawIMG(dirright,x+7,y-5);
				SDL_FreeSurface(temp);
			}
			else if (windows[i]->GetState()==BUTTON_DOWNHOVER)
			{
				SDL_Surface *temp=zoomSurface(buttondownhover, (double)w/60.0, (double)h/60.0, SMOOTH);
				DrawIMG(temp, x, y);
				if (windows[i]->GetType()==DIRBUTTONUP) DrawIMG(dirupg,x+1,y+1);
				else if (windows[i]->GetType()==DIRBUTTONDOWN) DrawIMG(dirdowng,x+1,y+2);
				else if (windows[i]->GetType()==DIRBUTTONLEFT) DrawIMG(dirleftg,x+6,y-5);
				else if (windows[i]->GetType()==DIRBUTTONRIGHT) DrawIMG(dirrightg,x+8,y-4);
				SDL_FreeSurface(temp);
			}
		}
	}
}
int GUI::SendMouseClick(int getx, int gety)
{
	int i, x, y, x2, y2;
	for (i=0; i<wnext; i++)
	{
		if (windows[i]->GetType()==BUTTON || windows[i]->GetType()==DIRBUTTONUP || windows[i]->GetType()==DIRBUTTONDOWN || windows[i]->GetType()==DIRBUTTONLEFT || windows[i]->GetType()==DIRBUTTONRIGHT)
		{
			x=windows[i]->GetX();
			y=windows[i]->GetY();
			x2=windows[i]->GetX()+windows[i]->GetW();
			y2=windows[i]->GetY()+windows[i]->GetH();
			if (getx>=x && getx<=x2 && gety>=y && gety<=y2)
			{
				if (windows[i]->GetState()==BUTTON_UP) windows[i]->SetState(BUTTON_DOWNHOVER);
				else if (windows[i]->GetState()==BUTTON_DOWN) windows[i]->SetState(BUTTON_UPHOVER);
				else if (windows[i]->GetState()==BUTTON_UPHOVER) windows[i]->SetState(BUTTON_DOWNHOVER);
				else if (windows[i]->GetState()==BUTTON_DOWNHOVER) windows[i]->SetState(BUTTON_UPHOVER);
				buttonisdown=true;
				return i;
			}
		}
	}
	return -1;
}
int GUI::SendMouseMotion(int getx,int gety)
{
	int i, x, y, x2, y2;
	int ret=0;
	if (!buttonisdown)
	{
		for (i=0; i<wnext; i++)
		{
			if (windows[i]->GetType()==BUTTON || windows[i]->GetType()==DIRBUTTONUP || windows[i]->GetType()==DIRBUTTONDOWN || windows[i]->GetType()==DIRBUTTONLEFT || windows[i]->GetType()==DIRBUTTONRIGHT)
			{
				x=windows[i]->GetX();
				y=windows[i]->GetY();
				x2=windows[i]->GetX()+windows[i]->GetW();
				y2=windows[i]->GetY()+windows[i]->GetH();
				if (getx>=x && getx<=x2 && gety>=y && gety<=y2)
				{
					if (windows[i]->GetState()==BUTTON_UP)
					{
						windows[i]->SetState(BUTTON_UPHOVER);
					}
					else if (windows[i]->GetState()==BUTTON_DOWN)
					{
						windows[i]->SetState(BUTTON_DOWNHOVER);
					}
				}
				else
				{
					if (windows[i]->GetState()==BUTTON_UPHOVER)
					{
						windows[i]->SetState(BUTTON_UP);
					}
				}
			}
		}
	}
	return ret;
}
void GUI::UpdateButtons()
{
	int i;
	for (i=0; i<wnext; i++)
	{
		if (windows[i]->GetType()==BUTTON || windows[i]->GetType()==DIRBUTTONUP || windows[i]->GetType()==DIRBUTTONDOWN || windows[i]->GetType()==DIRBUTTONLEFT || windows[i]->GetType()==DIRBUTTONRIGHT)
		{
			if (windows[i]->GetState()==BUTTON_DOWN) windows[i]->SetState(BUTTON_UP);
			if (windows[i]->GetState()==BUTTON_DOWNHOVER) windows[i]->SetState(BUTTON_UP);
		}
	}
	buttonisdown=false;
}

