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

#import "JavenDetailReseller.h"
#import "JavenOwnerOrg.h"
#import "JavenLastModifyInfo.h"
#import "JavenCertificate.h"
#import "JavenPasswordControl.h"
#import "JavenIdCard.h"
#import "JavenShop.h"
#import "JavenCreateInfo.h"
#import "JavenLoginControl.h"


NSString *const kJavenDetailResellerBirthday = @"birthday";
NSString *const kJavenDetailResellerPortrait = @"portrait";
NSString *const kJavenDetailResellerOwnerOrg = @"ownerOrg";
NSString *const kJavenDetailResellerCode = @"code";
NSString *const kJavenDetailResellerTelephone = @"telephone";
NSString *const kJavenDetailResellerMobilephone = @"mobilephone";
NSString *const kJavenDetailResellerUuid = @"uuid";
NSString *const kJavenDetailResellerLastModifyInfo = @"lastModifyInfo";
NSString *const kJavenDetailResellerSocialContact = @"socialContact";
NSString *const kJavenDetailResellerCertificate = @"certificate";
NSString *const kJavenDetailResellerVersion = @"version";
NSString *const kJavenDetailResellerName = @"name";
NSString *const kJavenDetailResellerInvitationCode = @"invitationCode";
NSString *const kJavenDetailResellerState = @"state";
NSString *const kJavenDetailResellerDomain = @"domain";
NSString *const kJavenDetailResellerPasswordControl = @"passwordControl";
NSString *const kJavenDetailResellerGender = @"gender";
NSString *const kJavenDetailResellerRoles = @"roles";
NSString *const kJavenDetailResellerPostalAddresses = @"postalAddresses";
NSString *const kJavenDetailResellerIdCard = @"idCard";
NSString *const kJavenDetailResellerReferrer = @"referrer";
NSString *const kJavenDetailResellerShop = @"shop";
NSString *const kJavenDetailResellerCreateInfo = @"createInfo";
NSString *const kJavenDetailResellerOrder = @"order";
NSString *const kJavenDetailResellerLoginControl = @"loginControl";
NSString *const kJavenDetailResellerDescription = @"description";


@interface JavenDetailReseller ()

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

@end

@implementation JavenDetailReseller

@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 certificate = _certificate;
@synthesize version = _version;
@synthesize name = _name;
@synthesize invitationCode = _invitationCode;
@synthesize state = _state;
@synthesize domain = _domain;
@synthesize passwordControl = _passwordControl;
@synthesize gender = _gender;
@synthesize roles = _roles;
@synthesize postalAddresses = _postalAddresses;
@synthesize idCard = _idCard;
@synthesize referrer = _referrer;
@synthesize shop = _shop;
@synthesize createInfo = _createInfo;
@synthesize order = _order;
@synthesize loginControl = _loginControl;
@synthesize internalBaseClassDescription = _internalBaseClassDescription;


+ (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:kJavenDetailResellerBirthday fromDictionary:dict];
            self.portrait = [self objectOrNilForKey:kJavenDetailResellerPortrait fromDictionary:dict];
            self.ownerOrg = [JavenOwnerOrg modelObjectWithDictionary:[dict objectForKey:kJavenDetailResellerOwnerOrg]];
            self.code = [self objectOrNilForKey:kJavenDetailResellerCode fromDictionary:dict];
            self.telephone = [self objectOrNilForKey:kJavenDetailResellerTelephone fromDictionary:dict];
            self.mobilephone = [self objectOrNilForKey:kJavenDetailResellerMobilephone fromDictionary:dict];
            self.uuid = [self objectOrNilForKey:kJavenDetailResellerUuid fromDictionary:dict];
            self.lastModifyInfo = [JavenLastModifyInfo modelObjectWithDictionary:[dict objectForKey:kJavenDetailResellerLastModifyInfo]];
            self.socialContact = [self objectOrNilForKey:kJavenDetailResellerSocialContact fromDictionary:dict];
            self.certificate = [JavenCertificate modelObjectWithDictionary:[dict objectForKey:kJavenDetailResellerCertificate]];
            self.version = [[self objectOrNilForKey:kJavenDetailResellerVersion fromDictionary:dict] doubleValue];
            self.name = [self objectOrNilForKey:kJavenDetailResellerName fromDictionary:dict];
            self.invitationCode = [self objectOrNilForKey:kJavenDetailResellerInvitationCode fromDictionary:dict];
            self.state = [self objectOrNilForKey:kJavenDetailResellerState fromDictionary:dict];
            self.domain = [self objectOrNilForKey:kJavenDetailResellerDomain fromDictionary:dict];
            self.passwordControl = [JavenPasswordControl modelObjectWithDictionary:[dict objectForKey:kJavenDetailResellerPasswordControl]];
            self.gender = [self objectOrNilForKey:kJavenDetailResellerGender fromDictionary:dict];
            self.roles = [self objectOrNilForKey:kJavenDetailResellerRoles fromDictionary:dict];
            self.postalAddresses = [self objectOrNilForKey:kJavenDetailResellerPostalAddresses fromDictionary:dict];
            self.idCard = [JavenIdCard modelObjectWithDictionary:[dict objectForKey:kJavenDetailResellerIdCard]];
            self.referrer = [self objectOrNilForKey:kJavenDetailResellerReferrer fromDictionary:dict];
            self.shop = [JavenShop modelObjectWithDictionary:[dict objectForKey:kJavenDetailResellerShop]];
            self.createInfo = [JavenCreateInfo modelObjectWithDictionary:[dict objectForKey:kJavenDetailResellerCreateInfo]];
            self.order = [[self objectOrNilForKey:kJavenDetailResellerOrder fromDictionary:dict] doubleValue];
            self.loginControl = [JavenLoginControl modelObjectWithDictionary:[dict objectForKey:kJavenDetailResellerLoginControl]];
            self.internalBaseClassDescription = [self objectOrNilForKey:kJavenDetailResellerDescription fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.birthday forKey:kJavenDetailResellerBirthday];
    [mutableDict setValue:self.portrait forKey:kJavenDetailResellerPortrait];
    [mutableDict setValue:[self.ownerOrg dictionaryRepresentation] forKey:kJavenDetailResellerOwnerOrg];
    [mutableDict setValue:self.code forKey:kJavenDetailResellerCode];
    [mutableDict setValue:self.telephone forKey:kJavenDetailResellerTelephone];
    [mutableDict setValue:self.mobilephone forKey:kJavenDetailResellerMobilephone];
    [mutableDict setValue:self.uuid forKey:kJavenDetailResellerUuid];
    [mutableDict setValue:[self.lastModifyInfo dictionaryRepresentation] forKey:kJavenDetailResellerLastModifyInfo];
    [mutableDict setValue:self.socialContact forKey:kJavenDetailResellerSocialContact];
    [mutableDict setValue:[self.certificate dictionaryRepresentation] forKey:kJavenDetailResellerCertificate];
    [mutableDict setValue:[NSNumber numberWithDouble:self.version] forKey:kJavenDetailResellerVersion];
    [mutableDict setValue:self.name forKey:kJavenDetailResellerName];
    [mutableDict setValue:self.invitationCode forKey:kJavenDetailResellerInvitationCode];
    [mutableDict setValue:self.state forKey:kJavenDetailResellerState];
    [mutableDict setValue:self.domain forKey:kJavenDetailResellerDomain];
    [mutableDict setValue:[self.passwordControl dictionaryRepresentation] forKey:kJavenDetailResellerPasswordControl];
    [mutableDict setValue:self.gender forKey:kJavenDetailResellerGender];
    NSMutableArray *tempArrayForRoles = [NSMutableArray array];
    for (NSObject *subArrayObject in self.roles) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForRoles addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForRoles addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForRoles] forKey:kJavenDetailResellerRoles];
    [mutableDict setValue:self.postalAddresses forKey:kJavenDetailResellerPostalAddresses];
    [mutableDict setValue:[self.idCard dictionaryRepresentation] forKey:kJavenDetailResellerIdCard];
    [mutableDict setValue:self.referrer forKey:kJavenDetailResellerReferrer];
    [mutableDict setValue:[self.shop dictionaryRepresentation] forKey:kJavenDetailResellerShop];
    [mutableDict setValue:[self.createInfo dictionaryRepresentation] forKey:kJavenDetailResellerCreateInfo];
    [mutableDict setValue:[NSNumber numberWithDouble:self.order] forKey:kJavenDetailResellerOrder];
    [mutableDict setValue:[self.loginControl dictionaryRepresentation] forKey:kJavenDetailResellerLoginControl];
    [mutableDict setValue:self.internalBaseClassDescription forKey:kJavenDetailResellerDescription];

    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:kJavenDetailResellerBirthday];
    self.portrait = [aDecoder decodeObjectForKey:kJavenDetailResellerPortrait];
    self.ownerOrg = [aDecoder decodeObjectForKey:kJavenDetailResellerOwnerOrg];
    self.code = [aDecoder decodeObjectForKey:kJavenDetailResellerCode];
    self.telephone = [aDecoder decodeObjectForKey:kJavenDetailResellerTelephone];
    self.mobilephone = [aDecoder decodeObjectForKey:kJavenDetailResellerMobilephone];
    self.uuid = [aDecoder decodeObjectForKey:kJavenDetailResellerUuid];
    self.lastModifyInfo = [aDecoder decodeObjectForKey:kJavenDetailResellerLastModifyInfo];
    self.socialContact = [aDecoder decodeObjectForKey:kJavenDetailResellerSocialContact];
    self.certificate = [aDecoder decodeObjectForKey:kJavenDetailResellerCertificate];
    self.version = [aDecoder decodeDoubleForKey:kJavenDetailResellerVersion];
    self.name = [aDecoder decodeObjectForKey:kJavenDetailResellerName];
    self.invitationCode = [aDecoder decodeObjectForKey:kJavenDetailResellerInvitationCode];
    self.state = [aDecoder decodeObjectForKey:kJavenDetailResellerState];
    self.domain = [aDecoder decodeObjectForKey:kJavenDetailResellerDomain];
    self.passwordControl = [aDecoder decodeObjectForKey:kJavenDetailResellerPasswordControl];
    self.gender = [aDecoder decodeObjectForKey:kJavenDetailResellerGender];
    self.roles = [aDecoder decodeObjectForKey:kJavenDetailResellerRoles];
    self.postalAddresses = [aDecoder decodeObjectForKey:kJavenDetailResellerPostalAddresses];
    self.idCard = [aDecoder decodeObjectForKey:kJavenDetailResellerIdCard];
    self.referrer = [aDecoder decodeObjectForKey:kJavenDetailResellerReferrer];
    self.shop = [aDecoder decodeObjectForKey:kJavenDetailResellerShop];
    self.createInfo = [aDecoder decodeObjectForKey:kJavenDetailResellerCreateInfo];
    self.order = [aDecoder decodeDoubleForKey:kJavenDetailResellerOrder];
    self.loginControl = [aDecoder decodeObjectForKey:kJavenDetailResellerLoginControl];
    self.internalBaseClassDescription = [aDecoder decodeObjectForKey:kJavenDetailResellerDescription];
    return self;
}

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

    [aCoder encodeObject:_birthday forKey:kJavenDetailResellerBirthday];
    [aCoder encodeObject:_portrait forKey:kJavenDetailResellerPortrait];
    [aCoder encodeObject:_ownerOrg forKey:kJavenDetailResellerOwnerOrg];
    [aCoder encodeObject:_code forKey:kJavenDetailResellerCode];
    [aCoder encodeObject:_telephone forKey:kJavenDetailResellerTelephone];
    [aCoder encodeObject:_mobilephone forKey:kJavenDetailResellerMobilephone];
    [aCoder encodeObject:_uuid forKey:kJavenDetailResellerUuid];
    [aCoder encodeObject:_lastModifyInfo forKey:kJavenDetailResellerLastModifyInfo];
    [aCoder encodeObject:_socialContact forKey:kJavenDetailResellerSocialContact];
    [aCoder encodeObject:_certificate forKey:kJavenDetailResellerCertificate];
    [aCoder encodeDouble:_version forKey:kJavenDetailResellerVersion];
    [aCoder encodeObject:_name forKey:kJavenDetailResellerName];
    [aCoder encodeObject:_invitationCode forKey:kJavenDetailResellerInvitationCode];
    [aCoder encodeObject:_state forKey:kJavenDetailResellerState];
    [aCoder encodeObject:_domain forKey:kJavenDetailResellerDomain];
    [aCoder encodeObject:_passwordControl forKey:kJavenDetailResellerPasswordControl];
    [aCoder encodeObject:_gender forKey:kJavenDetailResellerGender];
    [aCoder encodeObject:_roles forKey:kJavenDetailResellerRoles];
    [aCoder encodeObject:_postalAddresses forKey:kJavenDetailResellerPostalAddresses];
    [aCoder encodeObject:_idCard forKey:kJavenDetailResellerIdCard];
    [aCoder encodeObject:_referrer forKey:kJavenDetailResellerReferrer];
    [aCoder encodeObject:_shop forKey:kJavenDetailResellerShop];
    [aCoder encodeObject:_createInfo forKey:kJavenDetailResellerCreateInfo];
    [aCoder encodeDouble:_order forKey:kJavenDetailResellerOrder];
    [aCoder encodeObject:_loginControl forKey:kJavenDetailResellerLoginControl];
    [aCoder encodeObject:_internalBaseClassDescription forKey:kJavenDetailResellerDescription];
}

- (id)copyWithZone:(NSZone *)zone
{
    JavenDetailReseller *copy = [[JavenDetailReseller 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.certificate = [self.certificate copyWithZone:zone];
        copy.version = self.version;
        copy.name = [self.name copyWithZone:zone];
        copy.invitationCode = [self.invitationCode copyWithZone:zone];
        copy.state = [self.state copyWithZone:zone];
        copy.domain = [self.domain copyWithZone:zone];
        copy.passwordControl = [self.passwordControl copyWithZone:zone];
        copy.gender = [self.gender copyWithZone:zone];
        copy.roles = [self.roles copyWithZone:zone];
        copy.postalAddresses = [self.postalAddresses copyWithZone:zone];
        copy.idCard = [self.idCard copyWithZone:zone];
        copy.referrer = [self.referrer copyWithZone:zone];
        copy.shop = [self.shop copyWithZone:zone];
        copy.createInfo = [self.createInfo copyWithZone:zone];
        copy.order = self.order;
        copy.loginControl = [self.loginControl copyWithZone:zone];
        copy.internalBaseClassDescription = [self.internalBaseClassDescription copyWithZone:zone];
    }
    
    return copy;
}


@end
