/***
 * Excerpted from "iOS Recipes",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/cdirec for more book information.
***/
//
//  SharedTableViewController.m
//  IntrospectedColors
//
//  Created by Paul Warren on 2/3/11.
//  Copyright 2011 Primitive Dog Software. All rights reserved.
//

#import "SharedTableViewController.h"


@implementation SharedTableViewController

@synthesize colors;

- (NSArray *)getTransitionMethods {
	
	NSMutableArray *methodArray = [NSMutableArray array];
	Class introClass = [UIColor class];
	
	// Next, look for the methods
	unsigned int methodCount = 0;
	Method *mlist = class_copyMethodList(object_getClass(introClass), &methodCount);
	if (mlist != NULL) {
		int i;
		for (i = 0; i < methodCount; ++i) {
			
			NSString *methodName = [NSString stringWithCString:sel_getName(method_getName(mlist[i])) encoding:NSASCIIStringEncoding];
            int index = [methodName length]-5;
			if (([methodName length] > 5) && [[methodName substringFromIndex:index] isEqual:@"Color"]) {
				[methodArray addObject:methodName];
				NSLog(@"%@",methodName);
			}
		}
	}
	return [methodArray copy];
}

- (void)dealloc
{
    [super dealloc];
    [colors release]; colors = nil;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.colors = [self getTransitionMethods];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [colors count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView 
                             dequeueReusableCellWithIdentifier:
                             CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:
                 UITableViewCellStyleValue1 
                   reuseIdentifier:CellIdentifier] autorelease];
    }
    
    int index = [colors count] - indexPath.row-1;
    cell.textLabel.text = [colors objectAtIndex:index];
    
    UIColor *color = objc_msgSend([UIColor class], 
                        NSSelectorFromString(
                        [self.colors objectAtIndex:index]));
    
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.opaque = NO;
    cell.textLabel.backgroundColor = [UIColor clearColor];
    UIView *backView = [[UIView alloc] initWithFrame:cell.frame];
    backView.backgroundColor = color;
    cell.backgroundView = backView;
    [backView release];
                                                                         
    return cell;
}


@end
