//
//  JavenIdCard.m
//
//  Created by Z  on 16/5/3
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "JavenIdCard.h"


NSString *const kJavenIdCardIdType = @"idType";
NSString *const kJavenIdCardIdNum = @"idNum";


@interface JavenIdCard ()

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

@end

@implementation JavenIdCard

@synthesize idType = _idType;
@synthesize idNum = _idNum;


+ (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.idType = [self objectOrNilForKey:kJavenIdCardIdType fromDictionary:dict];
            self.idNum = [self objectOrNilForKey:kJavenIdCardIdNum fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.idType forKey:kJavenIdCardIdType];
    [mutableDict setValue:self.idNum forKey:kJavenIdCardIdNum];

    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.idType = [aDecoder decodeObjectForKey:kJavenIdCardIdType];
    self.idNum = [aDecoder decodeObjectForKey:kJavenIdCardIdNum];
    return self;
}

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

    [aCoder encodeObject:_idType forKey:kJavenIdCardIdType];
    [aCoder encodeObject:_idNum forKey:kJavenIdCardIdNum];
}

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

        copy.idType = [self.idType copyWithZone:zone];
        copy.idNum = [self.idNum copyWithZone:zone];
    }
    
    return copy;
}


@end
