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

#import "JavenCertificate.h"
#import "JavenBankCards.h"


NSString *const kJavenCertificateIdValidated = @"idValidated";
NSString *const kJavenCertificateBankCards = @"bankCards";
NSString *const kJavenCertificateTrueName = @"trueName";


@interface JavenCertificate ()

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

@end

@implementation JavenCertificate

@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:kJavenCertificateIdValidated fromDictionary:dict] boolValue];
        NSObject *receivedJavenBankCards = [dict objectForKey:kJavenCertificateBankCards];
        NSMutableArray *parsedJavenBankCards = [NSMutableArray array];
        if ([receivedJavenBankCards isKindOfClass:[NSArray class]]) {
            for (NSDictionary *item in (NSArray *)receivedJavenBankCards) {
                if ([item isKindOfClass:[NSDictionary class]]) {
                    [parsedJavenBankCards addObject:[JavenBankCards modelObjectWithDictionary:item]];
                }
            }
        } else if ([receivedJavenBankCards isKindOfClass:[NSDictionary class]]) {
            [parsedJavenBankCards addObject:[JavenBankCards modelObjectWithDictionary:(NSDictionary *)receivedJavenBankCards]];
        }
        
        self.bankCards = [NSArray arrayWithArray:parsedJavenBankCards];
        self.trueName = [self objectOrNilForKey:kJavenCertificateTrueName fromDictionary:dict];
        
    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[NSNumber numberWithBool:self.idValidated] forKey:kJavenCertificateIdValidated];
    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:kJavenCertificateBankCards];
    [mutableDict setValue:self.trueName forKey:kJavenCertificateTrueName];
    
    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:kJavenCertificateIdValidated];
    self.bankCards = [aDecoder decodeObjectForKey:kJavenCertificateBankCards];
    self.trueName = [aDecoder decodeObjectForKey:kJavenCertificateTrueName];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    
    [aCoder encodeBool:_idValidated forKey:kJavenCertificateIdValidated];
    [aCoder encodeObject:_bankCards forKey:kJavenCertificateBankCards];
    [aCoder encodeObject:_trueName forKey:kJavenCertificateTrueName];
}

- (id)copyWithZone:(NSZone *)zone
{
    JavenCertificate *copy = [[JavenCertificate alloc] init];
    
    if (copy) {
        
        copy.idValidated = self.idValidated;
        copy.bankCards = [self.bankCards copyWithZone:zone];
        copy.trueName = [self.trueName copyWithZone:zone];
    }
    
    return copy;
}


@end
