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

#import "JavenBankCards.h"



NSString *const kJavenBankCardsOwner = @"owner";
NSString *const kJavenBankCardsCardNo = @"cardNo";
NSString *const kJavenBankCardsBranchBank = @"branchBank";
NSString *const kJavenBankCardsAddress = @"address";
NSString *const kJavenBankCardsUuid = @"uuid";
NSString *const kJavenBankCardsBank = @"bank";
NSString *const kJavenBankCardsCarrier = @"carrier";


@interface JavenBankCards ()

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

@end

@implementation JavenBankCards

@synthesize owner = _owner;
@synthesize cardNo = _cardNo;
@synthesize branchBank = _branchBank;
@synthesize address = _address;
@synthesize uuid = _uuid;
@synthesize bank = _bank;
@synthesize carrier = _carrier;


+ (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.owner = [JavenOwner modelObjectWithDictionary:[dict objectForKey:kJavenBankCardsOwner]];
            self.cardNo = [self objectOrNilForKey:kJavenBankCardsCardNo fromDictionary:dict];
            self.branchBank = [self objectOrNilForKey:kJavenBankCardsBranchBank fromDictionary:dict];
            self.address = [JavenAddress modelObjectWithDictionary:[dict objectForKey:kJavenBankCardsAddress]];
            self.uuid = [self objectOrNilForKey:kJavenBankCardsUuid fromDictionary:dict];
            self.bank = [self objectOrNilForKey:kJavenBankCardsBank fromDictionary:dict];
            self.carrier = [self objectOrNilForKey:kJavenBankCardsCarrier fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[self.owner dictionaryRepresentation] forKey:kJavenBankCardsOwner];
    [mutableDict setValue:self.cardNo forKey:kJavenBankCardsCardNo];
    [mutableDict setValue:self.branchBank forKey:kJavenBankCardsBranchBank];
    [mutableDict setValue:[self.address dictionaryRepresentation] forKey:kJavenBankCardsAddress];
    [mutableDict setValue:self.uuid forKey:kJavenBankCardsUuid];
    [mutableDict setValue:self.bank forKey:kJavenBankCardsBank];
    [mutableDict setValue:self.carrier forKey:kJavenBankCardsCarrier];

    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.owner = [aDecoder decodeObjectForKey:kJavenBankCardsOwner];
    self.cardNo = [aDecoder decodeObjectForKey:kJavenBankCardsCardNo];
    self.branchBank = [aDecoder decodeObjectForKey:kJavenBankCardsBranchBank];
    self.address = [aDecoder decodeObjectForKey:kJavenBankCardsAddress];
    self.uuid = [aDecoder decodeObjectForKey:kJavenBankCardsUuid];
    self.bank = [aDecoder decodeObjectForKey:kJavenBankCardsBank];
    self.carrier = [aDecoder decodeObjectForKey:kJavenBankCardsCarrier];
    return self;
}

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

    [aCoder encodeObject:_owner forKey:kJavenBankCardsOwner];
    [aCoder encodeObject:_cardNo forKey:kJavenBankCardsCardNo];
    [aCoder encodeObject:_branchBank forKey:kJavenBankCardsBranchBank];
    [aCoder encodeObject:_address forKey:kJavenBankCardsAddress];
    [aCoder encodeObject:_uuid forKey:kJavenBankCardsUuid];
    [aCoder encodeObject:_bank forKey:kJavenBankCardsBank];
    [aCoder encodeObject:_carrier forKey:kJavenBankCardsCarrier];
}

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

        copy.owner = [self.owner copyWithZone:zone];
        copy.cardNo = [self.cardNo copyWithZone:zone];
        copy.branchBank = [self.branchBank copyWithZone:zone];
        copy.address = [self.address copyWithZone:zone];
        copy.uuid = [self.uuid copyWithZone:zone];
        copy.bank = [self.bank copyWithZone:zone];
        copy.carrier = [self.carrier copyWithZone:zone];
    }
    
    return copy;
}


@end
