#include "empire.h"
#include "logic.h"

extern int intmin(int a, int b);

void draw_unitstats(_unit *u)
{
  bool bigbuf = false;
	float alpha = 0.7;
	int i = 0, posx, posy;
	char str[30];
	
  while (i < 2)
  {
    if (u->load[i].loaded != 0)
    {
      bigbuf = true;
    }
    i++;
  }

  if (!bigbuf)
  {
    if (u->tilex - worldmap.scroll_x > 5)
    {
			posx = 0;
			posy = 435;
    }
    else
    {
			posx = 540;
			posy = 435;
    }
  }
  else
  {
    if (u->tilex - worldmap.scroll_x > 5)
    {
			posx = 0;
			posy = 390;
    }
    else
    {
			posx = 540;
			posy = 390;
    }
  }
	
	buffer_rectfill(posx, posy, posx + 100, posy + 90, DGREY, alpha - 0.1);
	buffer_rect(posx, posy, posx + 100, posy + 90, player_color(u->color), alpha);
	buffer_textout_ex(normal, unit_name(u->type), posx + 5, posy + 3, WHITE, alpha);
	sprintf(str, "Health: %-1.2f", u->health);
	buffer_textout_ex(normal, str, posx + 5, posy + 13, WHITE, alpha);
  sprintf(str, "Ammo: %-1d", u->ammo);
	buffer_textout_ex(normal, str, posx + 5, posy + 23, WHITE, alpha);
  sprintf(str, "Fuel: %-1d", u->gas);
	buffer_textout_ex(normal, str, posx + 5, posy + 33, WHITE, alpha);
  if (u->load[0].loaded == 1)
  {
		sprintf(str, "%-1s %-1d", unit_name(u->load[0].type).c_str(), rounded_health(u->load[0].health));
		buffer_textout_ex(normal, str, posx + 5, posy + 50, WHITE, alpha);
  }
  if (u->load[1].loaded == 1)
  {
		sprintf(str, "%-1s %-1d", unit_name(u->load[1].type).c_str(), rounded_health(u->load[1].health));
		buffer_textout_ex(normal, str, posx + 5, posy + 60, WHITE, alpha);
  }
}

void draw_playerstats(int plyr)
{
  int z = plyr / 10;  //AI players send draw location information in the plyr int as well
  plyr %= 10;  //it is in the tens column, so regular player information is untouched
  int mx = mouse_x;
  int col = player_color(plyr);
	char str[20];
	int posx;
	float alpha;
	static int w = 1;

  if (player[plyr].controller == AI)
  {  //for the AI, draw position was sent in 'plyr' in the tens column, and was sent to 'z'
		alpha = 0.4;
    if (z == 1)
    {
      posx = 0;
    }
    else
    {
      posx = 512;
    }
  }
  else
  {  //for a human, the draw position depends on the mouse location
		alpha = 0.66;
    if (mx > 320)
    {
      posx = 0;
    }
    else
    {
      posx = 510;
    }
  }

  buffer_rectfill(posx, 0, posx + 130, 95, DGREY, alpha);
	buffer_rect(posx, 0, posx + 129, 114, col, alpha);
  buffer_rect(posx + 1, 1, posx + 128, 113, col, alpha);
	buffer_blit(currentcharacter[plyr]->picture, posx + 25, 2, alpha);

	sprintf(str, "Funds: %-1d", player[plyr].cash);
	buffer_textout_ex(normal, str, posx + 5, 84, WHITE, alpha);

  if (player[plyr].power == currentcharacter[plyr]->powerneeded)  //if the power gauge is full, make it stand out
  {
		buffer_rectfill(posx + 2, 97, posx + 127, 112,
					          makecol(intmin(255, (1 + getr(col)) * w), intmin(255, (1 + getg(col)) * w), intmin(255, (1 + getb(col)) * w)),
		                alpha);
		w += 5;
		if (w > 255) w = 1;
	  sprintf(str, "Power: %-1d", player[plyr].power);
		buffer_textout_ex(normal, str, posx + 5, 100, col, alpha);
  }
  else
  {
		buffer_rectfill(posx + 2, 97, posx + (player[plyr].power * 127) / currentcharacter[plyr]->powerneeded, 112, col, alpha);
		sprintf(str, "Power: %-1d", player[plyr].power);
		buffer_textout_ex(normal, str, posx + 5, 100, WHITE, alpha);
  }
}
