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

#import "JavenPostalAddresses.h"


NSString *const kJavenPostalAddressesAddress = @"address";
NSString *const kJavenPostalAddressesCountry = @"country";
NSString *const kJavenPostalAddressesProvince = @"province";
NSString *const kJavenPostalAddressesCity = @"city";
NSString *const kJavenPostalAddressesZip = @"zip";
NSString *const kJavenPostalAddressesNation = @"nation";


@interface JavenPostalAddresses ()

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

@end

@implementation JavenPostalAddresses

@synthesize address = _address;
@synthesize country = _country;
@synthesize province = _province;
@synthesize city = _city;
@synthesize zip = _zip;
@synthesize nation = _nation;


+ (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.address = [self objectOrNilForKey:kJavenPostalAddressesAddress fromDictionary:dict];
            self.country = [self objectOrNilForKey:kJavenPostalAddressesCountry fromDictionary:dict];
            self.province = [self objectOrNilForKey:kJavenPostalAddressesProvince fromDictionary:dict];
            self.city = [self objectOrNilForKey:kJavenPostalAddressesCity fromDictionary:dict];
            self.zip = [self objectOrNilForKey:kJavenPostalAddressesZip fromDictionary:dict];
            self.nation = [self objectOrNilForKey:kJavenPostalAddressesNation fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.address forKey:kJavenPostalAddressesAddress];
    [mutableDict setValue:self.country forKey:kJavenPostalAddressesCountry];
    [mutableDict setValue:self.province forKey:kJavenPostalAddressesProvince];
    [mutableDict setValue:self.city forKey:kJavenPostalAddressesCity];
    [mutableDict setValue:self.zip forKey:kJavenPostalAddressesZip];
    [mutableDict setValue:self.nation forKey:kJavenPostalAddressesNation];

    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.address = [aDecoder decodeObjectForKey:kJavenPostalAddressesAddress];
    self.country = [aDecoder decodeObjectForKey:kJavenPostalAddressesCountry];
    self.province = [aDecoder decodeObjectForKey:kJavenPostalAddressesProvince];
    self.city = [aDecoder decodeObjectForKey:kJavenPostalAddressesCity];
    self.zip = [aDecoder decodeObjectForKey:kJavenPostalAddressesZip];
    self.nation = [aDecoder decodeObjectForKey:kJavenPostalAddressesNation];
    return self;
}

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

    [aCoder encodeObject:_address forKey:kJavenPostalAddressesAddress];
    [aCoder encodeObject:_country forKey:kJavenPostalAddressesCountry];
    [aCoder encodeObject:_province forKey:kJavenPostalAddressesProvince];
    [aCoder encodeObject:_city forKey:kJavenPostalAddressesCity];
    [aCoder encodeObject:_zip forKey:kJavenPostalAddressesZip];
    [aCoder encodeObject:_nation forKey:kJavenPostalAddressesNation];
}

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

        copy.address = [self.address copyWithZone:zone];
        copy.country = [self.country copyWithZone:zone];
        copy.province = [self.province copyWithZone:zone];
        copy.city = [self.city copyWithZone:zone];
        copy.zip = [self.zip copyWithZone:zone];
        copy.nation = [self.nation copyWithZone:zone];
    }
    
    return copy;
}


@end
