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

#import "BusinessFunction.h"


NSString *const kBusinessFunctionFunctionPic = @"functionPic";
NSString *const kBusinessFunctionFunctionName = @"functionName";
NSString *const kBusinessFunctionType= @"type";

@interface BusinessFunction ()

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

@end

@implementation BusinessFunction

@synthesize functionPic = _functionPic;
@synthesize functionName = _functionName;


+ (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]]) {
            self.functionPic = [self objectOrNilForKey:kBusinessFunctionFunctionPic fromDictionary:dict];
            self.functionName = [self objectOrNilForKey:kBusinessFunctionFunctionName fromDictionary:dict];
        self.type = [dict[@"type"] integerValue];
    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.functionPic forKey:kBusinessFunctionFunctionPic];
    [mutableDict setValue:self.functionName forKey:kBusinessFunctionFunctionName];

    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.functionPic = [aDecoder decodeObjectForKey:kBusinessFunctionFunctionPic];
    self.functionName = [aDecoder decodeObjectForKey:kBusinessFunctionFunctionName];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_functionPic forKey:kBusinessFunctionFunctionPic];
    [aCoder encodeObject:_functionName forKey:kBusinessFunctionFunctionName];
}

- (id)copyWithZone:(NSZone *)zone
{
    BusinessFunction *copy = [[BusinessFunction alloc] init];
    
    if (copy) {

        copy.functionPic = [self.functionPic copyWithZone:zone];
        copy.functionName = [self.functionName copyWithZone:zone];
    }
    
    return copy;
}


@end
