//
//  ZJPermissionManager.m
//  HDMall
//
//  Created by Javen on 2017/7/27.
//  Copyright © 2017年 上海勾芒信息科技. All rights reserved.
//

#import "ZJPermissionManager.h"

@implementation ZJPermissionManager

- (void)updatePermissions:(NSArray *)permissions {
    //(1)获取类的属性及属性对应的类型
    NSMutableArray * keys = [NSMutableArray array];
    NSMutableArray * attributes = [NSMutableArray array];
    /*
     * 例子
     * name = value3 attribute = T@"NSString",C,N,V_value3
     * name = value4 attribute = T^i,N,V_value4
     */
    unsigned int outCount;
    objc_property_t * properties = class_copyPropertyList([self class], &outCount);
    for (int i = 0; i < outCount; i ++) {
        objc_property_t property = properties[i];
        //通过property_getName函数获得属性的名字
        NSString * propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        [keys addObject:propertyName];
        //通过property_getAttributes函数可以获得属性的名字和@encode编码
        NSString * propertyAttribute = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];
        [attributes addObject:propertyAttribute];
    }
    //立即释放properties指向的内存
    free(properties);
    
    for (NSString *string in permissions) {
        NSArray *first = [string componentsSeparatedByString:@":"];
        NSString *permissionKey = [first firstObject];
        
        NSString *permissionValue = [first objectAtIndex:1];
        NSArray *values = [permissionValue componentsSeparatedByString:@";"];
        
        if ([keys containsObject:permissionKey]) {
            
            ZJPermission *permission = [ZJPermission modelWithDict:values];
            [self setValue:permission forKey:permissionKey];
        }
    }
    
}

@end

@implementation ZJPermission


+ (instancetype)modelWithDict:(NSArray *)arr {
    ZJPermission *model = [[ZJPermission alloc] initWithDict:arr];
    return model;
}
- (instancetype)initWithDict:(NSArray *)arr {
    
    if (self = [self init]) {
        //(1)获取类的属性及属性对应的类型
        NSMutableArray * keys = [NSMutableArray array];
        NSMutableArray * attributes = [NSMutableArray array];
        /*
         * 例子
         * name = value3 attribute = T@"NSString",C,N,V_value3
         * name = value4 attribute = T^i,N,V_value4
         */
        unsigned int outCount;
        objc_property_t * properties = class_copyPropertyList([self class], &outCount);
        for (int i = 0; i < outCount; i ++) {
            objc_property_t property = properties[i];
            //通过property_getName函数获得属性的名字
            NSString * propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
            [keys addObject:propertyName];
            //通过property_getAttributes函数可以获得属性的名字和@encode编码
            NSString * propertyAttribute = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];
            [attributes addObject:propertyAttribute];
        }
        //立即释放properties指向的内存
        free(properties);
        
        //(2)根据类型给属性赋值
        for (NSString * key in keys) {
            if ([arr containsObject:key]) {
                [self setValue:[NSNumber numberWithBool:YES] forKey:key];
            }else if ([key isEqualToString:@"newField"] && [arr containsObject:@"new"]) {
                [self setValue:[NSNumber numberWithBool:YES] forKey:key];
            }else{
                [self setValue:[NSNumber numberWithBool:NO] forKey:key];
            }
            
        }
    }
    return self;
    
}
@end
