//
//  BusinessFunctionMode.m
//
//  Created by 杰 张 on 16/7/29
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "BusinessFunctionModel.h"


NSString *const kBusinessFunctionModeFunction = @"function";
NSString *const kBusinessFunctionModeHeaderTitle = @"headerTitle";


@interface BusinessFunctionModel ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation BusinessFunctionModel

@synthesize function = _function;
@synthesize headerTitle = _headerTitle;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
        NSObject *receivedBusinessFunction = [dict objectForKey:kBusinessFunctionModeFunction];
        NSMutableArray *parsedBusinessFunction = [NSMutableArray array];
        if ([receivedBusinessFunction isKindOfClass:[NSArray class]]) {
            for (NSDictionary *item in (NSArray *)receivedBusinessFunction) {
                if ([item isKindOfClass:[NSDictionary class]]) {
                    [parsedBusinessFunction addObject:[BusinessFunction modelObjectWithDictionary:item]];
                }
            }
        } else if ([receivedBusinessFunction isKindOfClass:[NSDictionary class]]) {
            [parsedBusinessFunction addObject:[BusinessFunction modelObjectWithDictionary:(NSDictionary *)receivedBusinessFunction]];
        }
        
        self.function = [NSArray arrayWithArray:parsedBusinessFunction];
        self.headerTitle = [self objectOrNilForKey:kBusinessFunctionModeHeaderTitle fromDictionary:dict];
        
    }
    
    return self;
    
}

- (instancetype)initWithDictionaryPermissionControl:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
        NSObject *receivedBusinessFunction = [dict objectForKey:kBusinessFunctionModeFunction];
        NSMutableArray *parsedBusinessFunction = [NSMutableArray array];
        if ([receivedBusinessFunction isKindOfClass:[NSArray class]]) {
            for (NSDictionary *item in (NSArray *)receivedBusinessFunction) {
                if ([item isKindOfClass:[NSDictionary class]]) {
                    BusinessFunction *function = [BusinessFunction modelObjectWithDictionary:item];
                    switch (function.type) {
                        case functionTypeSaleInput:
                        {
                            if (AppGlobal.permission.saleinput.read) {
                                [parsedBusinessFunction addObject:function];
                            }
                        }
                            break;
                            
                        default:
                            break;
                    }
                    
                }
            }
        } else if ([receivedBusinessFunction isKindOfClass:[NSDictionary class]]) {
            [parsedBusinessFunction addObject:[BusinessFunction modelObjectWithDictionary:(NSDictionary *)receivedBusinessFunction]];
        }
        
        self.function = [NSArray arrayWithArray:parsedBusinessFunction];
        self.headerTitle = [self objectOrNilForKey:kBusinessFunctionModeHeaderTitle fromDictionary:dict];
        
    }
    
    return self;
    
}
- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    NSMutableArray *tempArrayForFunction = [NSMutableArray array];
    for (NSObject *subArrayObject in self.function) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForFunction addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForFunction addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForFunction] forKey:kBusinessFunctionModeFunction];
    [mutableDict setValue:self.headerTitle forKey:kBusinessFunctionModeHeaderTitle];
    
    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    
    self.function = [aDecoder decodeObjectForKey:kBusinessFunctionModeFunction];
    self.headerTitle = [aDecoder decodeObjectForKey:kBusinessFunctionModeHeaderTitle];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    
    [aCoder encodeObject:_function forKey:kBusinessFunctionModeFunction];
    [aCoder encodeObject:_headerTitle forKey:kBusinessFunctionModeHeaderTitle];
}

- (id)copyWithZone:(NSZone *)zone
{
    BusinessFunctionModel *copy = [[BusinessFunctionModel alloc] init];
    
    if (copy) {
        
        copy.function = [self.function copyWithZone:zone];
        copy.headerTitle = [self.headerTitle copyWithZone:zone];
    }
    
    return copy;
}


@end
