/* I hacked this function to be placed inside uibasic.c because I 
   wasn't sure where else to place it.

   Arguements are fairly straight forward.
   int color: the rest of the api seemed to use color, so I follow suit.
   int xbaseline: location of "bottom" of battery on screen
   int ybaseline: location of "left" edge of battery on screen
   int length: how long is the battery to be (in pixels)
   int width: how tall is the battery to be (in pixels)
   int percent: what percentage of the charge has been used 

   Note: I am making use of the Logf() function until logging is 
         straightened out.  
*/

void draw_battery(int color, int xbaseline, int ybaseline, int length, 
                  int width, int percent) 
{
  float capacity = 0;
  int bar_xoffset = 2;
  int bar_yoffset = 2;
  int bar_len = 0;
  int bar_wid = width - (bar_xoffset*2);
  int i = 0;

  /* We only worry about length and width because if you place 
     the battery off the screen, its your own damn fault.  We log
     and then just draw an empty battery */
  if((percent > 100) || (percent < 0) || (length < 0) || (width < 0)) {
    Logf("Error: Battery data invalid");
    percent = 0; 
  }

  /* top batt. edge */
  drawline(color, xbaseline,  ybaseline, xbaseline+length-2,  ybaseline);

  /* bot batt. edge */
  drawline(color, xbaseline, ybaseline+width, 
		   xbaseline+length-2, ybaseline+width);

  /* left batt. edge */
  drawline(color, xbaseline, ybaseline, xbaseline, ybaseline+width);

  /* right batt. edge */
  drawline(color, xbaseline+length, ybaseline+1, xbaseline+length, 
           ybaseline+width-1);

  /* 2 dots that account for the nub on the right side of the battery */
  drawdot(color, xbaseline+length-1, ybaseline+1);
  drawdot(color, xbaseline+length-1, ybaseline+width-1);

  if(percent > 0) {
    /* % battery is full, 100% is length-bar_xoffset-1 pixels */
    capacity = ((float)percent / 100.0) * (length-(bar_xoffset*2)-1);
    bar_len = capacity;
 
    for(i = 0; i < bar_wid+1; i++) {
      drawline(color, xbaseline+bar_xoffset, ybaseline+bar_yoffset+i, 
               xbaseline+bar_xoffset+bar_len, ybaseline+bar_yoffset+i);
    } 
  }
}
