//
//  UserInfoConsumer.m
//
//  Created by 杰 张 on 16/7/22
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "UserInfoConsumer.h"
#import "UserInfoOwnerOrg.h"
#import "UserInfoLastModifyInfo.h"
#import "UserInfoPasswordControl.h"
#import "UserInfoCreateInfo.h"
#import "UserInfoLoginControl.h"


NSString *const kUserInfoConsumerBirthday = @"birthday";
NSString *const kUserInfoConsumerPortrait = @"portrait";
NSString *const kUserInfoConsumerOwnerOrg = @"ownerOrg";
NSString *const kUserInfoConsumerCode = @"code";
NSString *const kUserInfoConsumerTelephone = @"telephone";
NSString *const kUserInfoConsumerMobilephone = @"mobilephone";
NSString *const kUserInfoConsumerUuid = @"uuid";
NSString *const kUserInfoConsumerLastModifyInfo = @"lastModifyInfo";
NSString *const kUserInfoConsumerSocialContact = @"socialContact";
NSString *const kUserInfoConsumerVersion = @"version";
NSString *const kUserInfoConsumerName = @"name";
NSString *const kUserInfoConsumerState = @"state";
NSString *const kUserInfoConsumerDomain = @"domain";
NSString *const kUserInfoConsumerPasswordControl = @"passwordControl";
NSString *const kUserInfoConsumerIdCard = @"idCard";
NSString *const kUserInfoConsumerGender = @"gender";
NSString *const kUserInfoConsumerPostalAddresses = @"postalAddresses";
NSString *const kUserInfoConsumerCreateInfo = @"createInfo";
NSString *const kUserInfoConsumerOrder = @"order";
NSString *const kUserInfoConsumerLoginControl = @"loginControl";
NSString *const kUserInfoConsumerDescription = @"description";


@interface UserInfoConsumer ()

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

@end

@implementation UserInfoConsumer

@synthesize birthday = _birthday;
@synthesize portrait = _portrait;
@synthesize ownerOrg = _ownerOrg;
@synthesize code = _code;
@synthesize telephone = _telephone;
@synthesize mobilephone = _mobilephone;
@synthesize uuid = _uuid;
@synthesize lastModifyInfo = _lastModifyInfo;
@synthesize socialContact = _socialContact;
@synthesize version = _version;
@synthesize name = _name;
@synthesize state = _state;
@synthesize domain = _domain;
@synthesize passwordControl = _passwordControl;
@synthesize idCard = _idCard;
@synthesize gender = _gender;
@synthesize postalAddresses = _postalAddresses;
@synthesize createInfo = _createInfo;
@synthesize order = _order;
@synthesize loginControl = _loginControl;
@synthesize consumerDescription = _consumerDescription;


+ (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.birthday = [self objectOrNilForKey:kUserInfoConsumerBirthday fromDictionary:dict];
            self.portrait = [self objectOrNilForKey:kUserInfoConsumerPortrait fromDictionary:dict];
            self.ownerOrg = [UserInfoOwnerOrg modelObjectWithDictionary:[dict objectForKey:kUserInfoConsumerOwnerOrg]];
            self.code = [self objectOrNilForKey:kUserInfoConsumerCode fromDictionary:dict];
            self.telephone = [self objectOrNilForKey:kUserInfoConsumerTelephone fromDictionary:dict];
            self.mobilephone = [self objectOrNilForKey:kUserInfoConsumerMobilephone fromDictionary:dict];
            self.uuid = [self objectOrNilForKey:kUserInfoConsumerUuid fromDictionary:dict];
            self.lastModifyInfo = [UserInfoLastModifyInfo modelObjectWithDictionary:[dict objectForKey:kUserInfoConsumerLastModifyInfo]];
            self.socialContact = [self objectOrNilForKey:kUserInfoConsumerSocialContact fromDictionary:dict];
            self.version = [[self objectOrNilForKey:kUserInfoConsumerVersion fromDictionary:dict] doubleValue];
            self.name = [self objectOrNilForKey:kUserInfoConsumerName fromDictionary:dict];
            self.state = [self objectOrNilForKey:kUserInfoConsumerState fromDictionary:dict];
            self.domain = [self objectOrNilForKey:kUserInfoConsumerDomain fromDictionary:dict];
            self.passwordControl = [UserInfoPasswordControl modelObjectWithDictionary:[dict objectForKey:kUserInfoConsumerPasswordControl]];
            self.idCard = [self objectOrNilForKey:kUserInfoConsumerIdCard fromDictionary:dict];
            self.gender = [self objectOrNilForKey:kUserInfoConsumerGender fromDictionary:dict];
            self.postalAddresses = [self objectOrNilForKey:kUserInfoConsumerPostalAddresses fromDictionary:dict];
            self.createInfo = [UserInfoCreateInfo modelObjectWithDictionary:[dict objectForKey:kUserInfoConsumerCreateInfo]];
            self.order = [[self objectOrNilForKey:kUserInfoConsumerOrder fromDictionary:dict] doubleValue];
            self.loginControl = [UserInfoLoginControl modelObjectWithDictionary:[dict objectForKey:kUserInfoConsumerLoginControl]];
            self.consumerDescription = [self objectOrNilForKey:kUserInfoConsumerDescription fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.birthday forKey:kUserInfoConsumerBirthday];
    [mutableDict setValue:self.portrait forKey:kUserInfoConsumerPortrait];
    [mutableDict setValue:[self.ownerOrg dictionaryRepresentation] forKey:kUserInfoConsumerOwnerOrg];
    [mutableDict setValue:self.code forKey:kUserInfoConsumerCode];
    [mutableDict setValue:self.telephone forKey:kUserInfoConsumerTelephone];
    [mutableDict setValue:self.mobilephone forKey:kUserInfoConsumerMobilephone];
    [mutableDict setValue:self.uuid forKey:kUserInfoConsumerUuid];
    [mutableDict setValue:[self.lastModifyInfo dictionaryRepresentation] forKey:kUserInfoConsumerLastModifyInfo];
    [mutableDict setValue:self.socialContact forKey:kUserInfoConsumerSocialContact];
    [mutableDict setValue:[NSNumber numberWithDouble:self.version] forKey:kUserInfoConsumerVersion];
    [mutableDict setValue:self.name forKey:kUserInfoConsumerName];
    [mutableDict setValue:self.state forKey:kUserInfoConsumerState];
    [mutableDict setValue:self.domain forKey:kUserInfoConsumerDomain];
    [mutableDict setValue:[self.passwordControl dictionaryRepresentation] forKey:kUserInfoConsumerPasswordControl];
    [mutableDict setValue:self.idCard forKey:kUserInfoConsumerIdCard];
    [mutableDict setValue:self.gender forKey:kUserInfoConsumerGender];
    [mutableDict setValue:self.postalAddresses forKey:kUserInfoConsumerPostalAddresses];
    [mutableDict setValue:[self.createInfo dictionaryRepresentation] forKey:kUserInfoConsumerCreateInfo];
    [mutableDict setValue:[NSNumber numberWithDouble:self.order] forKey:kUserInfoConsumerOrder];
    [mutableDict setValue:[self.loginControl dictionaryRepresentation] forKey:kUserInfoConsumerLoginControl];
    [mutableDict setValue:self.consumerDescription forKey:kUserInfoConsumerDescription];

    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.birthday = [aDecoder decodeObjectForKey:kUserInfoConsumerBirthday];
    self.portrait = [aDecoder decodeObjectForKey:kUserInfoConsumerPortrait];
    self.ownerOrg = [aDecoder decodeObjectForKey:kUserInfoConsumerOwnerOrg];
    self.code = [aDecoder decodeObjectForKey:kUserInfoConsumerCode];
    self.telephone = [aDecoder decodeObjectForKey:kUserInfoConsumerTelephone];
    self.mobilephone = [aDecoder decodeObjectForKey:kUserInfoConsumerMobilephone];
    self.uuid = [aDecoder decodeObjectForKey:kUserInfoConsumerUuid];
    self.lastModifyInfo = [aDecoder decodeObjectForKey:kUserInfoConsumerLastModifyInfo];
    self.socialContact = [aDecoder decodeObjectForKey:kUserInfoConsumerSocialContact];
    self.version = [aDecoder decodeDoubleForKey:kUserInfoConsumerVersion];
    self.name = [aDecoder decodeObjectForKey:kUserInfoConsumerName];
    self.state = [aDecoder decodeObjectForKey:kUserInfoConsumerState];
    self.domain = [aDecoder decodeObjectForKey:kUserInfoConsumerDomain];
    self.passwordControl = [aDecoder decodeObjectForKey:kUserInfoConsumerPasswordControl];
    self.idCard = [aDecoder decodeObjectForKey:kUserInfoConsumerIdCard];
    self.gender = [aDecoder decodeObjectForKey:kUserInfoConsumerGender];
    self.postalAddresses = [aDecoder decodeObjectForKey:kUserInfoConsumerPostalAddresses];
    self.createInfo = [aDecoder decodeObjectForKey:kUserInfoConsumerCreateInfo];
    self.order = [aDecoder decodeDoubleForKey:kUserInfoConsumerOrder];
    self.loginControl = [aDecoder decodeObjectForKey:kUserInfoConsumerLoginControl];
    self.consumerDescription = [aDecoder decodeObjectForKey:kUserInfoConsumerDescription];
    return self;
}

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

    [aCoder encodeObject:_birthday forKey:kUserInfoConsumerBirthday];
    [aCoder encodeObject:_portrait forKey:kUserInfoConsumerPortrait];
    [aCoder encodeObject:_ownerOrg forKey:kUserInfoConsumerOwnerOrg];
    [aCoder encodeObject:_code forKey:kUserInfoConsumerCode];
    [aCoder encodeObject:_telephone forKey:kUserInfoConsumerTelephone];
    [aCoder encodeObject:_mobilephone forKey:kUserInfoConsumerMobilephone];
    [aCoder encodeObject:_uuid forKey:kUserInfoConsumerUuid];
    [aCoder encodeObject:_lastModifyInfo forKey:kUserInfoConsumerLastModifyInfo];
    [aCoder encodeObject:_socialContact forKey:kUserInfoConsumerSocialContact];
    [aCoder encodeDouble:_version forKey:kUserInfoConsumerVersion];
    [aCoder encodeObject:_name forKey:kUserInfoConsumerName];
    [aCoder encodeObject:_state forKey:kUserInfoConsumerState];
    [aCoder encodeObject:_domain forKey:kUserInfoConsumerDomain];
    [aCoder encodeObject:_passwordControl forKey:kUserInfoConsumerPasswordControl];
    [aCoder encodeObject:_idCard forKey:kUserInfoConsumerIdCard];
    [aCoder encodeObject:_gender forKey:kUserInfoConsumerGender];
    [aCoder encodeObject:_postalAddresses forKey:kUserInfoConsumerPostalAddresses];
    [aCoder encodeObject:_createInfo forKey:kUserInfoConsumerCreateInfo];
    [aCoder encodeDouble:_order forKey:kUserInfoConsumerOrder];
    [aCoder encodeObject:_loginControl forKey:kUserInfoConsumerLoginControl];
    [aCoder encodeObject:_consumerDescription forKey:kUserInfoConsumerDescription];
}

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

        copy.birthday = [self.birthday copyWithZone:zone];
        copy.portrait = [self.portrait copyWithZone:zone];
        copy.ownerOrg = [self.ownerOrg copyWithZone:zone];
        copy.code = [self.code copyWithZone:zone];
        copy.telephone = [self.telephone copyWithZone:zone];
        copy.mobilephone = [self.mobilephone copyWithZone:zone];
        copy.uuid = [self.uuid copyWithZone:zone];
        copy.lastModifyInfo = [self.lastModifyInfo copyWithZone:zone];
        copy.socialContact = [self.socialContact copyWithZone:zone];
        copy.version = self.version;
        copy.name = [self.name copyWithZone:zone];
        copy.state = [self.state copyWithZone:zone];
        copy.domain = [self.domain copyWithZone:zone];
        copy.passwordControl = [self.passwordControl copyWithZone:zone];
        copy.idCard = [self.idCard copyWithZone:zone];
        copy.gender = [self.gender copyWithZone:zone];
        copy.postalAddresses = [self.postalAddresses copyWithZone:zone];
        copy.createInfo = [self.createInfo copyWithZone:zone];
        copy.order = self.order;
        copy.loginControl = [self.loginControl copyWithZone:zone];
        copy.consumerDescription = [self.consumerDescription copyWithZone:zone];
    }
    
    return copy;
}


@end
