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

#import "UserInfoIdCard.h"


NSString *const kUserInfoIdCardIdType = @"idType";
NSString *const kUserInfoIdCardIdNum = @"idNum";


@interface UserInfoIdCard ()

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

@end

@implementation UserInfoIdCard

@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:kUserInfoIdCardIdType fromDictionary:dict];
            self.idNum = [self objectOrNilForKey:kUserInfoIdCardIdNum fromDictionary:dict];

    }
    
    return self;
    
}

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

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

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

    [aCoder encodeObject:_idType forKey:kUserInfoIdCardIdType];
    [aCoder encodeObject:_idNum forKey:kUserInfoIdCardIdNum];
}

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

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


@end
