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

#import "JavenResellerModel.h"


NSString *const kJavenResellerModelReseller = @"reseller";
NSString *const kJavenResellerModelCommission = @"commission";


@interface JavenResellerModel ()

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

@end

@implementation JavenResellerModel

@synthesize reseller = _reseller;
@synthesize commission = _commission;


+ (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.reseller = [JavenReseller modelObjectWithDictionary:[dict objectForKey:kJavenResellerModelReseller]];
            self.commission = [[self objectOrNilForKey:kJavenResellerModelCommission fromDictionary:dict] doubleValue];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[self.reseller dictionaryRepresentation] forKey:kJavenResellerModelReseller];
    [mutableDict setValue:[NSNumber numberWithDouble:self.commission] forKey:kJavenResellerModelCommission];

    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.reseller = [aDecoder decodeObjectForKey:kJavenResellerModelReseller];
    self.commission = [aDecoder decodeDoubleForKey:kJavenResellerModelCommission];
    return self;
}

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

    [aCoder encodeObject:_reseller forKey:kJavenResellerModelReseller];
    [aCoder encodeDouble:_commission forKey:kJavenResellerModelCommission];
}

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

        copy.reseller = [self.reseller copyWithZone:zone];
        copy.commission = self.commission;
    }
    
    return copy;
}


@end
