//
//  MPT_Preferences.m
//  AUCoderDemo
//
//  Created by Andrew Salamon on 10/2/06.
//  Copyright (c) 2006 Machine Perception Laboratory, University of California San Diego.
//

#import "MPT_Preferences.h"
#import "MPT_PluginController.h"

/*
 setSelectedItemIdentifier
 */

@interface MPT_Preferences(PrivateMethods)
- (void)activatePrefPane:(id)sender;
- (void)resizeWindowForContent:(NSView *)view;
@end

NSString *MPT_ToolbarItemIdentifierGeneral = @"MPT_ToolbarItemIdentifierGeneral";
NSString *MPT_ToolbarItemIdentifierPlugins = @"MPT_ToolbarItemIdentifierPlugins";

@implementation MPT_Preferences
+ (NSArray *)pluginTableSortDescriptors
{
	NSSortDescriptor *typeDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"step" ascending:NO] autorelease];
	NSSortDescriptor *nameDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"localizedName" ascending:YES] autorelease];
	return [NSArray arrayWithObjects:typeDescriptor, nameDescriptor, nil];	
}

- (void)awakeFromNib
{
	NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:@"CERTPreferences"];
	[toolbar setDelegate:self];
	[toolbar autorelease];
	[[self window] setToolbar:toolbar];
	[toolbar setSelectedItemIdentifier:MPT_ToolbarItemIdentifierGeneral];
	[self activatePrefPane:generalToolbarItem];
}

- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
{
    return [NSArray arrayWithObjects: MPT_ToolbarItemIdentifierGeneral, MPT_ToolbarItemIdentifierPlugins,
        NSToolbarSeparatorItemIdentifier,
        NSToolbarFlexibleSpaceItemIdentifier,
        NSToolbarSpaceItemIdentifier, nil];
}

- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
{
    return [NSArray arrayWithObjects: MPT_ToolbarItemIdentifierGeneral, MPT_ToolbarItemIdentifierPlugins,
        NSToolbarSeparatorItemIdentifier, nil];
}

- (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
{
	NSToolbarItem *toolbarItem = nil;
	
    if( [itemIdentifier isEqual: MPT_ToolbarItemIdentifierGeneral] )
	{
		generalToolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier];
		toolbarItem = generalToolbarItem;

		// Set the text label to be displayed in the 
		// toolbar and customization palette 
		[toolbarItem setLabel:@"General"];
		[toolbarItem setPaletteLabel:@"General"];
		
		// Set up a reasonable tooltip, and image
		// you will likely want to localize many of the item's properties 
		[toolbarItem setToolTip:@"General Preferences"];
		[toolbarItem setImage:[NSImage imageNamed:@"GeneralPrefIcon"]];
		
		// Tell the item what message to send when it is clicked 
		[toolbarItem setTarget:self];
		[toolbarItem setAction:@selector(activatePrefPane:)];
		
    }
	else if( [itemIdentifier isEqual: MPT_ToolbarItemIdentifierPlugins] )
	{
		pluginToolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier];
		
		toolbarItem = pluginToolbarItem;

		// Set the text label to be displayed in the 
		// toolbar and customization palette 
		[toolbarItem setLabel:@"Plugins"];
		[toolbarItem setPaletteLabel:@"Plugins"];
		
		// Set up a reasonable tooltip, and image
		// you will likely want to localize many of the item's properties 
		[toolbarItem setToolTip:@"Plugin Preferences"];
		[toolbarItem setImage:[NSImage imageNamed:@"PluginPrefIcon"]];
		
		// Tell the item what message to send when it is clicked 
		[toolbarItem setTarget:self];
		[toolbarItem setAction:@selector(activatePrefPane:)];
    }
    return toolbarItem;
}

- (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
{
    return [NSArray arrayWithObjects: MPT_ToolbarItemIdentifierGeneral, MPT_ToolbarItemIdentifierPlugins, nil];
}

- (void)activatePrefPane:(id)sender
{
    if( [[sender itemIdentifier] isEqual: MPT_ToolbarItemIdentifierGeneral] )
	{
		[generalPrefs setFrameOrigin:NSMakePoint(0.0,0.0)];
		[self resizeWindowForContent:generalPrefs];
		[[self window] setContentView:generalPrefs];
	}
	else if( [[sender itemIdentifier] isEqual: MPT_ToolbarItemIdentifierPlugins] )
	{
		[pluginPrefs setFrameOrigin:NSMakePoint(0.0,0.0)];
		[self resizeWindowForContent:pluginPrefs];
		[[self window] setContentView:pluginPrefs];
	}
}

- (NSArray *)plugins
{
	return [[MPT_PluginController defaultController] plugins];
}

- (void)resizeWindowForContent:(NSView *)view
{
	NSWindow *win = [self window];
	NSRect rect = [win frame];
	NSRect newRect = [win frameRectForContentRect:[view frame]];
	rect.size = newRect.size;
	rect.origin.y += rect.size.height - newRect.size.height;
	[win setFrame:rect display:YES animate:YES];
}

- (NSArray *)pluginTableSortDescriptors
{
	return [[self class] pluginTableSortDescriptors];
}

- (void)setPluginTableSortDescriptors:(NSArray *)descs
{
	
}
@end
