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

#import "UserInfoAddress.h"


NSString *const kUserInfoAddressAddress = @"address";
NSString *const kUserInfoAddressCountry = @"country";
NSString *const kUserInfoAddressProvince = @"province";
NSString *const kUserInfoAddressCity = @"city";
NSString *const kUserInfoAddressZip = @"zip";
NSString *const kUserInfoAddressNation = @"nation";


@interface UserInfoAddress ()

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

@end

@implementation UserInfoAddress

@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:kUserInfoAddressAddress fromDictionary:dict];
            self.country = [self objectOrNilForKey:kUserInfoAddressCountry fromDictionary:dict];
            self.province = [self objectOrNilForKey:kUserInfoAddressProvince fromDictionary:dict];
            self.city = [self objectOrNilForKey:kUserInfoAddressCity fromDictionary:dict];
            self.zip = [self objectOrNilForKey:kUserInfoAddressZip fromDictionary:dict];
            self.nation = [self objectOrNilForKey:kUserInfoAddressNation fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.address forKey:kUserInfoAddressAddress];
    [mutableDict setValue:self.country forKey:kUserInfoAddressCountry];
    [mutableDict setValue:self.province forKey:kUserInfoAddressProvince];
    [mutableDict setValue:self.city forKey:kUserInfoAddressCity];
    [mutableDict setValue:self.zip forKey:kUserInfoAddressZip];
    [mutableDict setValue:self.nation forKey:kUserInfoAddressNation];

    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:kUserInfoAddressAddress];
    self.country = [aDecoder decodeObjectForKey:kUserInfoAddressCountry];
    self.province = [aDecoder decodeObjectForKey:kUserInfoAddressProvince];
    self.city = [aDecoder decodeObjectForKey:kUserInfoAddressCity];
    self.zip = [aDecoder decodeObjectForKey:kUserInfoAddressZip];
    self.nation = [aDecoder decodeObjectForKey:kUserInfoAddressNation];
    return self;
}

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

    [aCoder encodeObject:_address forKey:kUserInfoAddressAddress];
    [aCoder encodeObject:_country forKey:kUserInfoAddressCountry];
    [aCoder encodeObject:_province forKey:kUserInfoAddressProvince];
    [aCoder encodeObject:_city forKey:kUserInfoAddressCity];
    [aCoder encodeObject:_zip forKey:kUserInfoAddressZip];
    [aCoder encodeObject:_nation forKey:kUserInfoAddressNation];
}

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