/*
 * For legal stuff see the file COPYRIGHT
 */
#import "SessionViewMgr.h"
#import "ClientInspector.h"
#import "SessionEditor.h"
#import "Controller.h"

@interface SessionViewMgr(PRIVATE)
- (void)displayTotalMins;
- (void)displayClientMins;
- (float)totalBillings;
- (int)totalMins;
@end

@implementation SessionViewMgr

- itemEditor
{
  return [SessionEditor new];
}

/*
 * Provide the add, delete and sort methods to invoke on ClientInfo objects
 */
- (SEL)addMethod
{
  return @selector(addSession:);
}

- (SEL)deleteMethod
{
  return @selector(deleteSession:);
}

- (SEL)sortMethod
{
  return @selector(sortSessions);
}


- info:(ClientInfo *)info itemAt:(int)position
{
  return [info sessionAt:position];
}

- (int)itemCount:(ClientInfo *)info 
{
  return [info sessionCount];
}

- (void)displaySummary
{
  [self displayClientMins];
  [self displayTotalMins];
}

@end


@implementation SessionViewMgr(PRIVATE)

- (float)totalBillings
{
  float billings = 0.0;
  List *clientList = [[NXApp delegate] clientList];
  int i, count = [clientList count];

  for ( i = 0; i < count; i++ )
    billings += [[clientList objectAt:i] totalBillable];

  return billings;
}

- (int)totalMins
{
  List *clientList = [[NXApp delegate] clientList];
  int i, count = [clientList count];
  int totalMins = 0;

  for ( i = 0; i < count; i++ )
    totalMins += [[clientList objectAt:i] totalMins];

  return totalMins;
}

- (void)displayClientMins
{
  id info = [[ClientInspector sharedInstance] selectedClient];

  if ( info ) {
    char buf[20];
    sprintf( buf, "%.2f", [info totalHours] );
    [clientHoursField setStringValue:buf];

    sprintf( buf, "$%.2f", [info totalBillable] );
    [clientBillingsField setStringValue:buf];
  } else {
    [clientHoursField    setStringValue:""];
    [clientBillingsField setStringValue:""];
  }
}

/*
 * Display the total minutes and billings, and for the
 * currently selected client.
 */
- (void)displayTotalMins
{
  char buf[10];
  float hours = [self totalMins]/60.0;

  sprintf( buf, "%.2f", hours );
  [totalHoursField setStringValue:buf];

  sprintf( buf, "$%.2f", [self totalBillings] );
  [totalBillingsField setStringValue:buf];
}

@end
