/* PBDefaultBuildTool.m created by bungi on Sun 10-Nov-1996 */

#import <Foundation/Foundation.h>
#import <ProjectBuilder/PBBundleHost.h>

#import "PBDefaultBuildTool.h"

static PBDefaultBuildTool *sharedDefaultBuildTool          = nil;
static NSString           *PBProjectSettingsDictionaryKey  = @"PBProjectSettingsDictionary";
static NSString           *DontAskAboutDefaultSettingsKey  = @"DontAskAboutDefaultSettings";

@implementation PBDefaultBuildTool

+ (void) initialize;
{
    [super initialize];

    if (sharedDefaultBuildTool)
        return;

    sharedDefaultBuildTool = [[self alloc] init];
}

- init;
{
    NSMutableDictionary  *defaults;
    NSUserDefaults       *userDefaults;
    NSNotificationCenter *center;
    NSDictionary         *infoDict;
    
    [super init];

    // Register defaults.
    infoDict = [[NSBundle bundleForClass: isa] infoDictionary];
    defaults = [infoDict objectForKey: PBProjectSettingsDictionaryKey];
        
    userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults registerDefaults: [NSDictionary dictionaryWithObjects: &defaults
                                                                forKeys: &PBProjectSettingsDictionaryKey
                                                                  count: 1]];

    projectSettings = [[[NSUserDefaults standardUserDefaults] dictionaryForKey: PBProjectSettingsDictionaryKey] retain];
    settingOrder    = [[projectSettings allKeys] retain];
    

    // Register for Project Open Notifications so we can check each project.
    center = [NSNotificationCenter defaultCenter];

    [center addObserver: self
               selector: @selector(projectDidOpen:)
                   name: PBProjectDidOpenNotification
                 object: nil];
    
    return self;
}

#define DEFAULTS_RESET           0
#define DEFAULTS_ASK_LATER       1
#define DEFAULTS_KEEP_OLD_VALUES 2

- (void) projectDidOpen: (NSNotification *) notification;
{
    NSEnumerator *settingEnum;
    NSString     *settingKey;
    NSString     *setting;
    NSString     *currentSetting;
    BOOL         differs;

    project = [notification object];

    if ([project objectForKey: DontAskAboutDefaultSettingsKey])
        return;

    // See if any of the settings differ.
    differs     = NO;
    settingEnum = [projectSettings keyEnumerator];

    while ((settingKey = [settingEnum nextObject])) {
        currentSetting = [project objectForKey: settingKey];
        setting        = [projectSettings objectForKey: settingKey];

        if (![setting isEqualToString: currentSetting]) {
            differs = YES;
            break;
        }
    }

    if (!differs)
        return;

    if (!messagePanel) {
        // Load nib.
        if (![NSBundle loadNibNamed: [isa description] owner: self]) {
            NSRunAlertPanel([isa description], @"Failed to load nib", @"OK", nil, nil);
            return;
        }
        [messagePanel center];
    }

    [messagePanel makeKeyAndOrderFront: nil];

    switch ([NSApp runModalForWindow: messagePanel]) {
        case DEFAULTS_RESET:
            settingEnum = [projectSettings keyEnumerator];

            while ((settingKey = [settingEnum nextObject]))
                [project setObject: [projectSettings objectForKey: settingKey]
                            forKey: settingKey];
            break;
        case DEFAULTS_KEEP_OLD_VALUES:
            [project setObject: @"Y" forKey: DontAskAboutDefaultSettingsKey];
            break;
        case DEFAULTS_ASK_LATER:
        default:
            break;
    }

    [messagePanel orderOut: nil];
}

- (void) resetToDefaults: sender;
{
    [NSApp stopModalWithCode: DEFAULTS_RESET];
}

- (void) askOnNextLoad: sender;
{
    [NSApp stopModalWithCode: DEFAULTS_ASK_LATER];
}

- (void) keepOldValues: sender;
{
    [NSApp stopModalWithCode: DEFAULTS_KEEP_OLD_VALUES];
}

// NSTableView data source

- (int)numberOfRowsInTableView:(NSTableView *)tableView;
{
    return [settingOrder count];
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row;
{
    NSString *identifier;
    NSString *settingName;
    NSString *current, *correct;

    identifier  = [tableColumn identifier];
    settingName = [settingOrder objectAtIndex: row];
    current     = [project objectForKey: settingName];
    correct     = [projectSettings objectForKey: settingName];
    
    if ([identifier isEqualToString: @"Differs"]) {
        if (![correct isEqualToString: current])
            return @"Yes";
        else
            return @"";
    } else if ([identifier isEqualToString: @"Setting"]) {
        return settingName;
    } else if ([identifier isEqualToString: @"Current"]) {
        return current;
    } else if ([identifier isEqualToString: @"Correct"]) {
        return correct;
    } else {
        return @"<UNKNOWN COLUMN>";
    }
}

@end
