/*
Solar Conquest
Copyright (C) 2006 Greg Beaman

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

class CBarChart : public CMenuItem
{
protected:
	int numBars;
	float xfactor;

public:
	CBarChart(int iid, int px, int py, int w, int h);
	virtual ~CBarChart();

	void DrawItem(bool selected);
	void DrawBar(int num, float barHeight);
	void DrawItemText(bool selected);

	void BeginBars(int bars);
	void EndBars();
};

CBarChart::CBarChart(int iid, int px, int py, int w, int h)
{
	id = iid;

	x = px;
	y = py;
	width = w;
	height = h;
	itemType = MENU_ITEM_BARCHART;
	strcpy(caption,"");

	nextItem = NULL;
};

CBarChart::~CBarChart()
{
}

void CBarChart::DrawItem(bool selected)
{
	//Draw the box
	glBegin(GL_LINES);
		glColor4f(0,0,1,1);
		glVertex2f(x,y);
		glVertex2f(x+width,y);
		glVertex2f(x+width,y);
		glVertex2f(x+width,y+height);
		glVertex2f(x+width,y+height);
		glVertex2f(x,y+height);
		glVertex2f(x,y+height);
		glVertex2f(x,y);
	glEnd();

	glBegin(GL_QUADS);
		glColor4f(0,0,1,0.5);
		glVertex2f(x,y);
		glVertex2f(x+width,y);
		glVertex2f(x+width,y+height);
		glVertex2f(x,y+height);
	glEnd();
}

void CBarChart::DrawItemText(bool selected)
{
	g_MainFont->RenderText(x+6,y+6,1,1,caption);
}

void CBarChart::BeginBars(int bars)
{
	glBegin(GL_QUADS);
	numBars = bars;
	xfactor = width/2 - bars * 5;
}

void CBarChart::DrawBar(int num, float barHeight)
{
	float bheight = (height-2) * barHeight;
	int color = num % 5;

	if (color == 0)
		glColor4f(1,0,0,1);
	else if (color == 1)
		glColor4f(0,1,0,1);
	else if (color == 2)
		glColor4f(1,1,0,1);
	else if (color == 3)
		glColor4f(0,1,1,1);
	else if (color == 4)
		glColor4f(1,0,1,1);

	float xpos = x+1+xfactor+10*num;
	glVertex2f(xpos-1, y+height-1);
	glVertex2f(xpos-1, y+height-bheight-1);
	glVertex2f(xpos+1, y+height-bheight-1);
	glVertex2f(xpos+1, y+height-1);
}

void CBarChart::EndBars()
{
	glEnd();
}
