#import "HostController.h"
#import "Defaults.h"
#import "TokenString.h"
#import <appkit/appkit.h>
#import "EnhancedMatrix.h"

@interface HostController (PRIVATE)

-(void)_addHostToList: (const char *) aHost;

@end


@implementation HostController

- init
{
   [super init];
   [self setDocType: "Hosts"];
   _defaults = [Defaults new];
   return self;
}

- awakeFromNib
{
   [self select: nil];
   return self;
}


- setHosts: anObject
{
   hosts = anObject;

   [hosts  loadFromString: [_defaults get: "Hosts"]];
   [hosts setTarget: self];
   [hosts setAction: @selector(select:)];
   [hosts selectCellAt: 0 : 0];
   return self;
}

- update: sender
{
   const char *ahost,*updateHost;
   id hostList = [[TokenString alloc] init: [_defaults get: "Hosts"]];

   [hostList setSeparator: ';'];
   updateHost = [name stringValue];
   if(!updateHost)
       return nil;
   while(ahost = [hostList popStringValue])
       if(!strcmp(ahost,updateHost))
       {
	  [hostList setFStringValue: "%s PPPStart", updateHost];
	  [_defaults writeDB: [hostList stringValue] as: [start stringValue]];
	  break;
       }
   return self;
}


- add:sender
{
   const char *hostList = [_defaults get: "Hosts"];
   const char *newHost;
   id str = [[[StringStorage alloc] init] autorelease];

   newHost = [name stringValue];
   if(!newHost)
       return nil;

   if(!hostList)
       [str setStringValue: newHost];
   else
       [str setFStringValue: "%s;%s", hostList, newHost];
   [_defaults writeDB: "Hosts" as: [str stringValue]];

   [str setFStringValue: "%s PPPStart", newHost];
   [_defaults writeDB: [str stringValue] as: [start stringValue]];

   [str setFStringValue: "%s TotalTime", newHost];
   [_defaults writeDB: [str stringValue] as: "0"];

   [str setFStringValue: "%s LastReset", newHost];
   [_defaults writeDB: [str stringValue] as: "0"];

   [self _addHostToList: newHost];
   [self select:nil];
   [hosts sizeToCells];
   [hosts display];
   return self;
}

- remove:sender
{
   int rows, cols, x, theRow = 0;
   id aCell,cell = [hosts selectedCell];
   id hostList = [[[StringStorage alloc] init] autorelease];

   [hosts getNumRows: &rows numCols: &cols];

   for (x = 0; x < rows; x++)
   {
      aCell = [hosts cellAt: x : 0];
      if (cell == aCell)
      {
	 id str = [[[StringStorage alloc] init] autorelease];

	 [str setFStringValue: "%s PPPStart", [aCell title]];
	 [_defaults removeDB: [str stringValue]];

	 [str setFStringValue: "%s TotalTime", [aCell title]];
	 [_defaults removeDB: [str stringValue]];

	 [str setFStringValue: "%s LastReset", [aCell title]];
	 [_defaults removeDB: [str stringValue]];
	 theRow = x;	
      }
      else
      {
	 if([hostList strlen])
	     [hostList appendStringValue: ";"];
	 [hostList appendStringValue: [aCell title]];
      }
   }

   [_defaults writeDB: "Hosts" as: [hostList stringValue]];
   [hosts removeRowAt: theRow andFree: YES];
   [hosts sizeToCells];
   [hosts display];
   [self select: nil];
   return self;
}

- select: sender
{
   id cell = [hosts selectedCell];
   id str = [[[StringStorage alloc] init] autorelease];

   if(!cell)
       return nil;

   [name setStringValue: [cell title]];
   [str setFStringValue: "%s PPPStart", [cell title]];
   [start setStringValue: [_defaults get: [str stringValue]]];
   return self;
}

@end


@implementation HostController (PRIVATE)

-(void)_addHostToList: (const char *) aHost
{
   int rows, cols;
   id cell;

   [hosts getNumRows: &rows numCols: &cols];
   [hosts addRow];
   cell = [hosts cellAt: rows : 0];
   [cell setTitle: aHost];
}


@end
