//
//  NSObject+Customized.m
//  DingDCommunity
//
//  Created by JianjiYuan on 14-4-26.
//  Copyright (c) 2014年 JianjiYuan. All rights reserved.
//

#import "NSObject+Customized.h"
#import <objc/runtime.h>

@implementation NSObject (Customized)

//判断返回数据 是否为NULL
- (id)validDataOfClass:(Class)className
{
    //为空 或者 类型不对
    if (self == [NSNull null] || ![self isKindOfClass:[className class]]) {
        DLog(@"%@ is NULL or NOT referred class", self.description);
        if ([self isKindOfClass:[NSString class]]) {
            return @"";
        }else if ([self isKindOfClass:[NSNumber class]]) {
            return @0;
        }else if ([self isKindOfClass:[NSArray class]]) {
            return @[];
        }else if ([self isKindOfClass:[NSDictionary class]]) {
            return @{};
        }else {
            return [[[className class] alloc] init];
        }
    }
    if ([self isKindOfClass:[NSString class]]) {
        if ([(NSString *)self length] > 4
            && [[[(NSString *)self substringWithRange:NSMakeRange(1, 4)] lowercaseString] isEqualToString:@"null"]) {
            return @"";
        }
    }
    
    return self;
}

- (NSArray *)getPropertyList
{
    return [self getPropertyList:[self class]];
}

- (NSArray *)getPropertyList: (Class)clazz
{
    u_int count;
    objc_property_t *properties  = class_copyPropertyList(clazz, &count);
    NSMutableArray *propertyArray = [NSMutableArray arrayWithCapacity:count];
    
    for (int i = 0; i < count ; i++)
    {
        const char* propertyName = property_getName(properties[i]);
        [propertyArray addObject: [NSString  stringWithUTF8String: propertyName]];
    }
    
    free(properties);
    
    return propertyArray;
}

- (NSString *)tableSql:(NSString *)tablename
{
    NSMutableString *sql = [[NSMutableString alloc] init];
    NSArray *array = [self getPropertyList];
    [sql appendFormat:@"create table %@ (",tablename] ;
    NSInteger i = 0;
    for (NSString *key in array) {
        if (i>0) {
            [sql appendString:@","];
        }
        [sql appendFormat:@"%@ text",key];
        i++;
    }
    [sql appendString:@")"];
    return sql;
}

- (NSString *)tableSql
{
    return [self tableSql:[self className]];
}


- (NSDictionary *)convertDictionary
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    NSArray *propertyList = [self getPropertyList];
    for (NSString *key in propertyList) {
        SEL selector = NSSelectorFromString(key);
        
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        id value = [self performSelector:selector];
#pragma clang diagnostic pop
        
        if (value == nil) {
            value = [NSNull null];
        }
        [dict setObject:value forKey:key];
    }
    return dict;
}

- (id)initWithDictionary:(NSDictionary *)dict
{
    self = [self init];
    if(self)
        [self dictionaryForObject:dict];
    return self;
    
}

- (NSString *)className
{
    return [NSString stringWithUTF8String:object_getClassName(self)];
}

- (BOOL)checkPropertyName:(NSString *)name
{
    unsigned int propCount, i;
    objc_property_t* properties = class_copyPropertyList([self class], &propCount);
    for (i = 0; i < propCount; i++) {
        objc_property_t prop = properties[i];
        const char *propName = property_getName(prop);
        if(propName) {
            NSString *_name = [NSString stringWithCString:propName encoding:NSUTF8StringEncoding];
            if ([name isEqualToString:_name]) {
                return YES;
            }
        }
    }
    return NO;
}


- (void)dictionaryForObject:(NSDictionary*) dict
{
    for (NSString *key in [dict allKeys]) {
        id value = [dict objectForKey:key];
        
        if (value==[NSNull null]) {
            continue;
        }
        if ([value isKindOfClass:[NSDictionary class]]) {
            id subObj = [self valueForKey:key];
            if (subObj)
                [subObj dictionaryForObject:value];
        }
        else{
            [self setValue:value forKeyPath:key];
        }
    }
}


@end
