#include "empire.h"

extern int SIDEBAR_WIDTH, BOTTOMBAR_HEIGHT;

void draw_unitstats(_unit *u, int mx, int my)
{
	const int DRAW_OFFSET_X = 10;
	const int DRAW_OFFSET_Y = 10;
	const int STATS_WIDTH = 110;
	
	char c[20];
	int h = 45;
	int i, x, y;
	unitprops *p = &unitstats[u->type];
	
	h += unitstats[u->type].loadcapacity * 15;
	x = 0;
	y = 0;
	
	if (my + DRAW_OFFSET_Y + h > SCREEN_H - BOTTOMBAR_HEIGHT)
	{ //make sure the unit stats don't get cut off by the screen edges or go onto the sidebar or bottombar
		if (mx + DRAW_OFFSET_X + STATS_WIDTH > SCREEN_W - SIDEBAR_WIDTH)
		{
			x = mx - 110;
			y = my - h;
		}
		else
		{
			x = mx + DRAW_OFFSET_X;
			y = my - h;
		}
	}
	else
	{
		if (mx + DRAW_OFFSET_X + STATS_WIDTH > SCREEN_W - SIDEBAR_WIDTH)
		{
			x = mx - 110;
			y = my + DRAW_OFFSET_Y;
		}
		else
		{
			x = mx + DRAW_OFFSET_X;
			y = my + DRAW_OFFSET_Y;
		}
	}
	
	buffer_rectfill(x, y, x + 110, y + h, BLACK, 0.3);
	buffer_rect(x, y, x + 110, y + h, WHITE);
	buffer_textout_ex(normal, p->name.c_str(), x + 5, y + 5, WHITE);
	sprintf(c, "Health: %#.1f%%", u->health * 10);
	buffer_textout_ex(normal, c, x + 5, y + 20, WHITE);
	
	i = 0;
	while (i < unitstats[u->type].loadcapacity)
	{
		if (u->load[i].loaded == 1)
		{
			buffer_textout_ex(normal, unit_name(u->load[i].type), x + 5, y + 35 + (15 * i), WHITE);
		}
		else
		{
			buffer_textout_ex(normal, "-", x + 5, y + 35 + (15 * i), WHITE);
		}
		i++;
	}
}
