#import "ConverterController.h"
#import <ThemeKit/ThemeKit.h>

@implementation ConverterController

- (IBAction)convertResourceFile:(id)sender
{
	//First we need to query the user for the resource file
	NSOpenPanel *openQuery = [[NSOpenPanel alloc] init];
	[openQuery setAllowedFileTypes:[NSArray arrayWithObject:@"rsrc"]];
	[openQuery setTitle:@"Please choose a resource file"];
	[openQuery runModal];
	NSString *inputResourceFilePath = [openQuery filename];
	[openQuery release];
	//return out if the user canceled
	if(inputResourceFilePath==nil)
		return;
	//ask for the save location now
	NSSavePanel *saveQuery = [[NSSavePanel alloc] init];
	[saveQuery setAllowedFileTypes:[NSArray arrayWithObject:@"rsrc"]];
	[saveQuery setTitle:@"Save Output File"];
	[saveQuery runModal];
	NSString *outputResourceFilePath = [saveQuery filename];
	[saveQuery release];
	//again, return if output query canceled
	if(outputResourceFilePath==nil)
		return;
	//Create a TKTheme to hold the converted resource
	TKTheme *workingTheme = [[TKTheme alloc] init];
	//Add a placeholder appskin to hold the resources
	//com.apple.macosx is used for Extras.rsrc
	TKAppSkin *appSkin = [workingTheme addAppSkinForIdentifier:@"com.apple.macosx"];
	//Read in the resource file
	[appSkin pullFromExtrasResourceData:inputResourceFilePath];
	//Write out a properly endianified resource file
	//This code will only run on Intel based Macintoshes because it mixes with the Extras.rsrc on the machine.
	//We don't want to mix with the PowerPC Extras.rsrc, that would be bad.
	[appSkin writeExtrasFileAtPath:outputResourceFilePath withEndianness:kLittleEndian];
	//That's it, we're done!
	[workingTheme release];
}

@end
