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

#import "UserInfoBankCards.h"
#import "UserInfoOwner.h"
#import "UserInfoAddress.h"


NSString *const kUserInfoBankCardsOwner = @"owner";
NSString *const kUserInfoBankCardsCardNo = @"cardNo";
NSString *const kUserInfoBankCardsBranchBank = @"branchBank";
NSString *const kUserInfoBankCardsAddress = @"address";
NSString *const kUserInfoBankCardsUuid = @"uuid";
NSString *const kUserInfoBankCardsBank = @"bank";
NSString *const kUserInfoBankCardsCarrier = @"carrier";


@interface UserInfoBankCards ()

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

@end

@implementation UserInfoBankCards

@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 = [UserInfoOwner modelObjectWithDictionary:[dict objectForKey:kUserInfoBankCardsOwner]];
            self.cardNo = [self objectOrNilForKey:kUserInfoBankCardsCardNo fromDictionary:dict];
            self.branchBank = [self objectOrNilForKey:kUserInfoBankCardsBranchBank fromDictionary:dict];
            self.address = [UserInfoAddress modelObjectWithDictionary:[dict objectForKey:kUserInfoBankCardsAddress]];
            self.uuid = [self objectOrNilForKey:kUserInfoBankCardsUuid fromDictionary:dict];
            self.bank = [self objectOrNilForKey:kUserInfoBankCardsBank fromDictionary:dict];
            self.carrier = [self objectOrNilForKey:kUserInfoBankCardsCarrier fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[self.owner dictionaryRepresentation] forKey:kUserInfoBankCardsOwner];
    [mutableDict setValue:self.cardNo forKey:kUserInfoBankCardsCardNo];
    [mutableDict setValue:self.branchBank forKey:kUserInfoBankCardsBranchBank];
    [mutableDict setValue:[self.address dictionaryRepresentation] forKey:kUserInfoBankCardsAddress];
    [mutableDict setValue:self.uuid forKey:kUserInfoBankCardsUuid];
    [mutableDict setValue:self.bank forKey:kUserInfoBankCardsBank];
    [mutableDict setValue:self.carrier forKey:kUserInfoBankCardsCarrier];

    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:kUserInfoBankCardsOwner];
    self.cardNo = [aDecoder decodeObjectForKey:kUserInfoBankCardsCardNo];
    self.branchBank = [aDecoder decodeObjectForKey:kUserInfoBankCardsBranchBank];
    self.address = [aDecoder decodeObjectForKey:kUserInfoBankCardsAddress];
    self.uuid = [aDecoder decodeObjectForKey:kUserInfoBankCardsUuid];
    self.bank = [aDecoder decodeObjectForKey:kUserInfoBankCardsBank];
    self.carrier = [aDecoder decodeObjectForKey:kUserInfoBankCardsCarrier];
    return self;
}

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

    [aCoder encodeObject:_owner forKey:kUserInfoBankCardsOwner];
    [aCoder encodeObject:_cardNo forKey:kUserInfoBankCardsCardNo];
    [aCoder encodeObject:_branchBank forKey:kUserInfoBankCardsBranchBank];
    [aCoder encodeObject:_address forKey:kUserInfoBankCardsAddress];
    [aCoder encodeObject:_uuid forKey:kUserInfoBankCardsUuid];
    [aCoder encodeObject:_bank forKey:kUserInfoBankCardsBank];
    [aCoder encodeObject:_carrier forKey:kUserInfoBankCardsCarrier];
}

- (id)copyWithZone:(NSZone *)zone
{
    UserInfoBankCards *copy = [[UserInfoBankCards 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
