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

#import "UserInfoPostalAddresses.h"


NSString *const kUserInfoPostalAddressesAddress = @"address";
NSString *const kUserInfoPostalAddressesCountry = @"country";
NSString *const kUserInfoPostalAddressesProvince = @"province";
NSString *const kUserInfoPostalAddressesCity = @"city";
NSString *const kUserInfoPostalAddressesZip = @"zip";
NSString *const kUserInfoPostalAddressesNation = @"nation";


@interface UserInfoPostalAddresses ()

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

@end

@implementation UserInfoPostalAddresses

@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:kUserInfoPostalAddressesAddress fromDictionary:dict];
            self.country = [self objectOrNilForKey:kUserInfoPostalAddressesCountry fromDictionary:dict];
            self.province = [self objectOrNilForKey:kUserInfoPostalAddressesProvince fromDictionary:dict];
            self.city = [self objectOrNilForKey:kUserInfoPostalAddressesCity fromDictionary:dict];
            self.zip = [self objectOrNilForKey:kUserInfoPostalAddressesZip fromDictionary:dict];
            self.nation = [self objectOrNilForKey:kUserInfoPostalAddressesNation fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.address forKey:kUserInfoPostalAddressesAddress];
    [mutableDict setValue:self.country forKey:kUserInfoPostalAddressesCountry];
    [mutableDict setValue:self.province forKey:kUserInfoPostalAddressesProvince];
    [mutableDict setValue:self.city forKey:kUserInfoPostalAddressesCity];
    [mutableDict setValue:self.zip forKey:kUserInfoPostalAddressesZip];
    [mutableDict setValue:self.nation forKey:kUserInfoPostalAddressesNation];

    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:kUserInfoPostalAddressesAddress];
    self.country = [aDecoder decodeObjectForKey:kUserInfoPostalAddressesCountry];
    self.province = [aDecoder decodeObjectForKey:kUserInfoPostalAddressesProvince];
    self.city = [aDecoder decodeObjectForKey:kUserInfoPostalAddressesCity];
    self.zip = [aDecoder decodeObjectForKey:kUserInfoPostalAddressesZip];
    self.nation = [aDecoder decodeObjectForKey:kUserInfoPostalAddressesNation];
    return self;
}

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

    [aCoder encodeObject:_address forKey:kUserInfoPostalAddressesAddress];
    [aCoder encodeObject:_country forKey:kUserInfoPostalAddressesCountry];
    [aCoder encodeObject:_province forKey:kUserInfoPostalAddressesProvince];
    [aCoder encodeObject:_city forKey:kUserInfoPostalAddressesCity];
    [aCoder encodeObject:_zip forKey:kUserInfoPostalAddressesZip];
    [aCoder encodeObject:_nation forKey:kUserInfoPostalAddressesNation];
}

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