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

#import "UserInfoCertificate.h"
#import "UserInfoBankCards.h"


NSString *const kUserInfoCertificateIdValidated = @"idValidated";
NSString *const kUserInfoCertificateBankCards = @"bankCards";
NSString *const kUserInfoCertificateTrueName = @"trueName";


@interface UserInfoCertificate ()

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

@end

@implementation UserInfoCertificate

@synthesize idValidated = _idValidated;
@synthesize bankCards = _bankCards;
@synthesize trueName = _trueName;


+ (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.idValidated = [[self objectOrNilForKey:kUserInfoCertificateIdValidated fromDictionary:dict] boolValue];
    NSObject *receivedUserInfoBankCards = [dict objectForKey:kUserInfoCertificateBankCards];
    NSMutableArray *parsedUserInfoBankCards = [NSMutableArray array];
    if ([receivedUserInfoBankCards isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedUserInfoBankCards) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedUserInfoBankCards addObject:[UserInfoBankCards modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedUserInfoBankCards isKindOfClass:[NSDictionary class]]) {
       [parsedUserInfoBankCards addObject:[UserInfoBankCards modelObjectWithDictionary:(NSDictionary *)receivedUserInfoBankCards]];
    }

    self.bankCards = [NSArray arrayWithArray:parsedUserInfoBankCards];
            self.trueName = [self objectOrNilForKey:kUserInfoCertificateTrueName fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[NSNumber numberWithBool:self.idValidated] forKey:kUserInfoCertificateIdValidated];
    NSMutableArray *tempArrayForBankCards = [NSMutableArray array];
    for (NSObject *subArrayObject in self.bankCards) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForBankCards addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForBankCards addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForBankCards] forKey:kUserInfoCertificateBankCards];
    [mutableDict setValue:self.trueName forKey:kUserInfoCertificateTrueName];

    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.idValidated = [aDecoder decodeBoolForKey:kUserInfoCertificateIdValidated];
    self.bankCards = [aDecoder decodeObjectForKey:kUserInfoCertificateBankCards];
    self.trueName = [aDecoder decodeObjectForKey:kUserInfoCertificateTrueName];
    return self;
}

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

    [aCoder encodeBool:_idValidated forKey:kUserInfoCertificateIdValidated];
    [aCoder encodeObject:_bankCards forKey:kUserInfoCertificateBankCards];
    [aCoder encodeObject:_trueName forKey:kUserInfoCertificateTrueName];
}

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

        copy.idValidated = self.idValidated;
        copy.bankCards = [self.bankCards copyWithZone:zone];
        copy.trueName = [self.trueName copyWithZone:zone];
    }
    
    return copy;
}


@end
