#import "BWMainObject.h"
#import "BWOverWorld.h"
#import "BWGameData.h"

@implementation BWMainObject

- (void)awakeFromNib
{
	path = nil;
	romData = nil;
}

- (IBAction)open:(id)sender
{
	int r;
	NSOpenPanel *op = [NSOpenPanel openPanel];
	r = [op runModalForTypes:nil];
	if (r == NSOKButton) {
		
		if (path) [path release];
		path = [[NSString alloc] initWithString:[[op filenames] lastObject]];
		NSLog(@"Path : %@",path);
		
		if (romData) [romData release];
		romData = [[NSMutableData alloc] initWithContentsOfFile:path];
		NSLog(@"romData length : %X",[romData length]);
		NSLog(@"--------");
		// Eventually check if the file is good, even if we don't really care...
		
		[objOverWorld open:romData];
		[objGameData open:romData];
		
		NSLog(@"ROM loaded");
		NSLog(@"--------");
	}
}

- (IBAction)save:(id)sender
{
	[objOverWorld save];
	[objGameData save];
	
	if ([romData writeToFile:path atomically:YES])
		NSLog(@"Save - OK");
	else
		NSLog(@"Save - Error");
}

- (void)applicationWillTerminate:(NSNotification *)notif
{
	int i;
	if (romData) {
		i = NSRunAlertPanel(@"Do you want to save before quitting ?",@"",@"Yes",@"No",nil);
		if (i == NSAlertDefaultReturn)
			[self save:nil];
	}
}

@end

