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

#import "UserInfoBaseClass.h"
#import "UserInfoAppConfig.h"
#import "UserInfoConsumer.h"
#import "UserInfoReseller.h"


NSString *const kUserInfoBaseClassCode = @"code";
NSString *const kUserInfoBaseClassMessage = @"message";
NSString *const kUserInfoBaseClassSessionId = @"sessionId";
NSString *const kUserInfoBaseClassAppConfig = @"appConfig";
NSString *const kUserInfoBaseClassConsumer = @"consumer";
NSString *const kUserInfoBaseClassReseller = @"reseller";


@interface UserInfoBaseClass ()

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

@end

@implementation UserInfoBaseClass

@synthesize code = _code;
@synthesize message = _message;
@synthesize sessionId = _sessionId;
@synthesize appConfig = _appConfig;
@synthesize consumer = _consumer;
@synthesize reseller = _reseller;


+ (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.
    [self updateModelObjectWithDictionary:dict];
    
    return self;
    
}

- (instancetype)updateModelObjectWithDictionary:(NSDictionary *)dict {
    // 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.code = [[self objectOrNilForKey:kUserInfoBaseClassCode fromDictionary:dict] doubleValue];
        self.message = [self objectOrNilForKey:kUserInfoBaseClassMessage fromDictionary:dict];
        self.sessionId = [self objectOrNilForKey:kUserInfoBaseClassSessionId fromDictionary:dict];
        self.appConfig = [UserInfoAppConfig modelObjectWithDictionary:[dict objectForKey:kUserInfoBaseClassAppConfig]];
        self.consumer = [UserInfoConsumer modelObjectWithDictionary:[dict objectForKey:kUserInfoBaseClassConsumer]];
        self.reseller = [UserInfoReseller modelObjectWithDictionary:[dict objectForKey:kUserInfoBaseClassReseller]];
        
    }
    
    return self;
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[NSNumber numberWithDouble:self.code] forKey:kUserInfoBaseClassCode];
    [mutableDict setValue:self.message forKey:kUserInfoBaseClassMessage];
    [mutableDict setValue:self.sessionId forKey:kUserInfoBaseClassSessionId];
    [mutableDict setValue:[self.appConfig dictionaryRepresentation] forKey:kUserInfoBaseClassAppConfig];
    [mutableDict setValue:[self.consumer dictionaryRepresentation] forKey:kUserInfoBaseClassConsumer];
    [mutableDict setValue:[self.reseller dictionaryRepresentation] forKey:kUserInfoBaseClassReseller];

    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.code = [aDecoder decodeDoubleForKey:kUserInfoBaseClassCode];
    self.message = [aDecoder decodeObjectForKey:kUserInfoBaseClassMessage];
    self.sessionId = [aDecoder decodeObjectForKey:kUserInfoBaseClassSessionId];
    self.appConfig = [aDecoder decodeObjectForKey:kUserInfoBaseClassAppConfig];
    self.consumer = [aDecoder decodeObjectForKey:kUserInfoBaseClassConsumer];
    self.reseller = [aDecoder decodeObjectForKey:kUserInfoBaseClassReseller];
    return self;
}

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

    [aCoder encodeDouble:_code forKey:kUserInfoBaseClassCode];
    [aCoder encodeObject:_message forKey:kUserInfoBaseClassMessage];
    [aCoder encodeObject:_sessionId forKey:kUserInfoBaseClassSessionId];
    [aCoder encodeObject:_appConfig forKey:kUserInfoBaseClassAppConfig];
    [aCoder encodeObject:_consumer forKey:kUserInfoBaseClassConsumer];
    [aCoder encodeObject:_reseller forKey:kUserInfoBaseClassReseller];
}

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

        copy.code = self.code;
        copy.message = [self.message copyWithZone:zone];
        copy.sessionId = [self.sessionId copyWithZone:zone];
        copy.appConfig = [self.appConfig copyWithZone:zone];
        copy.consumer = [self.consumer copyWithZone:zone];
        copy.reseller = [self.reseller copyWithZone:zone];
    }
    
    return copy;
}


@end
