
#import "AppController.h"
#import <MiscFoundation/MiscSortedCollection.h>
#import "TestObject.h"

static NSString *opSeparator = @"\n----------------------------------------------------------------------\n";

@interface NSText(LogExtensions)

- (void)printString:(NSString *)aString;

@end

@implementation NSText(LogExtensions)

- (void)printString:(NSString *)aString
{
	NSRange theRange;

	theRange = NSMakeRange([[self string] length],0);
	[self replaceCharactersInRange:theRange withString:aString];
        theRange = NSMakeRange([[self string] length],0);
        [self scrollRangeToVisible:theRange];
}

@end

@implementation AppController

- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
	numberSkipList = [[MiscMutableSortedCollection numberCollection] retain];
	stringSkipList = [[MiscMutableSortedCollection stringCollection] retain];
	objectSkipList = [[MiscMutableSortedCollection collection] retain];
	[window makeKeyAndOrderFront:nil];
}
				
- (void)delete
{
	id object;
	
	if([radio selectedTag] == 1){
		object = [valueField stringValue];
		[stringSkipList removeAllObjectsOrderedSameAs:object];
		[textView printString:[(MiscSortedCollection *)stringSkipList description]];
	} else if([radio selectedTag] == 0){
		object = [NSNumber numberWithFloat:[valueField floatValue]];
		[numberSkipList removeAllObjectsOrderedSameAs:object];
		[textView printString:[(MiscSortedCollection *)numberSkipList description]];
	} else {
		object = [TestObject objectWithString:[valueField stringValue]];
		[objectSkipList removeAllObjectsOrderedSameAs:object];
		[textView printString:[(MiscSortedCollection *)objectSkipList description]];
	}
}

- (void)insert
{
	id object;
	
	if([radio selectedTag] == 1){
		object = [valueField stringValue];
		[stringSkipList insertObject:object];
		[textView printString:[(MiscSortedCollection *)stringSkipList description]];
	} else if([radio selectedTag] == 0){
		object = [NSNumber numberWithFloat:[valueField floatValue]];
		[numberSkipList insertObject:object];
		[textView printString:[(MiscSortedCollection *)numberSkipList description]];
	} else {
		object = [TestObject objectWithString:[valueField stringValue]];
		[objectSkipList insertObject:object];
		[textView printString:[(MiscSortedCollection *)objectSkipList description]];
	}
}

- (void)before
{
	id object, result;
	
	if([radio selectedTag] == 1){
		object = [valueField stringValue];
		result = [stringSkipList objectBefore:object];
	} else if([radio selectedTag] == 0){
		object = [NSNumber numberWithFloat:[valueField floatValue]];
		result = [numberSkipList objectBefore:object];
	} else {
		object = [TestObject objectWithString:[valueField stringValue]];
		result = [objectSkipList objectBefore:object];
	}
	if(result)
		[textView printString:[(NSObject *)result description]];
	else
		[textView printString:@"Nil object"];
}

- (void)same
{
	id object, resultArray;
	
	if([radio selectedTag] == 1){
		object = [valueField stringValue];
		resultArray = [stringSkipList allObjectsOrderedSameAs:object];
	} else if([radio selectedTag] == 0){
		object = [NSNumber numberWithFloat:[valueField floatValue]];
		resultArray = [numberSkipList allObjectsOrderedSameAs:object];
	} else {
		object = [TestObject objectWithString:[valueField stringValue]];
		resultArray = [objectSkipList allObjectsOrderedSameAs:object];
	}
	[textView printString:[(NSArray *)resultArray description]];
}

- (void)removeAll:(id)sender
{	
	[textView printString:opSeparator];
	if([radio selectedTag] == 1){
		[stringSkipList removeAllObjects];
		[textView printString:[(MiscSortedCollection *)stringSkipList description]];
	} else if([radio selectedTag] == 0){
		[numberSkipList removeAllObjects];
		[textView printString:[(MiscSortedCollection *)numberSkipList description]];
	} else {
		[objectSkipList removeAllObjects];
		[textView printString:[(MiscSortedCollection *)objectSkipList description]];
	}
}

- (void)showSkipList:(id)sender
{
	[textView printString:opSeparator];
	if([radio selectedTag] == 1){
		[textView printString:[(MiscSortedCollection *)stringSkipList description]];
	} else if([radio selectedTag] == 0){
		[textView printString:[(MiscSortedCollection *)numberSkipList description]];
	} else {
		[textView printString:[(MiscSortedCollection *)objectSkipList description]];
	}
}


- (void)clear:(id)sender
{
	[textView setString:@""];
}

- (void)performOp:(id)sender
{
	[textView printString:opSeparator];
	switch([opRadio selectedTag]){
		case 0:
				[self insert];
				break;
		case 1:
				[self delete];
				break;
		case 2:
				[self before];
				break;
		case 3:
				[self same];
				break;
		default:
				break;
	}		
	[valueField selectText:self];
}

@end
