//
//  JavenConsumer.m
//
//  Created by Z  on 16/4/28
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "JavenConsumer.h"


NSString *const kJavenConsumerMobilephone = @"mobilephone";
NSString *const kJavenConsumerCode = @"code";
NSString *const kJavenConsumerName = @"name";
NSString *const kJavenConsumerPortrait = @"portrait";
NSString *const kJavenConsumerUuid = @"uuid";


@interface JavenConsumer ()

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

@end

@implementation JavenConsumer

@synthesize mobilephone = _mobilephone;
@synthesize code = _code;
@synthesize name = _name;
@synthesize portrait = _portrait;
@synthesize uuid = _uuid;


+ (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.mobilephone = [self objectOrNilForKey:kJavenConsumerMobilephone fromDictionary:dict];
            self.code = [self objectOrNilForKey:kJavenConsumerCode fromDictionary:dict];
            self.name = [self objectOrNilForKey:kJavenConsumerName fromDictionary:dict];
            self.portrait = [self objectOrNilForKey:kJavenConsumerPortrait fromDictionary:dict];
            self.uuid = [self objectOrNilForKey:kJavenConsumerUuid fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.mobilephone forKey:kJavenConsumerMobilephone];
    [mutableDict setValue:self.code forKey:kJavenConsumerCode];
    [mutableDict setValue:self.name forKey:kJavenConsumerName];
    [mutableDict setValue:self.portrait forKey:kJavenConsumerPortrait];
    [mutableDict setValue:self.uuid forKey:kJavenConsumerUuid];

    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.mobilephone = [aDecoder decodeObjectForKey:kJavenConsumerMobilephone];
    self.code = [aDecoder decodeObjectForKey:kJavenConsumerCode];
    self.name = [aDecoder decodeObjectForKey:kJavenConsumerName];
    self.portrait = [aDecoder decodeObjectForKey:kJavenConsumerPortrait];
    self.uuid = [aDecoder decodeObjectForKey:kJavenConsumerUuid];
    return self;
}

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

    [aCoder encodeObject:_mobilephone forKey:kJavenConsumerMobilephone];
    [aCoder encodeObject:_code forKey:kJavenConsumerCode];
    [aCoder encodeObject:_name forKey:kJavenConsumerName];
    [aCoder encodeObject:_portrait forKey:kJavenConsumerPortrait];
    [aCoder encodeObject:_uuid forKey:kJavenConsumerUuid];
}

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

        copy.mobilephone = [self.mobilephone copyWithZone:zone];
        copy.code = [self.code copyWithZone:zone];
        copy.name = [self.name copyWithZone:zone];
        copy.portrait = [self.portrait copyWithZone:zone];
        copy.uuid = [self.uuid copyWithZone:zone];
    }
    
    return copy;
}


@end
