//
//  AppDelegate.m
//  OminoGoboTests
//
//  Created by David Van Brink on 1/5/14.
//  Copyright (c) 2014 omino. All rights reserved.
//

#import "AppDelegate.h"
#import "GlobalTicker.h"
#import "OmTestView.h"
#import "TestOmGrid.h"
#import "objc/runtime.h"
#import "objc/objc.h"
#import "objc/message.h"
#import "objc/objc-api.h"
@implementation AppDelegate


static void (*_method_invoke_void)(id, Method, ...) = (void (*)(id, Method, ...)) method_invoke;

static BOOL startsWith(const char *s, const char *prefix)
{
    while(*s && *prefix)
    {
        if(*s++ != *prefix++)
            return NO;
    }
    if(*prefix)
        return NO;
    return YES;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Device screen
    [GlobalTicker start];
    OmView *ov = [[OmTestView alloc] initWithScreen:nil];
    self.window = ov.window;
    [self.window makeKeyAndVisible];
    
    unsigned int methodCount;
    
    unsigned int classCount = 0;
    Class *classes = objc_copyClassList(&classCount);
    
#define MAX_TEST_CLASSES 200
    int testClassCount = 0;
    Class *testClasses = (Class *)calloc(sizeof(Class), MAX_TEST_CLASSES);

    for(int i = 0; i < classCount; i++)
    {
        Class class = classes[i];
        const char * cName = class_getName(class);
        if(startsWith(cName,"Test"))
        {
            if(testClassCount < MAX_TEST_CLASSES)
            {
                testClasses[testClassCount++] = class;
            }
        }
    }

    // All classes with the prefix are in testClasses. Ok.
    
    free(classes);
    classes = 0;
    
    
    for(int i = 0; i < testClassCount; i++)
    {
        Class class = testClasses[i];
        const char * cName = class_getName(class);
        if(startsWith(cName,"Test"))
        {
            // We have a candidate class, instantiate it and iterate the methods.
            [omTestView log:"class %s",cName];
            Method * methods = class_copyMethodList(class, &methodCount);
            id obj = [[class alloc] init];
            
            for(int i = 0; i < methodCount; i++)
            {
                Method method = methods[i];
                SEL sel = method_getName(method);
                NSString *selNss = NSStringFromSelector(sel);
                const char *selS = [selNss cStringUsingEncoding:NSASCIIStringEncoding];
                if(!startsWith(selS, "test"))
                    continue;
                [omTestView log:"[%s]",selS];
                
                // Note about method_invoke on void results, which these tests are:
                // http://stackoverflow.com/questions/19694173/using-objective-cs-invoke-method-to-call-a-void-method-under-arc
                _method_invoke_void((id)obj,method);
                //        (void)((__bridge void *)method_invoke(obj, method));
            }
        }
    }

    return YES;
}

//- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//{
//    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//    // Override point for customization after application launch.
//    self.window.backgroundColor = [UIColor whiteColor];
//    [self.window makeKeyAndVisible];
//    return YES;
//}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end


int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}
