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

static NXDefaultsVector defaultsVector = 
{
{ "LogFile", "/usr/adm/ppp2.2.debug" },
{ "LogDir","/usr/adm" },
{ "PPPStop", "/etc/ppp/pppkill"}, 
{ "DisplayLog","No" },
{ "DisplayMeter","No" },
{ "StartPPPOnLaunch","No" },
{ "TotalTime", "0" },
{ "SoundDir","/NextLibrary/Sounds" },
{ "LastReset","0" },
{ "NXHelpPanelSizes", "412, 87, 436, 676, 418, 120, 418, 438" },
{NULL,NULL}
};

static const char *types[] = {"snd",NULL};
   

@implementation Preferences

- init
{
   [super init];
   _defaults = [Defaults new];
   _openPanel = [OpenPanel new];
   [_openPanel allowMultipleFiles: NO];
   [_defaults regDefaults: defaultsVector];
   return self;
}

- makeKeyAndOrderFront: sender
{
   if(!panel)
       [self loadNib];

   [self revert: self];
   [panel makeKeyAndOrderFront: self];
   return self;
}

- revert: sender
{
   [hosts loadFromString: [_defaults get: "Hosts"]];
   [hosts selectCellWithTitle: [_defaults get: "DefaultHost"]];
   [stopCmd setStringValue: [_defaults get: "PPPStop"]];
   [logFile setStringValue: [_defaults get: "LogFile"]];
   [upSnd setStringValue: [_defaults get: "UpSound"]];
   [downSnd setStringValue: [_defaults get: "DownSound"]];
   [displayLog setState: !strcmp("Yes",[_defaults get: "DisplayLog"])];
   [displayMeter setState: !strcmp("Yes",[_defaults get: "DisplayMeter"])];
   [startPPPOnLaunch setState: !strcmp("Yes",[_defaults get: "StartPPPOnLaunch"])];
   return self;
}

- set: sender
{
   [_defaults writeDB: "DefaultHost" as: [[hosts selectedCell] title]];
   [_defaults writeDB: "PPPStop" as: [stopCmd stringValue]];
   [_defaults writeDB: "LogFile" as: [logFile stringValue]];
   [_defaults writeDB: "UpSound" as: [upSnd stringValue]];
   [_defaults writeDB: "DownSound" as: [downSnd stringValue]];
   [_defaults writeDB: "DisplayLog" as: ([displayLog state]? "Yes" : "No")];
   [_defaults writeDB: "DisplayMeter" as: ([displayMeter state]? "Yes" : "No")];
   [_defaults writeDB: "StartPPPOnLaunch" as: ([startPPPOnLaunch state]? "Yes" : "No")];
   return self;
}

- findLog: sender
{
   if([_openPanel runModalForDirectory: [_defaults get: "LogDir"] file: NULL] == NX_OKTAG)
   {
      [logFile setStringValue: [_openPanel filename]];
      [_defaults writeDB: "LogDir" as: [_openPanel directory]];
   }
   
   
   return self;
}

- findUpSnd: sender
{
   if([_openPanel runModalForDirectory: [_defaults get: "SoundDir"] file: NULL types: types] == NX_OKTAG)
   {
       [upSnd setStringValue: [_openPanel filename]];
       [_defaults writeDB: "SoundDir" as: [_openPanel directory]];
    }      

   return self;
}

- findDownSnd: sender
{
   if([_openPanel runModalForDirectory: [_defaults get: "SoundDir"] file: NULL types: types] == NX_OKTAG)
   {
      [downSnd setStringValue: [_openPanel filename]];
      [_defaults writeDB: "SoundDir" as: [_openPanel directory]];
   }

   return self;
}


@end
