#import "AppController.h"
#import "NSTableView+util.h"
#import "NSString+tsv.h"
#import "DeletableTableView.h"

@implementation AppController

- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem
{
	// Make sure that there is a table attached to the initialFirstResponder of the tab view item.
	id currentTable = [[oTabView selectedTabViewItem] initialFirstResponder];

	if (nil == currentTable) NSLog(@"WARNING -- nil initialFirstResponder for %@", [tabViewItem label]);
}

- (void)addCornerView
{
	// Center the "+" icon in the corner view as a subview of the background.
	NSRect cornerViewRect = [[oTableForCornerView cornerView] bounds];
	NSRect viewRect = [oCornerView bounds];
	NSPoint origin = NSMakePoint((NSWidth(cornerViewRect)-NSWidth(viewRect))/2.0, (NSHeight(cornerViewRect)-NSHeight(viewRect))/2.0);

	[oCornerView setFrameOrigin:origin];
	[[oTableForCornerView cornerView] addSubview:oCornerView];
}


- (void)awakeFromNib
{
	NSString *path = [[NSBundle mainBundle] pathForResource:@"software" ofType:@"tsv"];
	NSString *tsv = [NSString stringWithContentsOfFile:path];
	[oSoftwareArray addObjectsFromArray:[tsv arrayFromTSV]];

	path = [[NSBundle mainBundle] pathForResource:@"database" ofType:@"tsv"];
	tsv = [NSString stringWithContentsOfFile:path];
	[oPeopleArray addObjectsFromArray:[tsv arrayFromTSV]];

	path = [[NSBundle mainBundle] pathForResource:@"friends" ofType:@"tsv"];
	tsv = [NSString stringWithContentsOfFile:path];
	[oFriendsArray addObjectsFromArray:[tsv arrayFromTSV]];

	path = [[NSBundle mainBundle]
			pathForResource:@"schedule" ofType:@"plist"];
	[oScheduleArray addObjectsFromArray:[NSArray arrayWithContentsOfFile:path]];

	// Initialize current tab view
	[self tabView:oTabView didSelectTabViewItem:[oTabView selectedTabViewItem]];

	[self addCornerView];
}


/*"	Copy the selected rows.
"*/

- (IBAction) copy:(id)sender
{
	// Get the NSTableView we want to copy from.  In this case, we determine the "current" table view by getting the tab view item's initialFirstResponder, which is set in the nib.
	id currentTable = [[oTabView selectedTabViewItem] initialFirstResponder];

	// Now put the selected rows in the general pasteboard.
	if ([[currentTable dataSource] respondsToSelector:@selector(tableView:writeRows:toPasteboard:)])
	{
		(void) [[currentTable dataSource] tableView:currentTable writeRows:[currentTable selectedRows] toPasteboard:[NSPasteboard generalPasteboard]];
	}
}

/*"	delete the selected rows.  "Clear" item in "Edit" menu connects to delete: method in first responder.  We have the data source actually do the deleting.
"*/

- (IBAction) delete:(id)sender
{
	// Get the NSTableView we want to delete from.  In this case, we determine the "current" table view by getting the tab view item's initialFirstResponder, which is set in the nib.
	id currentTable = [[oTabView selectedTabViewItem] initialFirstResponder];
	if ([[currentTable dataSource] respondsToSelector:@selector(deleteSelectedRowsInTableView:)])
	{
		[[currentTable dataSource] deleteSelectedRowsInTableView:currentTable];
	}
}

/*"	Override respondsToSelector: to indicate whether deleting and copying can be performed, based upon current table's state."*/

- (BOOL)respondsToSelector:(SEL)aSelector
{
	BOOL result = NO;
	if (aSelector == @selector(copy:) || aSelector == @selector(delete:))
	{
		id currentTable = [[oTabView selectedTabViewItem] initialFirstResponder];
		if (nil != currentTable)
		{
			// allow copy/delete if there is a selected row.  We could be more sophisticated and make sure that the table supports copying/deleting, but this is close enough.
			result = ([currentTable selectedRow] >= 0);
		}
	}
	else	// not a special case, handle normally
	{
		result = [super respondsToSelector:aSelector];
	}
	return result;
}

- (BOOL) animateSorts
{
	return [oAnimateSortingCheckbox state];
}

@end
