// This code was created from the Stanford iTunesU iPhone Application Development course
// This particular implementation was created by Keith McDermott, http://keithmcd.com 2009-05-01
// Assignment 1B
//
// This code is being released under the Creative Commons Attribution-Share Alike 3.0 License 
// http://creativecommons.org/licenses/by-sa/3.0/us/

#import <Foundation/Foundation.h>

void PrintPathInfo();
void PrintProcessInfo();
void PrintBookmarkInfo();
void PrintIntrospectionInfo();

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    PrintPathInfo();
	PrintProcessInfo();
	PrintBookmarkInfo();
	PrintIntrospectionInfo();
	
	[pool release];
    return 0;
}

// Section 1 of the assignment
void PrintPathInfo()
{
	NSLog(@"PrintPathInfo Method");
	
	//Get the full path of the tilde and log it
	NSString *path = @"~";
	path = [path stringByExpandingTildeInPath];
	NSLog(@"My home folder is at '%@'", path);
	
	// Destruct the path
	NSArray *array = [path pathComponents];	
	for (NSString *element in array) {
		NSLog(@"The path can be exploded to: %@", element);
	}
}

// Section 2 of the assignment
void PrintProcessInfo()
{
	NSLog(@"PrintProcessInfo Method");
	
	//Get and display process info
	NSString *processName = [[NSProcessInfo processInfo] processName];
	int processID = [[NSProcessInfo processInfo] processIdentifier];
	
	NSLog(@"Process Name: '%@' Process ID: '%u'", processName, processID);
}

// Section 3 of the assignment
void PrintBookmarkInfo()
{
	NSLog(@"PrintBookmarkInfo Method");
	
	//Declare our dictionary
	NSMutableDictionary *bookmarks = [NSMutableDictionary dictionary];
	
	//Set the items
	[bookmarks setObject:@"http://www.stanford.edu" forKey:@"Stanford University"];
	[bookmarks setObject:@"http://www.apple.com" forKey:@"Apple"];
	[bookmarks setObject:@"http://cs193p.stanford.edu" forKey:@"CS193p"];
	[bookmarks setObject:@"http://itunes.stanford.edu" forKey:@"Stanford on iTunes U"];
	[bookmarks setObject:@"http://stanfordshop.com" forKey:@"Stanford Mall"];
	
	//Enumerate through the values
	for (NSString *element in bookmarks) {
		NSString *key = element;
		NSURL *value = [bookmarks objectForKey:element];
		
		//Only output stanford stuff
		if ([key hasPrefix:@"Stanford"]) {
			NSLog(@"Key: '%@' URL: '%@'", key, value);
		}
	}
}

// Section 4 of the assignment
void PrintIntrospectionInfo()
{
	NSLog(@"PrintIntrospectionInfo Method");
	
	//Populate a Mutable Array with about 4-5 items of various types
	NSMutableArray *introspect = [NSMutableArray arrayWithCapacity:5];
	
	//Set the items
	NSString *processName = [[NSProcessInfo processInfo] processName];
	NSMutableDictionary *bookmarks = [NSMutableDictionary dictionary];
	[bookmarks setObject:@"http://keithmcd.com" forKey:@"Keith's site"];
	
	NSString *path = [@"~" stringByExpandingTildeInPath];
	NSString *appleURL = @"http://apple.com";
//	NSURL *appleURL = @"http://apple.com";
	
	[introspect addObject:processName];
	[introspect addObject:bookmarks];
	[introspect addObject:path];
	[introspect addObject:appleURL];
	//int counter = 0;
	for (NSObject *object in introspect) {
		//counter++;
		//NSLog(@"Iteration: %u", counter);
		NSLog(@"Class name: %@", [object className]);
		if( [object isMemberOfClass:[NSString class]]) {
			NSLog(@"Is Member of NSString: YES");
		} else {
			NSLog(@"Is Member of NSString: NO");
		}
		if( [object isKindOfClass:[NSString class]]) {
			NSLog(@"Is Kind of NSString: YES");
		} else {
			NSLog(@"Is Kind of NSString: NO");
		}
		if( [object respondsToSelector:@selector(lowercaseString)]) {
			NSLog(@"Responds to lowercaseString: YES");
			NSLog(@"lowercaseString is: %@",[object lowercaseString]);
		} else {
			NSLog(@"Responds to lowercaseString: NO");
		}
		//if([object respondsToSelector:lowercaseString]) {
		//	NSLog(@"lowercaseString is: %@", [object performSelector:lowercaseString]);
		//}
	}
}