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

#import "JavenAddress.h"
#import "JavenIdCard.h"


NSString *const kJavenAddressAddress = @"address";
NSString *const kJavenAddressCityCode = @"cityCode";
NSString *const kJavenAddressCountry = @"country";
NSString *const kJavenAddressCity = @"city";
NSString *const kJavenAddressIdCard = @"idCard";
NSString *const kJavenAddressNation = @"nation";
NSString *const kJavenAddressZip = @"zip";
NSString *const kJavenAddressTelephone = @"telephone";
NSString *const kJavenAddressName = @"name";
NSString *const kJavenAddressProvince = @"province";


@interface JavenAddress ()

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

@end

@implementation JavenAddress

@synthesize address = _address;
@synthesize cityCode = _cityCode;
@synthesize country = _country;
@synthesize city = _city;
@synthesize idCard = _idCard;
@synthesize nation = _nation;
@synthesize zip = _zip;
@synthesize telephone = _telephone;
@synthesize name = _name;
@synthesize province = _province;


+ (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:kJavenAddressAddress fromDictionary:dict];
            self.cityCode = [self objectOrNilForKey:kJavenAddressCityCode fromDictionary:dict];
            self.country = [self objectOrNilForKey:kJavenAddressCountry fromDictionary:dict];
            self.city = [self objectOrNilForKey:kJavenAddressCity fromDictionary:dict];
            self.idCard = [JavenIdCard modelObjectWithDictionary:[dict objectForKey:kJavenAddressIdCard]];
            self.nation = [self objectOrNilForKey:kJavenAddressNation fromDictionary:dict];
            self.zip = [self objectOrNilForKey:kJavenAddressZip fromDictionary:dict];
            self.telephone = [self objectOrNilForKey:kJavenAddressTelephone fromDictionary:dict];
            self.name = [self objectOrNilForKey:kJavenAddressName fromDictionary:dict];
            self.province = [self objectOrNilForKey:kJavenAddressProvince fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.address forKey:kJavenAddressAddress];
    [mutableDict setValue:self.cityCode forKey:kJavenAddressCityCode];
    [mutableDict setValue:self.country forKey:kJavenAddressCountry];
    [mutableDict setValue:self.city forKey:kJavenAddressCity];
    [mutableDict setValue:[self.idCard dictionaryRepresentation] forKey:kJavenAddressIdCard];
    [mutableDict setValue:self.nation forKey:kJavenAddressNation];
    [mutableDict setValue:self.zip forKey:kJavenAddressZip];
    [mutableDict setValue:self.telephone forKey:kJavenAddressTelephone];
    [mutableDict setValue:self.name forKey:kJavenAddressName];
    [mutableDict setValue:self.province forKey:kJavenAddressProvince];

    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:kJavenAddressAddress];
    self.cityCode = [aDecoder decodeObjectForKey:kJavenAddressCityCode];
    self.country = [aDecoder decodeObjectForKey:kJavenAddressCountry];
    self.city = [aDecoder decodeObjectForKey:kJavenAddressCity];
    self.idCard = [aDecoder decodeObjectForKey:kJavenAddressIdCard];
    self.nation = [aDecoder decodeObjectForKey:kJavenAddressNation];
    self.zip = [aDecoder decodeObjectForKey:kJavenAddressZip];
    self.telephone = [aDecoder decodeObjectForKey:kJavenAddressTelephone];
    self.name = [aDecoder decodeObjectForKey:kJavenAddressName];
    self.province = [aDecoder decodeObjectForKey:kJavenAddressProvince];
    return self;
}

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

    [aCoder encodeObject:_address forKey:kJavenAddressAddress];
    [aCoder encodeObject:_cityCode forKey:kJavenAddressCityCode];
    [aCoder encodeObject:_country forKey:kJavenAddressCountry];
    [aCoder encodeObject:_city forKey:kJavenAddressCity];
    [aCoder encodeObject:_idCard forKey:kJavenAddressIdCard];
    [aCoder encodeObject:_nation forKey:kJavenAddressNation];
    [aCoder encodeObject:_zip forKey:kJavenAddressZip];
    [aCoder encodeObject:_telephone forKey:kJavenAddressTelephone];
    [aCoder encodeObject:_name forKey:kJavenAddressName];
    [aCoder encodeObject:_province forKey:kJavenAddressProvince];
}

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

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


@end
