//
//  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"
#import "TestController.h"
#import "MPT_Demo.h"

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

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

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

- (void)dealloc
{
	[toolbar release];
	[super dealloc];
}

- (void)awakeFromNib
{
	toolbar = [[NSToolbar alloc] initWithIdentifier:@"CERTPreferences"];
	[toolbar setDelegate:self];
	[[self window] setToolbar:toolbar];
	[[[self window] windowController] setWindowFrameAutosaveName:@"CertPrefWindow"]; // Not sure why this isn't be handled properly by the nib.
	[toolbar setSelectedItemIdentifier:MPT_ToolbarItemIdentifierGeneral];
	[self activatePrefPane:generalToolbarItem];
	
	if( [[MPT_Demo sharedDemo] isEvaluation] )
	{
		[alignmentLabel removeFromSuperview];;
		[alignmentButton removeFromSuperview];
	}
	
	[descView alignCenter:self];
	[[self window] setDelegate:self];
}

- (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] autorelease];
		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] autorelease];
		
		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];
		[self tableViewSelectionDidChange:nil];
	}
}

- (NSArray *)plugins
{
	// This will hide the Face plugin stub, which is really only used when outputing data
	NSMutableArray *goodPlugins = [NSMutableArray array];
	NSEnumerator *plugEnum = [[[MPT_PluginController defaultController] plugins] objectEnumerator];
	id<MPT_PluginProtocol> plugin;
	while( plugin = [plugEnum nextObject] )
	{
		if( ![[plugin step] isEqualToString:MPT_PluginStep_NoStep] )
			[goodPlugins addObject:plugin];
	}
	return goodPlugins;
	//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
{
	
}

- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
	NSArray *selectedPlugins = [pluginController selectedObjects];
	[pluginPrefBox setContentView:nil];
	if( [selectedPlugins count] > 0 )
	{
		id<MPT_PluginProtocol> plugin = [selectedPlugins objectAtIndex:0];
		NSView *pluginView = [plugin prefPane];
		if( pluginView )
		{
			[pluginPrefBox setContentView:pluginView];
			
//			NSWindow *win = [self window];
//			NSRect rect = [win frame];
//			float oldHeight = rect.size.height;
//			NSArray *subviews = [pluginPrefs subviews];
//			NSEnumerator *subEnum = [subviews objectEnumerator];
//			NSView *aView;
//			rect.size.height = 70 + [pluginView frame].size.height;
//			while( aView = [subEnum nextObject] )
//			{
//				rect.size.height += [aView frame].size.height;
//			}
//			rect.origin.y += oldHeight - rect.size.height;
//			[win setFrame:rect display:YES animate:YES];
		}
	}
}

// Hack to disable the face alignement items that are not yet working.
- (BOOL)enableAlignments
{
	return YES;
}

- (IBAction)faceDisplayTypeChanged:(id)sender
{
	[[NSNotificationCenter defaultCenter] postNotificationName:faceDisplayTypeNotification object:self];
}

- (BOOL)performKeyEquivalent:(NSEvent *)theEvent
{
	if( NSKeyDown == [theEvent type] && (NSCommandKeyMask & [theEvent modifierFlags]) )
	{
		NSString *chars = [theEvent charactersIgnoringModifiers];
		if( [chars isEqualToString:@"1"] && ![[toolbar selectedItemIdentifier] isEqualToString:MPT_ToolbarItemIdentifierGeneral] )
		{
			[toolbar setSelectedItemIdentifier:MPT_ToolbarItemIdentifierGeneral];
			[self activatePrefPane:generalToolbarItem];
			return YES;
		}
		else if( [chars isEqualToString:@"2"] && ![[toolbar selectedItemIdentifier] isEqualToString:MPT_ToolbarItemIdentifierPlugins] )
		{
			[toolbar setSelectedItemIdentifier:MPT_ToolbarItemIdentifierPlugins];
			[self activatePrefPane:pluginToolbarItem];
			return YES;
		}
	}
	return NO;
}

- (MPT_AUAppDelegate *)appDelegate
{
    return appDelegate;
}
- (void)setAppDelegate:(MPT_AUAppDelegate *)value
{
    if( appDelegate != value )
    {
        [appDelegate release];
        appDelegate = [value retain];
    }
}
@end
