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

#import "JavenResellerBigModel.h"



NSString *const kJavenResellerModelBirthday = @"birthday";
NSString *const kJavenResellerModelPortrait = @"portrait";
NSString *const kJavenResellerModelOwnerOrg = @"ownerOrg";
NSString *const kJavenResellerModelCode = @"code";
NSString *const kJavenResellerModelTelephone = @"telephone";
NSString *const kJavenResellerModelMobilephone = @"mobilephone";
NSString *const kJavenResellerModelUuid = @"uuid";
NSString *const kJavenResellerModelLastModifyInfo = @"lastModifyInfo";
NSString *const kJavenResellerModelSocialContact = @"socialContact";
NSString *const kJavenResellerModelCertificate = @"certificate";
NSString *const kJavenResellerModelVersion = @"version";
NSString *const kJavenResellerModelName = @"name";
NSString *const kJavenResellerModelInvitationCode = @"invitationCode";
NSString *const kJavenResellerModelState = @"state";
NSString *const kJavenResellerModelDomain = @"domain";
NSString *const kJavenResellerModelPasswordControl = @"passwordControl";
NSString *const kJavenResellerModelGender = @"gender";
NSString *const kJavenResellerModelRoles = @"roles";
NSString *const kJavenResellerModelPostalAddresses = @"postalAddresses";
NSString *const kJavenResellerModelIdCard = @"idCard";
NSString *const kJavenResellerModelReferrer = @"referrer";
NSString *const kJavenResellerModelShop = @"shop";
NSString *const kJavenResellerModelCreateInfo = @"createInfo";
NSString *const kJavenResellerModelOrder = @"order";
NSString *const kJavenResellerModelLoginControl = @"loginControl";
NSString *const kJavenResellerModelDescription = @"description";


@interface JavenResellerBigModel ()

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

@end

@implementation JavenResellerBigModel

@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:kJavenResellerModelBirthday fromDictionary:dict] doubleValue];
            self.portrait = [self objectOrNilForKey:kJavenResellerModelPortrait fromDictionary:dict];
//            self.ownerOrg = [JavenOwnerOrg modelObjectWithDictionary:[dict objectForKey:kJavenResellerModelOwnerOrg]];
            self.code = [self objectOrNilForKey:kJavenResellerModelCode fromDictionary:dict];
            self.telephone = [self objectOrNilForKey:kJavenResellerModelTelephone fromDictionary:dict];
            self.mobilephone = [self objectOrNilForKey:kJavenResellerModelMobilephone fromDictionary:dict];
            self.uuid = [self objectOrNilForKey:kJavenResellerModelUuid fromDictionary:dict];
            self.lastModifyInfo = [JavenLastModifyInfo modelObjectWithDictionary:[dict objectForKey:kJavenResellerModelLastModifyInfo]];
            self.socialContact = [JavenSocialContact modelObjectWithDictionary:[dict objectForKey:kJavenResellerModelSocialContact]];
            self.certificate = [JavenCertificate modelObjectWithDictionary:[dict objectForKey:kJavenResellerModelCertificate]];
            self.version = [[self objectOrNilForKey:kJavenResellerModelVersion fromDictionary:dict] doubleValue];
            self.name = [self objectOrNilForKey:kJavenResellerModelName fromDictionary:dict];
            self.invitationCode = [self objectOrNilForKey:kJavenResellerModelInvitationCode fromDictionary:dict];
            self.state = [self objectOrNilForKey:kJavenResellerModelState fromDictionary:dict];
            self.domain = [self objectOrNilForKey:kJavenResellerModelDomain fromDictionary:dict];
//            self.passwordControl = [JavenPasswordControl modelObjectWithDictionary:[dict objectForKey:kJavenResellerModelPasswordControl]];
            self.gender = [self objectOrNilForKey:kJavenResellerModelGender fromDictionary:dict];
    NSObject *receivedJavenRoles = [dict objectForKey:kJavenResellerModelRoles];
    NSMutableArray *parsedJavenRoles = [NSMutableArray array];
    if ([receivedJavenRoles isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedJavenRoles) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedJavenRoles addObject:[JavenRoles modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedJavenRoles isKindOfClass:[NSDictionary class]]) {
       [parsedJavenRoles addObject:[JavenRoles modelObjectWithDictionary:(NSDictionary *)receivedJavenRoles]];
    }

    self.roles = [NSArray arrayWithArray:parsedJavenRoles];
            self.postalAddresses = [JavenPostalAddresses modelObjectWithDictionary:[dict objectForKey:kJavenResellerModelPostalAddresses]];
            self.idCard = [JavenIdCard modelObjectWithDictionary:[dict objectForKey:kJavenResellerModelIdCard]];
            self.referrer = [JavenReferrer modelObjectWithDictionary:[dict objectForKey:kJavenResellerModelReferrer]];
            self.shop = [JavenShop modelObjectWithDictionary:[dict objectForKey:kJavenResellerModelShop]];
            self.createInfo = [JavenCreateInfo modelObjectWithDictionary:[dict objectForKey:kJavenResellerModelCreateInfo]];
            self.order = [[self objectOrNilForKey:kJavenResellerModelOrder fromDictionary:dict] doubleValue];
            self.loginControl = [JavenLoginControl modelObjectWithDictionary:[dict objectForKey:kJavenResellerModelLoginControl]];
            self.internalBaseClassDescription = [self objectOrNilForKey:kJavenResellerModelDescription fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[NSNumber numberWithDouble:self.birthday] forKey:kJavenResellerModelBirthday];
    [mutableDict setValue:self.portrait forKey:kJavenResellerModelPortrait];
//    [mutableDict setValue:[self.ownerOrg dictionaryRepresentation] forKey:kJavenResellerModelOwnerOrg];
    [mutableDict setValue:self.code forKey:kJavenResellerModelCode];
    [mutableDict setValue:self.telephone forKey:kJavenResellerModelTelephone];
    [mutableDict setValue:self.mobilephone forKey:kJavenResellerModelMobilephone];
    [mutableDict setValue:self.uuid forKey:kJavenResellerModelUuid];
    [mutableDict setValue:[self.lastModifyInfo dictionaryRepresentation] forKey:kJavenResellerModelLastModifyInfo];
    [mutableDict setValue:[self.socialContact dictionaryRepresentation] forKey:kJavenResellerModelSocialContact];
    [mutableDict setValue:[self.certificate dictionaryRepresentation] forKey:kJavenResellerModelCertificate];
    [mutableDict setValue:[NSNumber numberWithDouble:self.version] forKey:kJavenResellerModelVersion];
    [mutableDict setValue:self.name forKey:kJavenResellerModelName];
    [mutableDict setValue:self.invitationCode forKey:kJavenResellerModelInvitationCode];
    [mutableDict setValue:self.state forKey:kJavenResellerModelState];
    [mutableDict setValue:self.domain forKey:kJavenResellerModelDomain];
//    [mutableDict setValue:[self.passwordControl dictionaryRepresentation] forKey:kJavenResellerModelPasswordControl];
    [mutableDict setValue:self.gender forKey:kJavenResellerModelGender];
    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:kJavenResellerModelRoles];
    [mutableDict setValue:[self.postalAddresses dictionaryRepresentation] forKey:kJavenResellerModelPostalAddresses];
    [mutableDict setValue:[self.idCard dictionaryRepresentation] forKey:kJavenResellerModelIdCard];
    [mutableDict setValue:[self.referrer dictionaryRepresentation] forKey:kJavenResellerModelReferrer];
    [mutableDict setValue:[self.shop dictionaryRepresentation] forKey:kJavenResellerModelShop];
    [mutableDict setValue:[self.createInfo dictionaryRepresentation] forKey:kJavenResellerModelCreateInfo];
    [mutableDict setValue:[NSNumber numberWithDouble:self.order] forKey:kJavenResellerModelOrder];
    [mutableDict setValue:[self.loginControl dictionaryRepresentation] forKey:kJavenResellerModelLoginControl];
    [mutableDict setValue:self.internalBaseClassDescription forKey:kJavenResellerModelDescription];

    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 decodeDoubleForKey:kJavenResellerModelBirthday];
    self.portrait = [aDecoder decodeObjectForKey:kJavenResellerModelPortrait];
//    self.ownerOrg = [aDecoder decodeObjectForKey:kJavenResellerModelOwnerOrg];
    self.code = [aDecoder decodeObjectForKey:kJavenResellerModelCode];
    self.telephone = [aDecoder decodeObjectForKey:kJavenResellerModelTelephone];
    self.mobilephone = [aDecoder decodeObjectForKey:kJavenResellerModelMobilephone];
    self.uuid = [aDecoder decodeObjectForKey:kJavenResellerModelUuid];
    self.lastModifyInfo = [aDecoder decodeObjectForKey:kJavenResellerModelLastModifyInfo];
    self.socialContact = [aDecoder decodeObjectForKey:kJavenResellerModelSocialContact];
    self.certificate = [aDecoder decodeObjectForKey:kJavenResellerModelCertificate];
    self.version = [aDecoder decodeDoubleForKey:kJavenResellerModelVersion];
    self.name = [aDecoder decodeObjectForKey:kJavenResellerModelName];
    self.invitationCode = [aDecoder decodeObjectForKey:kJavenResellerModelInvitationCode];
    self.state = [aDecoder decodeObjectForKey:kJavenResellerModelState];
    self.domain = [aDecoder decodeObjectForKey:kJavenResellerModelDomain];
//    self.passwordControl = [aDecoder decodeObjectForKey:kJavenResellerModelPasswordControl];
    self.gender = [aDecoder decodeObjectForKey:kJavenResellerModelGender];
    self.roles = [aDecoder decodeObjectForKey:kJavenResellerModelRoles];
    self.postalAddresses = [aDecoder decodeObjectForKey:kJavenResellerModelPostalAddresses];
    self.idCard = [aDecoder decodeObjectForKey:kJavenResellerModelIdCard];
    self.referrer = [aDecoder decodeObjectForKey:kJavenResellerModelReferrer];
    self.shop = [aDecoder decodeObjectForKey:kJavenResellerModelShop];
    self.createInfo = [aDecoder decodeObjectForKey:kJavenResellerModelCreateInfo];
    self.order = [aDecoder decodeDoubleForKey:kJavenResellerModelOrder];
    self.loginControl = [aDecoder decodeObjectForKey:kJavenResellerModelLoginControl];
    self.internalBaseClassDescription = [aDecoder decodeObjectForKey:kJavenResellerModelDescription];
    return self;
}

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

    [aCoder encodeDouble:_birthday forKey:kJavenResellerModelBirthday];
    [aCoder encodeObject:_portrait forKey:kJavenResellerModelPortrait];
//    [aCoder encodeObject:_ownerOrg forKey:kJavenResellerModelOwnerOrg];
    [aCoder encodeObject:_code forKey:kJavenResellerModelCode];
    [aCoder encodeObject:_telephone forKey:kJavenResellerModelTelephone];
    [aCoder encodeObject:_mobilephone forKey:kJavenResellerModelMobilephone];
    [aCoder encodeObject:_uuid forKey:kJavenResellerModelUuid];
    [aCoder encodeObject:_lastModifyInfo forKey:kJavenResellerModelLastModifyInfo];
    [aCoder encodeObject:_socialContact forKey:kJavenResellerModelSocialContact];
    [aCoder encodeObject:_certificate forKey:kJavenResellerModelCertificate];
    [aCoder encodeDouble:_version forKey:kJavenResellerModelVersion];
    [aCoder encodeObject:_name forKey:kJavenResellerModelName];
    [aCoder encodeObject:_invitationCode forKey:kJavenResellerModelInvitationCode];
    [aCoder encodeObject:_state forKey:kJavenResellerModelState];
    [aCoder encodeObject:_domain forKey:kJavenResellerModelDomain];
//    [aCoder encodeObject:_passwordControl forKey:kJavenResellerModelPasswordControl];
    [aCoder encodeObject:_gender forKey:kJavenResellerModelGender];
    [aCoder encodeObject:_roles forKey:kJavenResellerModelRoles];
    [aCoder encodeObject:_postalAddresses forKey:kJavenResellerModelPostalAddresses];
    [aCoder encodeObject:_idCard forKey:kJavenResellerModelIdCard];
    [aCoder encodeObject:_referrer forKey:kJavenResellerModelReferrer];
    [aCoder encodeObject:_shop forKey:kJavenResellerModelShop];
    [aCoder encodeObject:_createInfo forKey:kJavenResellerModelCreateInfo];
    [aCoder encodeDouble:_order forKey:kJavenResellerModelOrder];
    [aCoder encodeObject:_loginControl forKey:kJavenResellerModelLoginControl];
    [aCoder encodeObject:_internalBaseClassDescription forKey:kJavenResellerModelDescription];
}

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

        copy.birthday = self.birthday;
        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
